应用程序在初始化变量时抛出Null Exception错误

时间:2013-05-10 02:52:10

标签: java android eclipse

目前我的应用程序正在提取数据库信息并填充TabView中保存的列表视图,但应用程序强制关闭才能运行。

当我初始化变量时,问题似乎存在。

下面是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

1 个答案:

答案 0 :(得分:2)

在这里:

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

您在updateDB方法

中使用它之前忘记初始化viewDBinitializeVariables()个实例