我已经为我的 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();
}
});
}
});