gpt4 book ai didi

android - Android神秘的空指针异常

转载 作者:行者123 更新时间:2023-12-03 15:49:20 25 4
gpt4 key购买 nike

自从我上传了我的应用程序以来,我收到了许多java.lang.NullPointerException崩溃报告。我正在努力理解该问题,因为它不会在我亲自测试的仿真器或物理设备上崩溃。以下是其中一份报告的示例:

java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:2072)
at android.view.View.performClick(View.java:2408)
at android.view.View$PerformClick.run(View.java:8817)
at android.os.Handler.handleCallback(Handler.java:587)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:143)
at android.app.ActivityThread.main(ActivityThread.java:4914)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at com.bryan.barrett.countdownforandroid.practiceSubscreen.openNumbersSelect(Unknown Source)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:521)
at android.view.View$1.onClick(View.java:2067)
... 11 more
Caused by: java.lang.NullPointerException
... 15 more

我猜想这是在按下“openNumbersSelect”按钮时在“practiceSubscreen”中崩溃的。这是该按钮的代码片段:
public void openNumbersSelect(View view){
Intent iNum = new Intent(this, numbersSelect.class);
String gamemode = "practice"; //practice mode
iNum.putExtra("gamemode", gamemode);
startActivity(iNum);

overridePendingTransition(R.anim.fadein, R.anim.fadeout);
if(sound){
soundPool.play(Main.SNDclick, audio.getStreamVolume(AudioManager.STREAM_MUSIC),
audio.getStreamVolume(AudioManager.STREAM_MUSIC), 1, 0, 1f);
}
}

这是要打开的以下NumbersSelect类中的onCreate代码:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers_select);

Intent i = getIntent();
gamemode = i.getStringExtra("gamemode");
gameround = i.getIntExtra("gameround", 0) + 1;
points = i.getIntExtra("points", 0);

//Shared Preferences
prefs = PreferenceManager.getDefaultSharedPreferences(this);
sound = prefs.getBoolean("sound", true);

//Sounds
setVolumeControlStream(AudioManager.STREAM_MUSIC);
audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
soundPool = Main.soundPool;

//Big Numbers
for (int n=25; n<=100; n+=25){ //add 25, 50, 75, 100
bignumbers.add(n);
}
//Small Numbers
for (int m=1; m<=10; m++){ //adds 1-10 twice
smallnumbers.add(m);
smallnumbers.add(m);
}

//Screen metrics
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int screenw = metrics.widthPixels; int screenh = metrics.heightPixels;
int unitw = (screenw/14); int unith = (screenh/40); //divide screen into 14 columns x 40 rows...

num1 = (Button)findViewById(R.id.num1img); nums.add(num1);
num2 = (Button)findViewById(R.id.num2img); nums.add(num2);
num3 = (Button)findViewById(R.id.num3img); nums.add(num3);
num4 = (Button)findViewById(R.id.num4img); nums.add(num4);
num5 = (Button)findViewById(R.id.num5img); nums.add(num5);
num6 = (Button)findViewById(R.id.num6img); nums.add(num6);
for(int n=0; n<6; n++){
nums.get(n).setHeight(screenh/4); nums.get(n).setTextSize(Main.fontSize);
nums.get(n).setWidth(screenw/6); nums.get(n).setPadding(1, 1, 1, 1);
}
lrg = (Button)findViewById(R.id.largebutton); lrg.setWidth((unitw*200)/35);
sml = (Button)findViewById(R.id.smallbutton); sml.setWidth((unitw*200)/35);

Typeface scribble = Typeface.createFromAsset(getAssets(), "fonts/scribble.ttf");

TextView numbersSeltext = (TextView)findViewById(R.id.numbersSeltext);
numbersSeltext.setTypeface(scribble);
numbersSeltext.setWidth(screenw); numbersSeltext.setHeight(unith*8);
if(gamemode.equals("full")){
numbersSeltext.setText("Round " + gameround + ": Select 6 Numbers");
}
}

如果有人能对此有所了解,将不胜感激!这是一个令人沮丧的问题,因为它在我自己测试过的设备上可以正常工作,并且基于反馈,它在许多设备上都可以正常工作,因此我什至不确定代码是否有问题,或者某些设备是否存在问题。内置。如果需要,我可以抛出更多的崩溃报告或代码!

提前致谢!

最佳答案

如果您是我,我将测试所有Button num1,num2 ...和许多其他 View 在使用之前是否为空,因为如果由于某种原因尚未创建 View ,则简单地获取 View 可能会引发nullpointerexception(例如,可滚动的列表 View 项仅填充可放入屏幕的 View )。如果您让use知道您的布局XML是什么,可能会有所帮助。

关于android - Android神秘的空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16158294/

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