程序加入对话框代码LogCat报错空指针
来源:互联网 发布:中国 印度 知乎 编辑:程序博客网 时间:2024/05/18 19:45
最近在做简易日记本,其中有一步是点击“addButton”(即“添加新日志”键)后弹出对话框提示是否要添加新日志,但是将这部分代码写进程序后,LogCat显示空指针,初学安卓开发,还请各位大神指点~
对话框代码如下:
addBn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("是否放弃当前日志?");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
setContentView(R.layout.activity_main);
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您已取消添加新日志!", Toast.LENGTH_LONG).show();
}
});
}
});
LogCat显示如下:
07-30 02:55:36.854: E/AndroidRuntime(393): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.diary/com.example.diary.MainActivity}: java.lang.NullPointerException
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.os.Looper.loop(Looper.java:123)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 02:55:36.854: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 02:55:36.854: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 02:55:36.854: E/AndroidRuntime(393): at dalvik.system.NativeStart.main(Native Method)
07-30 02:55:36.854: E/AndroidRuntime(393): Caused by: java.lang.NullPointerException
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.Activity.findViewById(Activity.java:1637)
07-30 02:55:36.854: E/AndroidRuntime(393): at com.example.diary.MainActivity.<init>(MainActivity.java:26)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.Class.newInstanceImpl(Native Method)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.Class.newInstance(Class.java:1429)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-30 02:55:36.854: E/AndroidRuntime(393): ... 11 more
对话框代码如下:
addBn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("是否放弃当前日志?");
builder.setPositiveButton("是", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
setContentView(R.layout.activity_main);
}
});
builder.setNegativeButton("否", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "您已取消添加新日志!", Toast.LENGTH_LONG).show();
}
});
}
});
LogCat显示如下:
07-30 02:55:36.854: E/AndroidRuntime(393): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.diary/com.example.diary.MainActivity}: java.lang.NullPointerException
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.os.Looper.loop(Looper.java:123)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.main(ActivityThread.java:4627)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.reflect.Method.invokeNative(Native Method)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.reflect.Method.invoke(Method.java:521)
07-30 02:55:36.854: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-30 02:55:36.854: E/AndroidRuntime(393): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-30 02:55:36.854: E/AndroidRuntime(393): at dalvik.system.NativeStart.main(Native Method)
07-30 02:55:36.854: E/AndroidRuntime(393): Caused by: java.lang.NullPointerException
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.Activity.findViewById(Activity.java:1637)
07-30 02:55:36.854: E/AndroidRuntime(393): at com.example.diary.MainActivity.<init>(MainActivity.java:26)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.Class.newInstanceImpl(Native Method)
07-30 02:55:36.854: E/AndroidRuntime(393): at java.lang.Class.newInstance(Class.java:1429)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
07-30 02:55:36.854: E/AndroidRuntime(393): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
07-30 02:55:36.854: E/AndroidRuntime(393): ... 11 more
0 0
- 程序加入对话框代码LogCat报错空指针
- 给对话框程序加入背景图片
- 基于对话框的MFC程序加入菜单资源
- 在logcat加入system.out
- Android程序加入代码混淆器
- logCat获取程序信息
- (转)在logcat加入system.out
- MFC中提示对话框的滑动弹出和程序图标加入系统托盘的实现
- MFC中提示对话框的滑动弹出和程序图标加入系统托盘的实现
- 在对话框中加入工具栏
- 在对话框中加入工具栏
- 对话框中加入属性页
- 为对话框中加入工具栏
- 在对话框中加入状态栏
- android日志打印代码 logcat
- 获取对话框句柄 对话框指针
- 获取对话框句柄 对话框指针
- Android logcat报错Nullpointexception空指针
- 链表 C++描述
- C# BackgroundWorker的使用
- Android线程调度机制
- Debug Current Instruction Pointer
- PARSEC测试集的应用领域和working set的大小
- 程序加入对话框代码LogCat报错空指针
- 141.Linked List Cycle (判断一个单链表是否有环)
- nyoj35表达式求值
- 利用反射输出类的结构:方法、构造器签名和全部域名
- iOS中MapKit
- 什么类型客户适合做展示型网站
- oracle公用分页存储过程
- android开发中出现的错误集(不断更新)
- 欢迎使用CSDN-markdown编辑器