2014-01-10 74 views
1

在主要活動了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。

+0

發佈你的XML的sigin類這將是問題Button blogin =(Button)findViewById(R.id.bRegister); –

+0

使用此代碼ImageButton blogin =(ImageButton)findViewById(R.id.imageButton1); –

+0

相反arg0.getContext()的使用MainActivity.this – jyomin

回答

3
Button blogin=(Button) findViewById(R.id.bRegister); 

我想你是在引用錯誤的按鈕id。

您需要檢查sign_in.xml中Button的編號。確保有按鈕sign_in.xml並確保ID匹配時你在Java代碼中引用

我想這應該是R.id.bLogin

編輯:

你有什麼是

<Button 
     android:id="@+id/button1" 

所以改變

Button blogin=(Button) findViewById(R.id.bRegister); 

Button bLogin = (Button)findViewById(R.id.button1); 

而且你嵌套LinearLayout雖然沒有錯,你可以有一個LinearLayout

+0

先生,我已經更新了,但還是同樣的問題 –

+0

@ user3118276後'sign_in.xml' – Raghunandan

+0

@ user3118276檢查我的編輯職位 – Raghunandan

0
Intent intent1 = new Intent(getApplicationContext(),Sign_In.class); 
        startActivity(intent1); 
+1

這不會導致'NullPointerException' – Raghunandan

1

試試這個..

變化

Button blogin=(Button) findViewById(R.id.blogin); 

Button blogin=(Button) findViewById(R.id.button1); 
      blogin.setOnClickListener(new OnClickListener() { 

       @Override 
       public void onClick(View arg0) { 
        // TODO Auto-generated method stub 
        Intent intent = new Intent(Sign_In.this,MessegeBox.class); 
        startActivity(intent); 
       } 
      }); 
+0

沒有必要更改爲'意圖intent =新意圖(Sign_In.this,MessegeBox.class); ' – Raghunandan

+0

@Raghunandan我只是爲了安全起見。這也會給出完美的結果。 – Hariharan

+0

關於'argo.getContext()'沒有什麼不安全的@ – Raghunandan

相關問題