Reputation: 9885
I am doing a Android tutorial on YouTube using Android Studio. I have no highlighted errors in my syntax but when I run the program on my emulator and my phone it crashes without starting up.
I am trying to learn how to troubleshoot issues like this. I have noticed since taking on Java, these types of issues are happening more and more leaving me puzzled and lost.
All I have to go on is the logcat showing me errors and null pointer exceptions from different class files that I assume come with the SDK.
Would someone please explain this to me and how I can track down my issue with the information I am given from the IDE?
To be clear I am looking for a set of rules to follow for troubleshooting my applications from here on out. It would be amazing if I have provided enough information here to recieve an example.
My program,
package com.wuno;
import android.graphics.Color;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.widget.TextView;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
public class MainActivity extends ActionBarActivity {
TextView myMessage;
OnClickListener myRadioClickListener;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Button myButton=(Button)findViewById(R.id.myButton);
Button.OnClickListener myListener = new Button.OnClickListener() {
@Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),
getString(R.id.myText),
Toast.LENGTH_LONG).show();
}
};
myButton.setOnClickListener(myListener);
myMessage=(TextView)findViewById(R.id.myText);
myRadioClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
RadioButton myRadioButton=(RadioButton)v;
switch(myRadioButton.getId()) {
case R.id.myRadioButtonRed: myMessage.setTextColor(Color.RED); break;
case R.id.myRadioButtonGreen: myMessage.setTextColor(Color.GREEN); break;
case R.id.myRadioButtonBlue: myMessage.setTextColor(Color.BLUE); break;
}
}
};
RadioButton rbred, rbgreen, rbblue;
rbred=(RadioButton)findViewById(R.id.myRadioButtonRed);
rbgreen=(RadioButton)findViewById(R.id.myRadioButtonGreen);
rbblue=(RadioButton)findViewById(R.id.myRadioButtonBlue);
rbred.setOnClickListener(myRadioClickListener);
rbgreen.setOnClickListener(myRadioClickListener);
rbblue.setOnClickListener(myRadioClickListener);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
The logcat,
05-17 02:23:15.660 929-929/com.wuno D/AndroidRuntime﹕ Shutting down VM
05-17 02:23:15.660 929-929/com.wuno W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb1af0ba8)
05-17 02:23:15.680 929-929/com.wuno E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.wuno, PID: 929
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.wuno/com.wuno.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.wuno.MainActivity.onCreate(MainActivity.java:37)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
05-17 02:23:20.030 929-929/com.wuno I/Process﹕ Sending signal. PID: 929 SIG: 9
Upvotes: 0
Views: 90
Reputation: 437
First of all you need to understand compile time error and runtime error compile time error is which eclipse and sdk can judge when compiling the program like if u missing any semicolon or referencing a variable which is not declared yet etc...
while runtime error is not predictable unless it is deployed and taking your example of null pointer exception it means you are trying to access a location (variable) which is not yet intialized. When we declare a string like String str; what is there in the memory ? its null and when we assign a value to it then 01010 bits are stored in the memory (register) now its not null hence we can use it... so unless u run the program how will the compiler know..
one of the most typical example is divide by 0 if you have 2 input boxes one for numerator and other for denominator and a button if u click it will show the result as toast... now if you enter a value 0 in the denominator it is at mathematical error hence unless you run the program the error would not produce even tho the program compile and running and if denominator is 0 it will crash....
I hope didnt bored you with long explaination but i tried my level best to make it as brief as possible...
thx
Upvotes: 1
Reputation: 7343
This logCat line:
Caused by: java.lang.NullPointerException
at com.wuno.MainActivity.onCreate(MainActivity.java:37)
is from your code and NOT from the SDK.
Upon looking at your onCreate function, you did NOT initialize the layout that your app will use, that's why you get the nullPointer and the app does not start at all.
In your onCreate do this:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.id.layout.xml); //or whatever your layoutfile is called
//assign your buttons and functions here
}
In general, when debugging from the LogCat, look for the errors that look something similar to the one above. For example:
at com.packageName.className.functionName(className.java:lineNumber)
From there on, if it's a nullPointer, make sure you were able to initialize the variable you assigned, or if it's an indexOutOfBounds, make sure that you are looping within the bounds of your array/arrayList. There are a lot more other errors that you'll encounter in Android and I only gave a short list.
Upvotes: 1