繁体   English   中英

这个Java类的编译过程很顺利,但是当我将Android手机用作ADB时,单击该类。 表示设备已停止工作

[英]This Java class compiles smoothly but when I click on this when using my android phone as ADB. It says the device has stopped working

这是引起麻烦的类,因为在编写该类之前,该程序在电话上可以正常运行。
因此,我认为问题出在这堂课上。
请查找以查找问题。

JAVA类:

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

}

错误显示

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

错误在第32行。

您必须找到UI元素并在代码中定义它们。 就像是:

b = (Button) findViewById(R.id.ID_OF_BUTTON);
tog = (ToggleButton) findViewById(R.id.ID);

然后,您可以呼叫听众。

您正在访问togb而没有初始化,这会导致NPE

tog = (ToggleButton) findViewById(R.id.idOfTOG);
b = (Button)  findViewById(R.id.idOfButton);

在调用setOnClickListener之前在onCreate

您将从TextPlay.onCreate()的第32行抛出NullPointerException。它就在堆栈跟踪中。

现在,您可以*附加调试器并逐步执行该方法,查找空引用,或者*跟踪类的设置,并查看空引用可能来自何处

好吧,根据您的评论。 我认为问题是因为

布局

<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"
/>

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM