Android神秘的空指针异常

时间:2013-04-22 23:15:35

标签: android crash null nullpointerexception illegalstateexception

由于我上传了我的应用程序,因此收到了许多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);
    }
} 

这是以下要打开的numberSelect类中的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");
        }
}

如果有人能对此有所了解,我们将不胜感激!这是一个令人沮丧的问题,因为它在我自己测试的设备上完美运行,并且它似乎在很多基于反馈的设备上正常工作所以我甚至不确定代码是否存在问题或者它是如何确定某些设备的已建成..如果需要,我可以抛出更多崩溃报告或代码!

提前致谢!

1 个答案:

答案 0 :(得分:0)

如果我是你,我将测试所有Buttons num1,num2 ......和许多其他视图在使用之前是否为null,因为如果尚未创建视图,仅仅获取视图可能会引发nullpointerexception无论什么原因(例如,可滚动列表视图项目仅填充可适合屏幕的视图)。如果你让我们知道你的布局xml是什么,它可能会有所帮助。