Saroj Shrestha Saroj Shrestha - 3 months ago 11
Java Question

Unfortunately, AndroidApp got stopped when i click any button in Eclipse AVD

When I run app. in Eclipse AVD it works fine. But, As soon as I click Add button: the app got stopped with message. Unfortunately, Project has stopped.


MainActivity.java


package com.example.firstproject;


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.widget.Button;
import android.widget.EditText;


public class MainActivity extends Activity {

Button button,btnAdd,btnSub;
EditText val1,val2;

double num1,num2,res;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);


val1 = (EditText) findViewById(R.id.txtVal1);
val2 = (EditText) findViewById(R.id.txtVal2);
btnAdd = (Button) findViewById(R.id.btnSum);
btnSub = (Button) findViewById(R.id.btnDiff);

btnAdd.setOnClickListener(new MyOnClickListener(this));
}


@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;
}

}



MyOnClickListener.java


package com.example.firstproject;

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Toast;

public class MyOnClickListener extends MainActivity implements OnClickListener{

MainActivity caller;
public MyOnClickListener(MainActivity activity) {
this.caller = activity;
}
public void onClick(View view) {
num1 = Double.parseDouble(val1.getText().toString());
num2 = Double.parseDouble(val2.getText().toString());
res = num1 + num2;
Toast.makeText(this, String.valueOf(res), Toast.LENGTH_SHORT).show();
}

}



activity_main.xml


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<EditText
android:id="@+id/txtVal1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >

<requestFocus />
</EditText>

<EditText
android:id="@+id/txtVal2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />

<Button
android:id="@+id/btnSum"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:text="Add" />

<Button
android:id="@+id/btnDiff"
android:layout_width="127dp"
android:layout_height="wrap_content"
android:text="Subtract" />

</LinearLayout>


Error_log

07-13 01:02:24.800: D/(972): HostConnection::get() New Host Connection established 0xb8b52560, tid 972
07-13 01:02:24.930: W/EGL_emulation(972): eglSurfaceAttrib not implemented
07-13 01:02:24.960: D/OpenGLRenderer(972): Enabling debug mode 0
07-13 01:02:42.092: D/AndroidRuntime(972): Shutting down VM
07-13 01:02:42.092: W/dalvikvm(972): threadid=1: thread exiting with uncaught exception (group=0xb3aa3b90)
07-13 01:02:42.152: E/AndroidRuntime(972): FATAL EXCEPTION: main
07-13 01:02:42.152: E/AndroidRuntime(972): Process: com.example.firstproject, PID: 972
07-13 01:02:42.152: E/AndroidRuntime(972): java.lang.NullPointerException
07-13 01:02:42.152: E/AndroidRuntime(972): at com.example.firstproject.MyOnClickListener.onClick(MyOnClickListener.java:14)
07-13 01:02:42.152: E/AndroidRuntime(972): at android.view.View.performClick(View.java:4424)
07-13 01:02:42.152: E/AndroidRuntime(972): at android.view.View$PerformClick.run(View.java:18383)
07-13 01:02:42.152: E/AndroidRuntime(972): at android.os.Handler.handleCallback(Handler.java:733)
07-13 01:02:42.152: E/AndroidRuntime(972): at android.os.Handler.dispatchMessage(Handler.java:95)
07-13 01:02:42.152: E/AndroidRuntime(972): at android.os.Looper.loop(Looper.java:137)
07-13 01:02:42.152: E/AndroidRuntime(972): at android.app.ActivityThread.main(ActivityThread.java:4998)
07-13 01:02:42.152: E/AndroidRuntime(972): at java.lang.reflect.Method.invokeNative(Native Method)
07-13 01:02:42.152: E/AndroidRuntime(972): at java.lang.reflect.Method.invoke(Method.java:515)
07-13 01:02:42.152: E/AndroidRuntime(972): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
07-13 01:02:42.152: E/AndroidRuntime(972): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
07-13 01:02:42.152: E/AndroidRuntime(972): at dalvik.system.NativeStart.main(Native Method)

Answer

You have got null object reference of EditText while getting num1 value from val1 variable EditText. So access this variable using caller.

public void onClick(View view) {
        num1 = Double.parseDouble(caller.val1.getText().toString());
        num2 = Double.parseDouble(caller.val2.getText().toString());
        res = num1 + num2;
        Toast.makeText(caller, String.valueOf(res), Toast.LENGTH_SHORT).show();
}