gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 04:44:33 24 4
gpt4 key购买 nike

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

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com