我开始活动时收到错误消息。这是我的活动:
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 = .....并且它提供了相同的错误消息。
请让我知道如何减轻错误。
答案 0 :(得分: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。