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