gpt4 book ai didi

android - 多个 SeekBar Activity 致命崩溃

转载 作者:行者123 更新时间:2023-11-29 16:10:21 25 4
gpt4 key购买 nike

我在 Android 中有这个带有多个 SeekBars 的 MainActivity 类:

package com.simplemathgame;

import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class MainActivity extends Activity implements OnSeekBarChangeListener {
private TextView numberOfAddDrills = (TextView) findViewById(R.id.add_drills_number);
private TextView numberOfSubDrills = (TextView) findViewById(R.id.sub_drills_number);
private TextView numberOfMulDrills = (TextView) findViewById(R.id.mul_drills_number);
private TextView numberOfDivDrills = (TextView) findViewById(R.id.div_drills_number);


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

SeekBar addSeekBar = (SeekBar) findViewById(R.id.add_seek_bar);
SeekBar subSeekBar = (SeekBar) findViewById(R.id.sub_seek_bar);
SeekBar mulSeekBar = (SeekBar) findViewById(R.id.mul_seek_bar);
SeekBar divSeekBar = (SeekBar) findViewById(R.id.div_seek_bar);

addSeekBar.setOnSeekBarChangeListener(this);
subSeekBar.setOnSeekBarChangeListener(this);
mulSeekBar.setOnSeekBarChangeListener(this);
divSeekBar.setOnSeekBarChangeListener(this);
Log.w("here","2");
}

@Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2){
Log.w("here","3");
switch (arg0.getId())
{
case R.id.add_seek_bar:
numberOfAddDrills.setText(""+arg1);
break;
case R.id.sub_seek_bar:
numberOfSubDrills.setText(""+arg1);
break;
case R.id.mul_seek_bar:
numberOfMulDrills.setText(""+arg1);
break;
case R.id.div_seek_bar:
numberOfDivDrills.setText(""+arg1);
break;
}
}

@Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}

@Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub

}
}

这是 LogCat:

12-20 08:58:49.402: D/dalvikvm(668): GC_EXTERNAL_ALLOC freed 43K, 53% free 2546K/5379K, external 1917K/2137K, paused 43ms
12-20 08:58:52.563: D/AndroidRuntime(668): Shutting down VM
12-20 08:58:52.563: W/dalvikvm(668): threadid=1: thread exiting with uncaught exception (group=0x40015560)
12-20 08:58:52.582: E/AndroidRuntime(668): FATAL EXCEPTION: main
12-20 08:58:52.582: E/AndroidRuntime(668): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.simplemathgame/com.simplemathgame.MainActivity}: java.lang.NullPointerException
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.os.Handler.dispatchMessage(Handler.java:99)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.os.Looper.loop(Looper.java:123)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.ActivityThread.main(ActivityThread.java:3683)
12-20 08:58:52.582: E/AndroidRuntime(668): at java.lang.reflect.Method.invokeNative(Native Method)
12-20 08:58:52.582: E/AndroidRuntime(668): at java.lang.reflect.Method.invoke(Method.java:507)
12-20 08:58:52.582: E/AndroidRuntime(668): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-20 08:58:52.582: E/AndroidRuntime(668): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-20 08:58:52.582: E/AndroidRuntime(668): at dalvik.system.NativeStart.main(Native Method)
12-20 08:58:52.582: E/AndroidRuntime(668): Caused by: java.lang.NullPointerException
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.Activity.findViewById(Activity.java:1647)
12-20 08:58:52.582: E/AndroidRuntime(668): at com.simplemathgame.MainActivity.<init>(MainActivity.java:11)
12-20 08:58:52.582: E/AndroidRuntime(668): at java.lang.Class.newInstanceImpl(Native Method)
12-20 08:58:52.582: E/AndroidRuntime(668): at java.lang.Class.newInstance(Class.java:1409)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
12-20 08:58:52.582: E/AndroidRuntime(668): ... 11 more
12-20 08:58:54.923: I/Process(668): Sending signal. PID: 668 SIG: 9

我不知道这个 java.lang.RuntimeException 在哪里。谢谢

最佳答案

你应该看看这里,看看那一点上有什么是空的:

12-20 08:58:52.582: E/AndroidRuntime(668): Caused by: java.lang.NullPointerException
12-20 08:58:52.582: E/AndroidRuntime(668): at android.app.Activity.findViewById(Activity.java:1647)
12-20 08:58:52.582: E/AndroidRuntime(668): at com.simplemathgame.MainActivity.<init>(MainActivity.java:11)

所以第 11 行是崩溃的地方。

问题是您不能从那里调用 findViewById,而是在 OnCreate 方法中调用它们,在 setContentView 之后。

希望对您有所帮助。

关于android - 多个 SeekBar Activity 致命崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13968434/

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