The main activity has buttons to open new activities for various things. One activity, a calculator, closes as soon as I click the button. The other activities work fine.
The xml file for the activity is question is:
<AbsoluteLayout android:id="@+id/widget0"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADDITION OF NUMBERS"
android:id="@+id/textView1"
android:layout_x="73dip"
android:layout_y="28dip"></TextView>
<TextView android:layout_width="wrap_content"
android:layout_x="36dip"
android:layout_height="wrap_content"
android:text="First Amount"
android:id="@+id/textView2"
android:layout_y="80dip"></TextView>
<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:id="@+id/amount1"
android:layout_height="wrap_content"
android:layout_y="62dip"></EditText>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Amount"
android:id="@+id/textView3"
android:layout_x="36dip"
android:layout_y="169dip"></TextView>
<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="169dip"
android:id="@+id/amount2"
android:layout_height="wrap_content"
android:layout_y="152dip"></EditText>
<Button android:layout_width="wrap_content"
android:id="@+id/calculate"
android:layout_x="41dip"
android:layout_height="wrap_content"
android:text="Calculate"
android:layout_y="232dip"></Button>
<EditText android:text=""
android:layout_width="wrap_content"
android:layout_x="172dip"
android:id="@+id/tt"
android:layout_height="wrap_content"
android:layout_y="232dip"></EditText>
</AbsoluteLayout>
The java file is:
package com.example.welderhelper;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Button;
import android.view.View;
public class Calculators extends ActionBarActivity
{
EditText amount1;
EditText amount2;
TextView tt;
Button calculate;
double x=0;
double y=0;
double z=0;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculators);
initControls();
}
private void initControls()
{
amount1=(EditText)findViewById(R.id.amount1);
amount2=(EditText)findViewById(R.id.amount2);
tt=(TextView)findViewById(R.id.tt);
calculate=(Button)findViewById(R.id.calculate);
calculate.setOnClickListener(new Button.OnClickListener()
{public void onClick
(View v) { calculate();}});
}
private void calculate()
{
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=x+y;
tt.setText(Double.toString(z));
}
}
I was trying to use as a reference, http://www.techillumination.in/2010/02/simple-android-application-for-adding.html , but it didn't work. So even after I copied and pasted it, changing the activity names, it still didn't work. I am learning so I am lost. It is just suppose to take 2 numbers and add them together. Thanks for any help.
Edit: Here is the logcat:
04-06 19:35:50.286: V/Activity(4144): onPause com.example.welderhelper.MainActivity@4204ef40
04-06 19:35:50.306: V/Activity(4144): onCreate com.example.welderhelper.Calculators@42346760: null
04-06 19:35:50.336: D/AndroidRuntime(4144): Shutting down VM
04-06 19:35:50.336: W/dalvikvm(4144): threadid=1: thread exiting with uncaught exception (group=0x41dd0700)
04-06 19:35:50.336: E/AndroidRuntime(4144): FATAL EXCEPTION: main
04-06 19:35:50.336: E/AndroidRuntime(4144): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.welderhelper/com.example.welderhelper.Calculators}: java.lang.NullPointerException
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2340)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2390)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.access$600(ActivityThread.java:153)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1269)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.os.Looper.loop(Looper.java:137)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.main(ActivityThread.java:5290)
04-06 19:35:50.336: E/AndroidRuntime(4144): at java.lang.reflect.Method.invokeNative(Native Method)
04-06 19:35:50.336: E/AndroidRuntime(4144): at java.lang.reflect.Method.invoke(Method.java:525)
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-06 19:35:50.336: E/AndroidRuntime(4144): at dalvik.system.NativeStart.main(Native Method)
04-06 19:35:50.336: E/AndroidRuntime(4144): Caused by: java.lang.NullPointerException
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.example.welderhelper.Calculators.initControls(Calculators.java:34)
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.example.welderhelper.Calculators.onCreate(Calculators.java:26)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.Activity.performCreate(Activity.java:5243)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
04-06 19:35:50.336: E/AndroidRuntime(4144): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2294)
04-06 19:35:50.336: E/AndroidRuntime(4144): ... 11 more
04-06 19:35:51.527: I/Process(4144): Sending signal. PID: 4144 SIG: 9
If you mean that your app crashes after pushing the Button labeled Calculate, it's probably because one of the parseDouble()
method calls is throwing a NumberFormatException
. Change your calculate()
method like so:
private void calculate()
{
try
{
x = Double.parseDouble(amount1.getText().toString());
y = Double.parseDouble(amount2.getText().toString());
z = x + y;
tt.setText(Double.toString(z));
}
catch (NumberFormatException e)
{
tt.setText("Invalid input");
}
}
From these two lines in your logcat
Caused by: java.lang.NullPointerException
04-06 19:35:50.336: E/AndroidRuntime(4144): at com.example.welderhelper.Calculators.initControls(Calculators.java:34)
something is null
on line 34 in initControls()
which appears to be this line
calculate.setOnClickListener(new Button.OnClickListener()
which means you don't have a Button
in activity_calculators.xml
with the id
of calculate
. It appears that the xml you have posted is for your first Activity
and not the one you have posted since none of those id
s from the java file exist in the xml you posted. The id
s you try to reference should be in activity_calculators.xml
.
Edit
Ok, I really need to pay more attention. Those id
s are in the xml you posted so either that is not activity_calculators.xml
or you need to clean and rebuild your project.
try changing
calculate.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
calculate();
}
});
to
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
calculate();
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.