gpt4 book ai didi

android - 不幸的是,App 已停止 Android 开发错误

转载 作者:行者123 更新时间:2023-12-03 16:53:59 27 4
gpt4 key购买 nike

当我尝试打开一个名为 areacircle 的新 Activity 时,它显示错误:“不幸的是,Essentials 已停止”。我 100% 确定问题不存在于 Manifest 文件中。我认为我的代码中可能存在一些问题。

编辑:我更新了圆形代码和 LogCat,程序仍然崩溃

areacircle xml:

<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="30sp"
android:text="@string/acirclefirstnumbtn"
android:textAppearance="?android:attr/textAppearanceLarge" />

<EditText
android:id="@+id/radiusedittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal"
android:text="@string/radius"
android:textSize="30sp" >

<requestFocus />
</EditText>

<Button
android:id="@+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="findarea"
android:text="@string/findarea"
android:textSize="50sp" />

<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30dp"
android:text="@string/area"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40sp" />

<TextView
android:id="@+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/areatextview"
android:textSize="50sp"
android:layout_gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />

区域圈代码:
package com.turnapps.essentials;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class areacircle extends Activity {

TextView textView1;
TextView textView2;
private static double radius;
Button button1;
EditText radiusedittext;
TextView answer;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.areacircle);
textView1 = (EditText) findViewById(R.id.textView1);
textView2 = (EditText) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
radiusedittext = (EditText) findViewById(R.id.radiusedittext);
answer = (EditText) findViewById(R.id.answer);

}
double pi = 3.14;
double radiusedittext1 = radius;
double areatextview;

public void findarea(View sender){


areatextview = radiusedittext1 * radiusedittext1 * pi;
answer.getText().equals(areatextview);
}
}

这是 findmy 的代码(我从中启动 areacircle 的 Activity ):
public class findmy extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.findmy);
Button d = (Button) findViewById(R.id.circleareabtn);
d.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent acircleIntent = new Intent(view.getContext(),
areacircle.class);
startActivity(acircleIntent);
}
});

}

}

这是 LogCat 内容:
08-27 14:14:57.731: E/Trace(2726): error opening trace file: No such file or directory  (2)
08-27 14:14:58.101: D/gralloc_goldfish(2726): Emulator without GPU emulation detected.
08-27 14:34:43.373: E/Trace(2838): error opening trace file: No such file or directory (2)
08-27 14:34:45.123: D/gralloc_goldfish(2838): Emulator without GPU emulation detected.
08-27 14:34:55.630: D/dalvikvm(2838): GC_CONCURRENT freed 166K, 11% free 2610K/2908K, paused 72ms+33ms, total 261ms
08-27 14:35:02.010: D/AndroidRuntime(2838): Shutting down VM
08-27 14:35:02.030: W/dalvikvm(2838): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-27 14:35:02.100: E/AndroidRuntime(2838): FATAL EXCEPTION: main
08-27 14:35:02.100: E/AndroidRuntime(2838): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.turnapps.essentials/com.turnapps.essentials.areacircle}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.os.Looper.loop(Looper.java:137)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-27 14:35:02.100: E/AndroidRuntime(2838): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:35:02.100: E/AndroidRuntime(2838): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 14:35:02.100: E/AndroidRuntime(2838): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-27 14:35:02.100: E/AndroidRuntime(2838): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-27 14:35:02.100: E/AndroidRuntime(2838): at dalvik.system.NativeStart.main(Native Method)
08-27 14:35:02.100: E/AndroidRuntime(2838): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-27 14:35:02.100: E/AndroidRuntime(2838): at com.turnapps.essentials.areacircle.onCreate(areacircle.java:21)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.Activity.performCreate(Activity.java:5104)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-27 14:35:02.100: E/AndroidRuntime(2838): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-27 14:35:02.100: E/AndroidRuntime(2838): ... 11 more
08-27 14:36:21.561: E/Trace(2872): error opening trace file: No such file or directory (2)
08-27 14:36:21.881: D/AndroidRuntime(2872): Shutting down VM
08-27 14:36:21.881: W/dalvikvm(2872): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-27 14:36:21.901: E/AndroidRuntime(2872): FATAL EXCEPTION: main
08-27 14:36:21.901: E/AndroidRuntime(2872): java.lang.RuntimeException: Unable to instantiate application android.app.Application: java.lang.NullPointerException
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.LoadedApk.makeApplication(LoadedApk.java:504)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4364)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.ActivityThread.access$1300(ActivityThread.java:141)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1294)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.os.Looper.loop(Looper.java:137)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-27 14:36:21.901: E/AndroidRuntime(2872): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:36:21.901: E/AndroidRuntime(2872): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 14:36:21.901: E/AndroidRuntime(2872): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-27 14:36:21.901: E/AndroidRuntime(2872): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-27 14:36:21.901: E/AndroidRuntime(2872): at dalvik.system.NativeStart.main(Native Method)
08-27 14:36:21.901: E/AndroidRuntime(2872): Caused by: java.lang.NullPointerException
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.LoadedApk.initializeJavaContextClassLoader(LoadedApk.java:379)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.LoadedApk.getClassLoader(LoadedApk.java:322)
08-27 14:36:21.901: E/AndroidRuntime(2872): at android.app.LoadedApk.makeApplication(LoadedApk.java:496)
08-27 14:36:21.901: E/AndroidRuntime(2872): ... 11 more
08-27 14:36:32.331: E/Trace(2903): error opening trace file: No such file or directory (2)
08-27 14:36:34.631: D/gralloc_goldfish(2903): Emulator without GPU emulation detected.
08-27 14:36:37.820: D/dalvikvm(2903): GC_CONCURRENT freed 170K, 11% free 2604K/2904K, paused 75ms+94ms, total 309ms
08-27 14:36:45.061: D/AndroidRuntime(2903): Shutting down VM
08-27 14:36:45.071: W/dalvikvm(2903): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
08-27 14:36:45.140: E/AndroidRuntime(2903): FATAL EXCEPTION: main
08-27 14:36:45.140: E/AndroidRuntime(2903): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.turnapps.essentials/com.turnapps.essentials.areacircle}: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.ActivityThread.access$600(ActivityThread.java:141)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.os.Looper.loop(Looper.java:137)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.ActivityThread.main(ActivityThread.java:5041)
08-27 14:36:45.140: E/AndroidRuntime(2903): at java.lang.reflect.Method.invokeNative(Native Method)
08-27 14:36:45.140: E/AndroidRuntime(2903): at java.lang.reflect.Method.invoke(Method.java:511)
08-27 14:36:45.140: E/AndroidRuntime(2903): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
08-27 14:36:45.140: E/AndroidRuntime(2903): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
08-27 14:36:45.140: E/AndroidRuntime(2903): at dalvik.system.NativeStart.main(Native Method)
08-27 14:36:45.140: E/AndroidRuntime(2903): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
08-27 14:36:45.140: E/AndroidRuntime(2903): at com.turnapps.essentials.areacircle.onCreate(areacircle.java:21)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.Activity.performCreate(Activity.java:5104)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-27 14:36:45.140: E/AndroidRuntime(2903): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
08-27 14:36:45.140: E/AndroidRuntime(2903): ... 11 more

最佳答案

初始化 onCreate 中的 View 在 setContentView 之后.您需要先将内容设置为 Activity ,然后再初始化 View 。您的初始化失败导致 NullPointerException .

TextView textView1;
TextView textView2;
private static double radius;
Button button1;
EditText radiusedittext;
TextView answer;
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.areacircle);
textView1 = (EditText) findViewById(R.id.textView1);
textView2 = (EditText) findViewById(R.id.textView2);
button1 = (Button) findViewById(R.id.button1);
radiusedittext = (EditText) findViewById(R.id.radiusedittext);
answer = (EditText) findViewById(R.id.answer);
}

编辑

你有这个
   <TextView
android:id="@+id/answer"

还有这个
  TextView answer;

但是你做
   answer = (EditText) findViewById(R.id.answer); // wrong casting to edittext

所以用下面的语句替换上面的语句
   answer = (TextView) findViewById(R.id.answer);

关于android - 不幸的是,App 已停止 Android 开发错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18468047/

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