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
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);
}
}
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");
}
}
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!
Thanks in advance!
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.