我的要求是点击图片我应该通过alertbuilder发出警报。 它发生得非常好。在alertbuilder中我指的是一个布局。然后我引用了小部件的ID。然后在onclick方法的范围内,我为这些小部件做了动作。
holder.source.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Source",Toast.LENGTH_LONG).show();
dialog = new Dialog(RecordingActivity2.this);
dialog.setTitle("Source File");
dialog.setContentView(R.layout.activity_read);
ImageButton im = (ImageButton)findViewById(R.id.close);
im.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
**If i am making actions here application is closing unexpectedly.**
}
});
dialog.show();
}
});
这是我的logcat输出
07-04 17:25:29.923: E/AndroidRuntime(1928): FATAL EXCEPTION: main
07-04 17:25:29.923: E/AndroidRuntime(1928): java.lang.NullPointerException
07-04 17:25:29.923: E/AndroidRuntime(1928): at com.pdev.activities.RecordingActivity2$VehicleAdapter$1.onClick(RecordingActivity2.java:209)
07-04 17:25:29.923: E/AndroidRuntime(1928): at android.view.View.performClick(View.java:3480)
07-04 17:25:29.923: E/AndroidRuntime(1928): at android.view.View$PerformClick.run(View.java:13983)
07-04 17:25:29.923: E/AndroidRuntime(1928): at android.os.Handler.handleCallback(Handler.java:605)
07-04 17:25:29.923: E/AndroidRuntime(1928): at android.os.Handler.dispatchMessage(Handler.java:92)
07-04 17:25:29.923: E/AndroidRuntime(1928): at android.os.Looper.loop(Looper.java:137)
07-04 17:25:29.923: E/AndroidRuntime(1928): at android.app.ActivityThread.main(ActivityThread.java:4340)
答案 0 :(得分:5)
替换
ImageButton im = (ImageButton)findViewById(R.id.close);
要
ImageButton im = (ImageButton)dialog.findViewById(R.id.close);