gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 07:02:22 26 4
gpt4 key购买 nike

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

问题似乎出在我初始化变量时。

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

}

和日志猫:

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

最佳答案

在这里:

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

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

关于java - 初始化变量时应用程序抛出 Null Exception 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16474401/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com