2014-09-05 6 views
0

왜 setOnClickListener가 아래 오류를 제공합니까? MainActivity.java의사용자 지정 대화 상자에서 오류가 발생했습니다

java.lang.NullPointerException 
com.example.MainActivity.onClick(MainActivity.java:206) 
android.view.View.performClick(View.java:4240) 
android.view.View$PerformClick.run(View.java:17721) 
. 
. 
. 

코드는 다음과 같습니다 CustomDialogClass.java의

public class MainActivity extends Activity implements OnClickListener{ 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    View color=findViewById(R.id.vie); 
    color.setOnClickListener(this); 
} 

@Override 
public void onClick(View arg0) { 
if(arg0.getId()==R.id.vie){ 

     final CustomDialogClass cdc=new CustomDialogClass(this); 
     View dialogButton = cdc.findViewById(R.id.view3); 

     //**** the blow code that is in Block comment give an error**** 

     /* dialogButton.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       cdc.dismiss(); 
      } 
     }); */ 
     cdc.show(); 
} 
} 
} 

코드는 다음과 같습니다

public class CustomDialogClass extends Dialog { 

public Activity c; 
public Dialog d; 

public CustomDialogClass(Activity a) { 
    super(a); 
// TODO Auto-generated constructor stub 
this.c = a; 
} 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
super.onCreate(savedInstanceState); 
setContentView(R.layout.custom_dialog); 
setTitle("select"); 

} 
} 

및 custom_dialog.xml은 다음과 같습니다

<?xml version="1.0" encoding="utf-8"?> 
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:layout_gravity="center" 
    android:layout_margin="40dp" 
    android:gravity="center" > 

<View 
    android:id="@+id/view3" 
    android:layout_width="45dp" 
    android:layout_height="45dp" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:background="#24b5ed" /> 
</RelativeLayout> 

수있는 사람 내가 뭘 잘못하고 있다고 말해?

+0

What 's line MainActivity.java:206? –

+0

대화 상자 클래스의 onCreate() 메소드는'Dialog.show()'이후에만 호출됩니다. 대화 상자에는 다른보기를 찾을 수있는보기가 없습니다. – user3811368

+0

불필요한 코드를 삭제합니다! – user3824114

답변

0

파일 : SRC/CustomDialog.java

public class CustomDialog extends Activity { 

    private Button buttonClick; 

    public void onCreate(Bundle savedInstanceState) { 

     super.onCreate(savedInstanceState); 
     setContentView(R.layout.custom_dialog_main); 

     buttonClick = (Button) findViewById(R.id.buttonClick); 

     // add listener to button 
     buttonClick.setOnClickListener(new OnClickListener() { 

      @Override 
      public void onClick(View arg0) { 

       // Create custom dialog object 
       final Dialog dialog = new Dialog(CustomDialog.this); 
       // Include dialog.xml file 
       dialog.setContentView(R.layout.dialog); 
       // Set dialog title 
       dialog.setTitle("Custom Dialog"); 

       // set values for custom dialog components - text, image and button 
       TextView text = (TextView) dialog.findViewById(R.id.textDialog); 
       text.setText("Custom dialog Android example."); 
       ImageView image = (ImageView) dialog.findViewById(R.id.imageDialog); 
       image.setImageResource(R.drawable.image0); 

       dialog.show(); 

       Button declineButton = (Button) dialog.findViewById(R.id.declineButton); 
       // if decline button is clicked, close the custom dialog 
       declineButton.setOnClickListener(new OnClickListener() { 
        @Override 
        public void onClick(View v) { 
         // Close dialog 
         dialog.dismiss(); 
        } 
       }); 


      } 

     }); 

    } 

} 

파일 : 고해상도/레이아웃/custom_dialog_main.xml

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 

    <Button 
     android:id="@+id/buttonClick" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Click To Show Custom Dialog" /> 

</LinearLayout 

>

파일 : 고해상도/레이아웃/dialog.xml

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/imageDialog" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_marginRight="6dp" /> 

    <TextView 
     android:id="@+id/textDialog" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:textColor="#FFF" 
     android:layout_toRightOf="@+id/imageDialog"/> 

    <Button 
     android:id="@+id/declineButton" 
     android:layout_width="100px" 
     android:layout_height="wrap_content" 
     android:text=" Submit " 
     android:layout_marginTop="5dp" 
     android:layout_marginRight="5dp" 
     android:layout_below="@+id/textDialog" 
     android:layout_toRightOf="@+id/imageDialog" 
     /> 

</RelativeLayout> 
+0

위의 코드는 http://www.mkyong.com/android/android-custom-dialog-example/에서 볼 수 있지만 코드를 실행하지 않는 이유는 무엇입니까? – user3824114