I am starting to develop Android apps! I am starting my first one and ran into an early problem. What the app does is have two buttons which you can click on to add or subtract the main variable. All was going well until when I tested it on my Nexus 4 and it force closes when I hit a button. I looked at simiar questions and still cannot figure out my problem. Thanks!
Logcat:
04-16 10:42:48.528: D/libEGL(17421): loaded /system/lib/egl/libEGL_adreno200.so
04-16 10:42:48.538: D/libEGL(17421): loaded /system/lib/egl/libGLESv1_CM_adreno200.so
04-16 10:42:48.538: D/libEGL(17421): loaded /system/lib/egl/libGLESv2_adreno200.so
04-16 10:42:48.538: I/Adreno200-EGL(17421): <eglInitialize:269>: EGL 1.4 QUALCOMM build: Nondeterministic AU_full_mako_PARTNER-ANDROID/JB-MR1-DEV_CL2961380_release_AU (CL2961380)
04-16 10:42:48.538: I/Adreno200-EGL(17421): Build Date: 12/10/12 Mon
04-16 10:42:48.538: I/Adreno200-EGL(17421): Local Branch:
04-16 10:42:48.538: I/Adreno200-EGL(17421): Remote Branch: m/partner-android/jb-mr1-dev
04-16 10:42:48.538: I/Adreno200-EGL(17421): Local Patches: NONE
04-16 10:42:48.538: I/Adreno200-EGL(17421): Reconstruct Branch: NOTHING
04-16 10:42:48.568: D/OpenGLRenderer(17421): Enabling debug mode 0
04-16 10:42:52.592: D/AndroidRuntime(17421): Shutting down VM
04-16 10:42:52.592: W/dalvikvm(17421): threadid=1: thread exiting with uncaught exception (group=0x40bef930)
04-16 10:42:52.592: E/AndroidRuntime(17421): FATAL EXCEPTION: main
04-16 10:42:52.592: E/AndroidRuntime(17421): java.lang.NullPointerException
04-16 10:42:52.592: E/AndroidRuntime(17421): at com.frostbytedev.addsub.StartingPoint$2.onClick(StartingPoint.java:37)
04-16 10:42:52.592: E/AndroidRuntime(17421): at android.view.View.performClick(View.java:4204)
04-16 10:42:52.592: E/AndroidRuntime(17421): at android.view.View$PerformClick.run(View.java:17355)
04-16 10:42:52.592: E/AndroidRuntime(17421): at android.os.Handler.handleCallback(Handler.java:725)
04-16 10:42:52.592: E/AndroidRuntime(17421): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 10:42:52.592: E/AndroidRuntime(17421): at android.os.Looper.loop(Looper.java:137)
04-16 10:42:52.592: E/AndroidRuntime(17421): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 10:42:52.592: E/AndroidRuntime(17421): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 10:42:52.592: E/AndroidRuntime(17421): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 10:42:52.592: E/AndroidRuntime(17421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 10:42:52.592: E/AndroidRuntime(17421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 10:42:52.592: E/AndroidRuntime(17421): at dalvik.system.NativeStart.main(Native Method)
04-16 10:43:22.033: D/AndroidRuntime(17514): Shutting down VM
04-16 10:43:22.033: W/dalvikvm(17514): threadid=1: thread exiting with uncaught exception (group=0x40bef930)
04-16 10:43:22.033: E/AndroidRuntime(17514): FATAL EXCEPTION: main
04-16 10:43:22.033: E/AndroidRuntime(17514): java.lang.NullPointerException
04-16 10:43:22.033: E/AndroidRuntime(17514): at com.frostbytedev.addsub.StartingPoint$2.onClick(StartingPoint.java:37)
04-16 10:43:22.033: E/AndroidRuntime(17514): at android.view.View.performClick(View.java:4204)
04-16 10:43:22.033: E/AndroidRuntime(17514): at android.view.View$PerformClick.run(View.java:17355)
04-16 10:43:22.033: E/AndroidRuntime(17514): at android.os.Handler.handleCallback(Handler.java:725)
04-16 10:43:22.033: E/AndroidRuntime(17514): at android.os.Handler.dispatchMessage(Handler.java:92)
04-16 10:43:22.033: E/AndroidRuntime(17514): at android.os.Looper.loop(Looper.java:137)
04-16 10:43:22.033: E/AndroidRuntime(17514): at android.app.ActivityThread.main(ActivityThread.java:5041)
04-16 10:43:22.033: E/AndroidRuntime(17514): at java.lang.reflect.Method.invokeNative(Native Method)
04-16 10:43:22.033: E/AndroidRuntime(17514): at java.lang.reflect.Method.invoke(Method.java:511)
04-16 10:43:22.033: E/AndroidRuntime(17514): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-16 10:43:22.033: E/AndroidRuntime(17514): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-16 10:43:22.033: E/AndroidRuntime(17514): at dalvik.system.NativeStart.main(Native Method)
04-16 10:43:23.685: I/Process(17514): Sending signal. PID: 17514 SIG: 9
I also have my Activity and Class file.
Activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="top"
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=".StartingPoint" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Your total is 0"
android:textSize="45dp"
android:gravity="center"
android:id="@+id/tvDisplay"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Add One"
android:textSize="20dp"
android:id="@+id/bAdd" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Subtract One"
android:textSize="20dp"
android:id="@+id/bSub" />
</LinearLayout>
Finally, my Class file:
package com.frostbytedev.addsub;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class StartingPoint extends Activity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
counter = 0;
add=(Button) findViewById(R.id.bAdd);
sub=(Button) findViewById(R.id.bSub);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
counter++;
display.setText("Your total is "+counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter--;
display.setText("Your total is "+counter);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}
}
You need to find the id of textview.
android:id="@+id/tvDisplay"
display = TextView findViewById(R.id.tvDisplay);
You do not have statement as above in your code which is reason for NullPointerException.
Once you find the id as above set text to textview
display.setText("Your total is "+counter);
Explanation:
Every resource like strings, drawables and views like button textview will have a entry in R.java file like below
public static final int tvDisplay=0x7f080000;// value may differ
The value is int. So R.id.textview will will be integer value. You need to initialize textview by finding the id and then set text in TextView.
A more wide explanation on the error, so you won't open another question like this in a while.
Read the error:
java.lang.NullPointerException
04-16 10:42:52.592: E/AndroidRuntime(17421): at com.frostbytedev.addsub.StartingPoint$2.onClick(StartingPoint.java:37)
Basically there is something null
at that line (37).
Go to that line and look:
display.setText("Your total is "+counter);
You're calling setText(String text)
on display
. So the problem is there.
If you look before you haven't instanciated display
(that is null) and so you're calling a method on nothing. As others pointed out just get the View with findViewById, cast it to a TextView and save it on the display variable:
display = (TextView) findViewById(R.id.textview);
And learn how to debug , that's the key!
You need to assign a view ( tvDisplay
) to display
. Currently display
is only declared but not assigned to and therefore null .
you have not initialized display TextView
do this
diplay = (TextView) findVidewById(R.id.tvDisplay);
add
display=(TextView) findViewById(R.id.your id);
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.