2013-05-10 21 views
0

應用程序拋出NullException錯誤當前我的應用程序正在拉取數據庫信息並填充TabView中的列表視圖,但應用程序強制關閉後才能運行。當Initializng變量

當我初始化我的變量時,問題似乎在於。

下面是對的onCreate類和intializeVariables方法,其中的logcat顯示錯誤:

protected void onCreate(Bundle savedInstanceState) { 

     // Set activity to full screen!! 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     th = (TabHost) findViewById(R.id.tabhost); 

     th.setup(); 
     TabSpec specs = th.newTabSpec("tag1"); 
     // one tab 
     specs.setContent(R.id.tab1); 
     // what appears on actual tab 
     specs.setIndicator("Tools"); 
     th.addTab(specs); 
     specs = th.newTabSpec("tag2"); 
     // one tab 
     specs.setContent(R.id.tab2); 
     // what appears on actual tab 
     specs.setIndicator("Calorie Calculator"); 
     th.addTab(specs); 
     specs = th.newTabSpec("tag3"); 
     // one tab 
     specs.setContent(R.id.tab3); 
     // what appears on actual tab 
     specs.setIndicator("Your Stats!"); 
     th.addTab(specs); 

     initializeVariables(); 
     statListView = getListView(); 
     statListView.setOnItemClickListener(statViewListener); 

     // map each name to a TextView 
     String[] from = new String[] { "name" }; 
     int[] to = new int[] { R.id.tvStatistics }; 
     statAdapter = new SimpleCursorAdapter(MainActivity.this, 
       R.layout.stat_list, null, from, to); 
     setListAdapter(statAdapter); // set adapter 

     ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
       this, R.array.spinActMultArray, 
       android.R.layout.simple_spinner_item); 
     adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 
     spinActMult.setAdapter(adapter); 
     spinActMult.setOnItemSelectedListener(new spinActMultFunction()); 

    } 

    private void initializeVariables() { 
     spinActMult = (Spinner) findViewById(R.id.spinActivityMultipler); 
     activityMultiplier = (TextView) findViewById(R.id.tvActMult); 
     maintCalories = (TextView) findViewById(R.id.tvMaintLevel); 
     calcCalories = (Button) findViewById(R.id.btnCalcCalories); 
     goalPicker = (RadioGroup) findViewById(R.id.rgGoalPicker); 
     calorieResult = (TextView) findViewById(R.id.tvCalResults); 
     lbmResult = (TextView) findViewById(R.id.tvLBMResult); 
     bmrResult = (TextView) findViewById(R.id.tvBMRResult); 
     etWeight = (EditText) findViewById(R.id.etWInput); 
     weightInt = etWeight.getText().toString(); 
     etBodyfat = (EditText) findViewById(R.id.etBFInput); 
     bodyfatInt = etBodyfat.getText().toString(); 
     goalPicker.setOnCheckedChangeListener(this); 
     calcCalories.setOnClickListener(this); 

     StopwatchActivity = (Button) findViewById(R.id.btnStopwatchActivity); 
     mapARun = (Button) findViewById(R.id.btnMapARun); 
     weightConverter = (Button) findViewById(R.id.btnWeightConverter); 

     mapARun.setOnClickListener(this); 
     weightConverter.setOnClickListener(this); 
     StopwatchActivity.setOnClickListener(this); 
     updateDB.setOnClickListener(this); 
     viewDB.setOnClickListener(this); 

    } 

和logcat的:

05-10 03:41:52.687: E/AndroidRuntime(5974): FATAL EXCEPTION: main 
05-10 03:41:52.687: E/AndroidRuntime(5974): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.uhi.fatfighter/com.uhi.fatfighter.MainActivity}: java.lang.NullPointerException 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2185) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2210) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.ActivityThread.access$600(ActivityThread.java:142) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1208) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.os.Handler.dispatchMessage(Handler.java:99) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.os.Looper.loop(Looper.java:137) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.ActivityThread.main(ActivityThread.java:4928) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at java.lang.reflect.Method.invokeNative(Native Method) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at java.lang.reflect.Method.invoke(Method.java:511) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:558) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at dalvik.system.NativeStart.main(Native Method) 
05-10 03:41:52.687: E/AndroidRuntime(5974): Caused by: java.lang.NullPointerException 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at com.uhi.fatfighter.MainActivity.initializeVariables(MainActivity.java:129) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at com.uhi.fatfighter.MainActivity.onCreate(MainActivity.java:86) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.Activity.performCreate(Activity.java:5008) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2139) 
05-10 03:41:52.687: E/AndroidRuntime(5974):  ... 11 more 

回答

2

在這裏:

updateDB.setOnClickListener(this); //<<<< 
    viewDB.setOnClickListener(this); //<<<< 

你忘了在內使用它之前初始化updateDBviewDB實例方法