在主要活動了java.lang.RuntimeException:無法啓動活動顯示java.lang.NullPointerException
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_page);
login=(Button) findViewById(R.id.bLogin);
register=(Button) findViewById(R.id.bRegister);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(arg0.getContext(),Sign_In.class);
startActivity(intent1);
}
});
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(arg0.getContext(),Register.class);
startActivity(intent2);
}
});
}
In Sign_In Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
Button blogin=(Button) findViewById(R.id.bLogin);
blogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(arg0.getContext(),MessegeBox.class);
startActivity(intent);
}
});
}
XML佈局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_marginTop="50dip"
android:background="#FFFFFF"
android:orientation="vertical" >
<EditText
android:id="@+id/phone_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:gravity="center"
android:hint="Mobile Number"
android:inputType="phone"
android:maxLength="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/phone_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:gravity="center"
android:hint="Invivation Pin (If Any)"
android:inputType="phone"
android:maxLength="10" />
<Button
android:id="@+id/button1"
style="@style/ButtonText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:background="@drawable/blue_button"
android:text="LOGIN" />
</LinearLayout>
</LinearLayout>
這裏是日誌貓
01-10 00:40:49.897: E/AndroidRuntime(1030): FATAL EXCEPTION: main
01-10 00:40:49.897: E/AndroidRuntime(1030): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.multiplesystem.nosms/net.multiplesystem.nosms.Sign_In}: java.lang.NullPointerException
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.os.Looper.loop(Looper.java:137)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-10 00:40:49.897: E/AndroidRuntime(1030): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 00:40:49.897: E/AndroidRuntime(1030): at java.lang.reflect.Method.invoke(Method.java:525)
01-10 00:40:49.897: E/AndroidRuntime(1030): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-10 00:40:49.897: E/AndroidRuntime(1030): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-10 00:40:49.897: E/AndroidRuntime(1030): at dalvik.system.NativeStart.main(Native Method)
01-10 00:40:49.897: E/AndroidRuntime(1030): Caused by: java.lang.NullPointerException
01-10 00:40:49.897: E/AndroidRuntime(1030): at net.multiplesystem.nosms.Sign_In.onCreate(Sign_In.java:18)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.Activity.performCreate(Activity.java:5133)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-10 00:40:49.897: E/AndroidRuntime(1030): ... 11 more
當我點擊主要活動的登錄按鈕時。它顯示空指針異常。已經嘗試了很多,但無法解決問題。請告訴我我錯在哪裏。提前Thanx。
發佈你的XML的sigin類這將是問題Button blogin =(Button)findViewById(R.id.bRegister); –
使用此代碼ImageButton blogin =(ImageButton)findViewById(R.id.imageButton1); –
相反arg0.getContext()的使用MainActivity.this – jyomin