2013-09-29 64 views
1

今天是我第一天用java。我無法弄清楚這個錯誤的原因。無法實例化活動ComponentInfo {com.example.ragefacefolks/com.example.ragefacefolks.MainActivity}:java.lang.NullPointerException

MainActivity.java是這樣的下面

package com.example.ragefacefolks; 

import android.os.Bundle; 
import android.view.ContextMenu; 
import android.view.MenuInflater; 
import android.view.View; 
import android.view.ContextMenu.ContextMenuInfo; 
import android.widget.Button; 
import android.app.Activity; 

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
    } 

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

    @Override 
    public void registerForContextMenu(View view) { 
     // TODO Auto-generated method stub 
     super.registerForContextMenu(bMain); 
    } 

    @Override 
    public void onCreateContextMenu(ContextMenu menu, View v, 
      ContextMenuInfo menuInfo) { 
     // TODO Auto-generated method stub 
     super.onCreateContextMenu(menu, v, menuInfo); 
     MenuInflater inflater = getMenuInflater(); 
     inflater.inflate(R.menu.context_menu, menu); 

    } 


} 

不會很難爲你們知道它的說理。需要一點幫助。 日誌貓顯示這些結果

09-27 07:59:24.677: D/AndroidRuntime(555): Shutting down VM 
09-27 07:59:24.677: W/dalvikvm(555): threadid=1: thread exiting with uncaught exception (group=0x40015560) 
09-27 07:59:24.707: E/AndroidRuntime(555): FATAL EXCEPTION: main 
09-27 07:59:24.707: E/AndroidRuntime(555): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.ragefacefolks/com.example.ragefacefolks.MainActivity}: java.lang.NullPointerException 
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569) 
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
09-27 07:59:24.707: E/AndroidRuntime(555): at android.os.Handler.dispatchMessage(Handler.java:99) 
09-27 07:59:24.707: E/AndroidRuntime(555): at android.os.Looper.loop(Looper.java:123) 
09-27 07:59:24.707: E/AndroidRuntime(555): at android.app.ActivityThread.main(ActivityThread.java:3683) 
09-27 07:59:24.707: E/AndroidRuntime(555): at java.lang.reflect.Method.invokeNative(Native Method) 

回答

1

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

應在onCreatesetContentView後。在這樣的類(特別是,不能onCreate之前被調用)

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

:您初始化它的任何方法

private Button bMain; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    bMain = (Button) findViewById(R.id.button1); 
} 
+0

你打我8秒! – Szymon

+0

Thankyou,Raghunadan。我知道這是一個新手問題。 :) –

+0

@ArsalanShah歡迎您。如果它有助於標記答案爲 – Raghunandan

1

之外,你不能打電話。

將此代碼移動到onCreate

+0

好的。謝啦! :) –

1

刪除線

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

,改變onCreate method.-

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    Button bMain = (Button) findViewById(R.id.button1); 
    registerForContextMenu(bMain); 
} 

沒有必要重寫registerForContextMenu方法。

+0

謝謝,桑托斯! :) –

相關問題