This is the class causing trouble because before writing this class, the program ran fine on the phone.
So I think the problem is with this class.
Please look in to find the problem.
JAVA CLASS:
package com.boston.ppp.boston;
import java.util.Random;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.text.InputType;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Switch;
import android.widget.TextView;
import android.widget.ToggleButton;
/**
* Created by ppp on 4/19/2015.
*/
public class TextPlay extends Activity implements View.OnClickListener {
Button b;
ToggleButton tog;
EditText ed;
TextView tex;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.text);
arnavbhai();
tog.setOnClickListener(this);
b.setOnClickListener(this);
}
private void arnavbhai() {
b = (Button) findViewById(R.id.button1);
tog = (ToggleButton) findViewById(R.id.togglebutton);
ed = (EditText) findViewById(R.id.etcommand);
tex = (TextView) findViewById(R.id.tvresults);
}
@Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button1:
String check = ed.getText().toString();
tex.setText(check);
if (check.contentEquals("left")) {
tex.setGravity(Gravity.LEFT);
}
if (check.contentEquals("right")) {
tex.setGravity(Gravity.RIGHT);
}
if (check.contentEquals("center")) {
tex.setGravity(Gravity.CENTER);
}
if (check.contains("WTF")) {
Random crazy = new Random();
tex.setText("WTF");
tex.setTextSize(crazy.nextInt(75));
tex.setTextColor(Color.rgb(crazy.nextInt(265),crazy.nextInt(265),crazy.nextInt(265)));
switch(crazy.nextInt(3))
{
case 0:
tex.setGravity(Gravity.LEFT);
break;
case 1:
tex.setGravity(Gravity.RIGHT);
break;
case 2:
tex.setGravity(Gravity.CENTER);
break;
}
}
else {
tex.setText("invalid");
tex.setGravity(Gravity.CENTER);
tex.setTextColor(Color.WHITE);
}
break;
case R.id.togglebutton:
if (tog.isChecked()) {
ed.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
} else {
ed.setInputType(InputType.TYPE_CLASS_TEXT);
}
break;
}
}
}
ERROR SHOWN :
04-21 18:18:02.157 8024-8024/com.boston.ppp.boston W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x41662d40)
04-21 18:18:02.161 8024-8024/com.boston.ppp.boston E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.boston.ppp.boston, PID: 8024
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.boston.ppp.boston/com.boston.ppp.boston.TextPlay}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.boston.ppp.boston.TextPlay.onCreate(TextPlay.java:32)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5086)
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:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
04-21 18:20:23.527 8024-8024/com.boston.ppp.boston I/Process﹕ Sending signal. PID: 8024 SIG: 9
The error is in the line 32.
You have to find your UI elements and define them in your code. Something like:
b = (Button) findViewById(R.id.ID_OF_BUTTON);
tog = (ToggleButton) findViewById(R.id.ID);
Then, you can call the listenners.
you are accessing tog
and b
without initialization which is causing a NPE
. add
tog = (ToggleButton) findViewById(R.id.idOfTOG);
b = (Button) findViewById(R.id.idOfButton);
in onCreate
before calling setOnClickListener
You have a NullPointerException being thrown from TextPlay.onCreate(), line 32. It's right there in the stack trace.
Now, you can either * Attach a debugger and step through the method, looking for null references, or * Trace the set-up of your class and see where the null reference could be coming from
well, based on your comment. I think the problems is because of this
Layout
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="New Button"
android:id="@+id/bresults"
/>
<ToggleButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="80"
android:checked="true"
android:paddingBottom="10dp"
android:id="@+id/togglebutton"
/>
Code
Button b;
ToggleButton tog;
@Override
protected void onCreate(Bundle savedInstanceState) {
arnavbhai();
tog.setOnClickListener(this);
b.setOnClickListener(this); <-- seems this part is throwing NPE
}
private void arnavbhai() {
b = (Button) findViewById(R.id.button1); <-- shouldn't it be R.id.bresults ?
tog = (ToggleButton) findViewById(R.id.togglebutton);
}
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.