非常奇怪且令人沮丧的错误。
只要我在模拟器上加载我的应用程序并尝试打开它,我就会在on create方法中收到空指针异常错误。
正如标题中所说,这段代码已经好几周了。
这是logcat:
02-14 20:03:45.813: E/AndroidRuntime(285): FATAL EXCEPTION: main
02-14 20:03:45.813: E/AndroidRuntime(285): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.flybase2/com.example.flybase2.MainActivity}: java.lang.NullPointerException
02-14 20:03:45.813: E/AndroidRuntime(285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.os.Looper.loop(Looper.java:123)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-14 20:03:45.813: E/AndroidRuntime(285): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 20:03:45.813: E/AndroidRuntime(285): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 20:03:45.813: E/AndroidRuntime(285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-14 20:03:45.813: E/AndroidRuntime(285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-14 20:03:45.813: E/AndroidRuntime(285): at dalvik.system.NativeStart.main(Native Method)
02-14 20:03:45.813: E/AndroidRuntime(285): Caused by: java.lang.NullPointerException
02-14 20:03:45.813: E/AndroidRuntime(285): at com.example.flybase2.MainActivity.onCreate(MainActivity.java:33)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
02-14 20:03:45.813: E/AndroidRuntime(285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
02-14 20:03:45.813: E/AndroidRuntime(285): ... 11 more
这是班级:
package com.example.flybase2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener{
Button bContacts;
Button bAppointment;
Button bShopping;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.appmenus);
bContacts = (Button) findViewById(R.id.btnCons);
bAppointment = (Button) findViewById(R.id.btnAppoint);
bShopping = (Button) findViewById(R.id.btnShop);
bContacts.setOnClickListener(this);
bAppointment.setOnClickListener(this);
bShopping.setOnClickListener(this);
}
@Override
public void onClick(View passedAppChoice) {
switch(passedAppChoice.getId()){
case (R.id.btnCons):
Intent cons = new Intent("com.example.flybase2.contacts");
startActivity(cons);
break;
case (R.id.btnAppoint):
Intent Appoint = new Intent("com.example.flybase2.appointmantMenu");
startActivity(Appoint);
break;
case (R.id.btnShop):
Intent shop = new Intent("com.example.flybase2.ShoppingList");
startActivity(shop);
break;
}
}
}
Appmenus.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal" >
<ImageView
android:id="@+id/imgLink"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="0.91"
android:src="@drawable/menulist" />
<TextView
android:id="@+id/textView1"
android:layout_width="165dp"
android:layout_height="match_parent"
android:layout_weight="0.14"
android:gravity="center"
android:text="Menu"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40sp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/btnCons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="@drawable/consicon"
android:src="@drawable/consicon"
android:text="Contacts " />
<Button
android:id="@+id/btnToDo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="@drawable/todo"
android:src="@drawable/todo"
android:text="'To Do' List" />
<Button
android:id="@+id/btnShop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="@drawable/shoppingmenu"
android:src="@drawable/shoppingmenu"
android:text="Shopping List" />
<Button
android:id="@+id/btnAppoint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="@drawable/appointmainicon"
android:src="@drawable/appointmainicon"
android:text="Appointment List" />
</LinearLayout>
</ScrollView>
</LinearLayout>
希望有人能看到我出错的地方!。
答案 0 :(得分:0)
R.layout.appmenus
是否包含以上所有按钮(请检查ID)。由于R.id是一个应用程序范围的命名空间,它也可以在其他布局中显式意图的格式应为:
Intent explicitIntent = new Intent(InvokingActivity.this,InvokedActivity.class);
startActivity(explicitIntent);
答案 1 :(得分:0)
在初始化按钮对象后,在onCreate中添加此日志打印。
Log.d("MainActivityTAG", "bContacts ="+bContacts+ ", bAppointment="+bAppointment+", bShopping="+bShopping);
以下代码中的一个按钮对象似乎是null
。
bContacts = (Button) findViewById(R.id.btnCons);
bAppointment = (Button) findViewById(R.id.btnAppoint);
bShopping = (Button) findViewById(R.id.btnShop);
以上日志打印将让您知道哪个按钮实例为null
,您将了解您是否获得了ID错误的按钮或设置了错误的布局,但没有定义其中一个按钮。< / p>
答案 2 :(得分:0)
最终启动了一个新项目并简单地复制了代码,因此非常快。它现在正在运作。我不知道发生了什么,但相信我的XML appmenu.xml文件布局中存在某种腐败现象。出于某种原因,我认为eclipse不再能够“看到”约会按钮,即使它明显存在并被其ID引用。非常感谢所有贡献者。