我试图通过一个包含3个按钮的对话框传递一个包但是我得到了空对象引用的错误,这是我的代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reshte);
// TODO Auto-generated method stub
ImageButton zistbt=(ImageButton)findViewById(R.id.imageButton1);
zistbt.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
CustomDialogClass cdd=new CustomDialogClass(reshte.this);
cdd.show();
}
});
}
public class CustomDialogClass extends Dialog implements
android.view.View.OnClickListener {
public Activity c;
public Button sat1,sat2,sat3;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.difselect);
getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
//buttons
sat1.setOnClickListener(this);
sat2.setOnClickListener(this);
sat3.setOnClickListener(this);
//font
final TextView textdialog = (TextView) this.findViewById(R.id.txt_rate);
//font
String fontPath2 = "fonts/yekan.ttf";
// Loading Font Face
Typeface tf2 = Typeface.createFromAsset(getAssets(), fontPath2);
textdialog.setTypeface(tf2);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
Intent intent = new Intent(reshte.this,List1.class);
Bundle bundle = new Bundle();
bundle.putString("url","http://api.safshari.com/konkor/zist.json");
bundle.putString("daste","zist");
bundle.putString("sath","1");
intent.putExtras(bundle);
startActivity(intent);
break;
case R.id.button2:
Intent intent1 = new Intent(reshte.this,List1.class);
Bundle bundle1 = new Bundle();
bundle1.putString("url","http://api.safshari.com/konkor/zist.json");
bundle1.putString("daste","zist");
bundle1.putString("sath","2");
intent1.putExtras(bundle1);
startActivity(intent1);
break;
case R.id.button3:
Intent intent2 = new Intent(reshte.this,List1.class);
Bundle bundle2 = new Bundle();
bundle2.putString("url","http://api.safshari.com/konkor/zist.json");
bundle2.putString("daste","zist");
bundle2.putString("sath","3");
intent2.putExtras(bundle2);
startActivity(intent2);
break;
}
// TODO Auto-generated method stub
dismiss();
}
}
}
我的日志猫没有点击监听器对话框工作得很好但是当我添加它们时我得到这个错误:
07-04 12:50:34.947: E/AndroidRuntime(18822): FATAL EXCEPTION: main
07-04 12:50:34.947: E/AndroidRuntime(18822): Process: com.safshari.konkor, PID: 18822
07-04 12:50:34.947: E/AndroidRuntime(18822): java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
07-04 12:50:34.947: E/AndroidRuntime(18822): at com.safshari.konkor.reshte$CustomDialogClass.onCreate(reshte.java:52)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.app.Dialog.dispatchOnCreate(Dialog.java:373)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.app.Dialog.show(Dialog.java:274)
07-04 12:50:34.947: E/AndroidRuntime(18822): at com.safshari.konkor.reshte$1.onClick(reshte.java:31)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.view.View.performClick(View.java:4780)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.view.View$PerformClick.run(View.java:19866)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.os.Handler.handleCallback(Handler.java:739)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.os.Handler.dispatchMessage(Handler.java:95)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.os.Looper.loop(Looper.java:135)
07-04 12:50:34.947: E/AndroidRuntime(18822): at android.app.ActivityThread.main(ActivityThread.java:5254)
07-04 12:50:34.947: E/AndroidRuntime(18822): at java.lang.reflect.Method.invoke(Native Method)
07-04 12:50:34.947: E/AndroidRuntime(18822): at java.lang.reflect.Method.invoke(Method.java:372)
07-04 12:50:34.947: E/AndroidRuntime(18822): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
07-04 12:50:34.947: E/AndroidRuntime(18822): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
答案 0 :(得分:0)
在附加点击侦听器之前初始化按钮
sat1 = (Button) findViewById(R.id.sat1); //Replace sat1 with id defined in XML layout
答案 1 :(得分:0)
您的按钮尚未初始化,这就是他们传递null的原因。
首先在OnCreate中初始化它们