Android的神秘的空指针异常 [英] Android mysterious null pointer exception

查看:233
本文介绍了Android的神秘的空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

既然我已经上传我的应用我已经收到了许多显示java.lang.NullPointerException崩溃报告。我努力理解,因为它不能在模拟器或物理设备我亲自测试崩溃的问题。下面是报告的一个例子:

Since I have uploaded my app I have received many java.lang.NullPointerException crash reports. I am struggling to understand the problem as it does not crash on the emulators or the physical devices i have personally tested. Below is an example of one of the reports:

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

我presume这是在practiceSubscreen崩溃的时候openNumbersSelect按钮是pressed。这是我该按钮件code的:

I presume this is crashing in the 'practiceSubscreen' when 'openNumbersSelect' button is pressed. This is my piece of code for that button:

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 code:

and this is the onCreate code in the following numbersSelect class that it is meant to open:

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

如果任何人都可以对这个有何启示,将不胜AP preciated!这是一个令人沮丧的问题,因为它完美的作品对我已经测试自己的设备,而且似乎根据反馈意见,以便即时通讯甚至不知道在很多设备工作正常,如果有与code,或者有问题的某些设备是如何构建的。如果需要,我可以扔了更多的崩溃报告或code!

If anyone could shed any light on this it would be greatly appreciated! It is a frustrating problem as it works flawlessly on devices i have tested myself, and it seems to be working fine on a lot of devices based on feedback so im not even sure if there is a problem with the code or if its how certain devices are built.. I can throw up more crash reports or code if needed!

在此先感谢!

推荐答案

如果我是你,我会测试所有按钮NUM1,NUM2 ......和许多其他的观点是否为空在使用前,因为只得到一个如果视图尚未创建进入存在因任何原因(例如,一个滚动的列表视图项仅填充视图,可以放入屏幕)视图可能引发NullPointerException异常。它可以帮助更多,如果你让使用者知道什么是你的布局XML。

If I were you, I would test whether all the Buttons num1,num2... and numerous other views are null before they are used, because simply getting a view might raise nullpointerexception if the view is not yet created into existence for whatever reasons (for example, a scrollable listview items only populate views that can fit into screen). It might help more if you let use know what is your layout xml.

这篇关于Android的神秘的空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆