I am pretty new to coding, so I have no idea what the errormessage is trying to tell me, also a GoogleSearch didn't really help me.
Here is the javaCode that I wrote:
Button mButton;
EditText mEdit;
Button testluck;
EditText nummer;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
testluck = (Button)findViewById(R.id.testluck);
mButton = (Button)findViewById(R.id.okay);
mEdit = (EditText)findViewById(R.id.name);
final TextView questionOne =(TextView)findViewById(R.id.questionOne);
final String name = mEdit.getText().toString();
final TextView textfield =(TextView)findViewById(R.id.textfield);
final TextView deineNummer =(TextView)findViewById(R.id.deineNummer);
nummer = (EditText)findViewById(R.id.name);
String nummerstring = nummer.getText().toString();
final int nummereins =Integer.parseInt(nummerstring);
final TextView unterschied =(TextView)findViewById(R.id.unterschied);
mButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
String name = mEdit.getText().toString();
questionOne.setText("Lass uns ein Spiel spielen, " + name + "!");
mEdit.setText("");
textfield.setText("Was ist deine Glückszahl zwischen 1 und 100?");
mButton.setText("");
}
});
testluck.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
deineNummer.setText("Deine gewählte Nummer ist " + nummereins);
int Gewinnzahl = (int) (Math.random()*100 +1);
int Unterschied = Math.abs(Gewinnzahl - nummereins);
unterschied.setText(Unterschied);
}
});
}
}
and when I try to run the app it crashes, showing me this ErrorLog:
FATAL EXCEPTION: main
Process: com.example.android.testyourluck, PID: 4829
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.android.testyourluck/com.example.android.testyourluck.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.example.android.testyourluck.MainActivity.onCreate(MainActivity.java:32)
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)
Can someone please tell me what I have done wrong? I would also love to hear how I could optimize my code!