当我尝试打开一个名为areacircle的新活动时,它显示错误:“不幸的是,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" />
areacircle代码:
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的代码(我发起的活动是来自的):
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
答案 0 :(得分:1)
在onCreate
之后setContentView
初始化视图。您需要先将内容设置为活动,然后初始化视图。您的初始化失败,导致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);
答案 1 :(得分:0)
检查:
com.turnapps.essentials.areacircle。(areacircle.java:11)
似乎是罪魁祸首。
修改强>
在您的类的第21行,您将EditText对象分配给您声明为TextView变量的对象。这是两种不同类型的观点,你想做什么?有EditTexts还是TextViews?
另外,作为指导原则,通常的做法是让类名以大写字母开头:areacircle将是Areacircle或AreaCircle。这有助于您的代码的可读性,并在寻求帮助时帮助其他人理解您的代码。