java - 这个Java类编译顺利,但是当我使用我的Android手机作为ADB时单击它。它说设备已停止工作

标签 java android eclipse android-activity android-studio

这个类会造成麻烦,因为在编写这个类之前,程序在手机上运行良好。
所以我认为问题出在这个类上。
请查看以查找问题。

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

然后,您可以调用监听器。

关于java - 这个Java类编译顺利,但是当我使用我的Android手机作为ADB时单击它。它说设备已停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29773791/

相关文章:

java - spring-shell 和 CRaSH 有什么区别?

java - Eclipse PDT 插件给我带来了麻烦

java - 在 Android O 的主屏幕上创建快捷方式

android - 我无法在 Eclipse 中将 C/C++ Nature 添加到 Android 项目

java - 在 Eclipse 中找不到 Maven 选项

java - 带有 Kotlin 插件的 Android Studio 1.5 在调试器模式下出现异常。如何修复它?

java - Java Iterable 的优点和缺点

java - 重新创建springrabbitCachingConnectionFactory的所有连接

Android启动画面不全屏显示图像

android - WebView 不像浏览器那样渲染 css?