0

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

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

下面是 logcat 显示错误的 onCreate 类和 intializeVariables 方法:

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);

    }

和日志猫:

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
4

1 回答 1

2

在这里:

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

在方法中使用它之前忘记初始化updateDBviewDB实例initializeVariables()

于 2013-05-10T02:57:07.277 回答