2013-10-20 174 views
0

當我開始我的活動我收到一個錯誤。這裏是我的活動:錯誤聲明TextViews

public class DebtList extends Activity { 

    String debtNames1, debtNames2, debtNames3, debtNames4, debtNames5 = null; 
    Double debtAmount1, debtAmount2, debtAmount3, debtAmount4, debtAmount5; 
    Double debtRate1, debtRate2, debtRate3, debtRate4, debtRate5; 
    Integer debtTerm1, debtTerm2, debtTerm3, debtTerm4, debtTerm5; 

    //Name Displays 
    TextView debtName1 = (TextView) findViewById(R.id.dispName1); //logcat states this is the location of the error 
    TextView debtName2 = (TextView) findViewById(R.id.dispName2); 
    TextView debtName3 = (TextView) findViewById(R.id.dispName3); 
    TextView debtName4 = (TextView) findViewById(R.id.dispName4); 
    TextView debtName5 = (TextView) findViewById(R.id.dispName5); 

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

這裏是logcat的副本:

10-20 05:59:15.014: D/AndroidRuntime(389): Shutting down VM 
10-20 05:59:15.014: W/dalvikvm(389): threadid=1: thread exiting with uncaught exception (group=0x4001d800) 
10-20 05:59:15.094: D/dalvikvm(389): GC_FOR_MALLOC freed 5706 objects/305808 bytes in 68ms 
10-20 05:59:15.094: E/AndroidRuntime(389): FATAL EXCEPTION: main 
10-20 05:59:15.094: E/AndroidRuntime(389): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{biz.midl.debttracking/biz.midl.debttracking.DebtList}: java.lang.NullPointerException 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.os.Handler.dispatchMessage(Handler.java:99) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.os.Looper.loop(Looper.java:123) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.ActivityThread.main(ActivityThread.java:4627) 
10-20 05:59:15.094: E/AndroidRuntime(389): at java.lang.reflect.Method.invokeNative(Native Method) 
10-20 05:59:15.094: E/AndroidRuntime(389): at java.lang.reflect.Method.invoke(Method.java:521) 
10-20 05:59:15.094: E/AndroidRuntime(389): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
10-20 05:59:15.094: E/AndroidRuntime(389): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
10-20 05:59:15.094: E/AndroidRuntime(389): at dalvik.system.NativeStart.main(Native Method) 
10-20 05:59:15.094: E/AndroidRuntime(389): Caused by: java.lang.NullPointerException 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.Activity.findViewById(Activity.java:1637) 
10-20 05:59:15.094: E/AndroidRuntime(389): at biz.midl.debttracking.DebtList.<init>(DebtList.java:16) 
10-20 05:59:15.094: E/AndroidRuntime(389): at java.lang.Class.newInstanceImpl(Native Method) 
10-20 05:59:15.094: E/AndroidRuntime(389): at java.lang.Class.newInstance(Class.java:1429) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.Instrumentation.newActivity(Instrumentation.java:1021) 
10-20 05:59:15.094: E/AndroidRuntime(389): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577) 

我試着做最後的TextView debtName1 = ......,它提供了相同的錯誤消息。

請讓我知道什麼可以做,以減輕錯誤。

回答

3

所有五個TextView的作業都必須是的的onCreate功能。

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

    //Name Displays 
    TextView debtName1 = (TextView) findViewById(R.id.dispName1); 
    TextView debtName2 = (TextView) findViewById(R.id.dispName2); 
    TextView debtName3 = (TextView) findViewById(R.id.dispName3); 
    TextView debtName4 = (TextView) findViewById(R.id.dispName4); 
    TextView debtName5 = (TextView) findViewById(R.id.dispName5); 

    // Some other stuff .. 
    } 

在創建對象(您的DebtList活動)時,會在全局級別進行變量賦值。那時xml還沒有膨脹(在setContentView(..)中發生),因此該xml視圖的id(R.id.dispNameX)不會屈服於真實的對象。因此NullPointerException。