我已经为我的alertdialog做了一个布局,在那里我有一个调用dialog.dismiss()
的退出按钮以及一些将数据保存到首选项的按钮。我已经将onClickListeners设置为all,我的对话框没有任何按钮,例如正面或负面,只有布局中的那些。
然而,在调用对话框时,应用程序崩溃了。
08-20 07:58:01.291: E/AndroidRuntime(3297): FATAL EXCEPTION: main 08-20 07:58:01.291: E/AndroidRuntime(3297): java.lang.NullPointerException 08-20 07:58:01.291: E/AndroidRuntime(3297): at com.xcommerce.androidstore.HomeScreen$21.onClick(HomeScreen.java:1183) 08-20 07:58:01.291: E/AndroidRuntime(3297): at android.view.View.performClick(View.java:4202) 08-20 07:58:01.291: E/AndroidRuntime(3297): at android.view.View$PerformClick.run(View.java:17340) 08-20 07:58:01.291: E/AndroidRuntime(3297): at android.os.Handler.handleCallback(Handler.java:725) 08-20 07:58:01.291: E/AndroidRuntime(3297): at android.os.Handler.dispatchMessage(Handler.java:92) 08-20 07:58:01.291: E/AndroidRuntime(3297): at android.os.Looper.loop(Looper.java:137) 08-20 07:58:01.291: E/AndroidRuntime(3297): at android.app.ActivityThread.main(ActivityThread.java:5039) 08-20 07:58:01.291: E/AndroidRuntime(3297): at java.lang.reflect.Method.invokeNative(Native Method) 08-20 07:58:01.291: E/AndroidRuntime(3297): at java.lang.reflect.Method.invoke(Method.java:511) 08-20 07:58:01.291: E/AndroidRuntime(3297): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 08-20 07:58:01.291: E/AndroidRuntime(3297): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 08-20 07:58:01.291: E/AndroidRuntime(3297): at dalvik.system.NativeStart.main(Native Method)
TextView sort = (TextView) findViewById(R.id.sorteaza);
final Dialog sortDialog = new Dialog(this, R.style.DialogTheme);
sort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater inflater = HomeScreen.this.getLayoutInflater();
View sortView = inflater.inflate(R.layout.sort_layout, null);
sortDialog.setContentView(sortView);
WindowManager manager = (WindowManager) getSystemService(Activity.WINDOW_SERVICE);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(sortDialog.getWindow().getAttributes());
lp.width = manager.getDefaultDisplay().getWidth();
lp.height = manager.getDefaultDisplay().getHeight() - manager.getDefaultDisplay().getHeight()/5;
sortDialog.getWindow().setAttributes(lp);
sortDialog.show();
//sort controls
SharedPreferences sortPref = getSharedPreferences("SORT", 0);
final SharedPreferences.Editor ed = sortPref.edit();
Button exitSort, sortAZ, sortZA, sortCheap, sortExpensive;
exitSort = (Button) findViewById(R.id.exitSort);
sortAZ = (Button) findViewById(R.id.sortAZ);
sortZA = (Button) findViewById(R.id.sortZA);
sortCheap = (Button) findViewById(R.id.sortCheap);
sortExpensive = (Button) findViewById(R.id.sortExpensive);
exitSort.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sortDialog.dismiss();
}
});
sortAZ.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ed.putString("sort", "AZ");
ed.commit();
}
});
sortZA.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ed.putString("sort", "ZA");
ed.commit();
}
});
sortCheap.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ed.putString("sort", "cheap");
ed.commit();
}
});
sortExpensive.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
ed.putString("sort", "expensive");
ed.commit();
}
});
}
});
答案 0 :(得分:2)
您需要使用对话框对象来初始化视图。正如Luksprog所建议的那样。
您为自定义视图充气并将其设置为对话框。你有膨胀的布局中的按钮。因此,使用对话框对象初始化按钮。
exitSort = (Button) sortDialog.findViewById(R.id.exitSort);
sortAZ = (Button) sortDialog.findViewById(R.id.sortAZ);
sortZA = (Button) sortDialog.findViewById(R.id.sortZA);
sortCheap = (Button) sortDialog.findViewById(R.id.sortCheap);
sortExpensive = (Button) sortDialog.findViewById(R.id.sortExpensive);
答案 1 :(得分:1)
你的问题排在第:sortDialog.dismiss();
"
sortDialog "
是DialogBuilder
个对象,它不是Dialog
object
。
因此,在调用show()
函数时,您需要一个变量来处理对话框。
例如:
final Dialog dg = sortDialog.show();
and close dialog by "dg":
dg.dismiss();