Android Eclipse 中的 ClassCastException
尝试运行我的应用程序时出现以下错误: 你能告诉我哪里错了吗:) 哦,还有,我怎样才能找出代码中的哪一行发生了错误?谢谢
02-03 08:01:31.159: ERROR/AndroidRuntime(281): FATAL EXCEPTION: main
02-03 08:01:31.159: ERROR/AndroidRuntime(281): java.lang.RuntimeException: Unable to instantiate application com.android.demo.notepad2.NoteEdit: java.lang.ClassCastException: com.android.demo.notepad2.NoteEdit
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread.access$3000(ActivityThread.java:125)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at java.lang.reflect.Method.invoke(Method.java:521)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): Caused by: java.lang.ClassCastException: com.android.demo.notepad2.NoteEdit
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.Instrumentation.newApplication(Instrumentation.java:957)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.Instrumentation.newApplication(Instrumentation.java:942)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): ... 11 more
这是我的 NoteEdit 类:
package com.android.demo.notepad2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class NoteEdit extends Activity {
EditText mTitleText;
EditText mBodyText;
Long mRowId;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.note_edit);
setTitle(R.string.edit_note);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
mRowId = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (title != null) {
mTitleText.setText(title);
}
if (body != null) {
mBodyText.setText(body);
}
}
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText()
.toString());
bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText()
.toString());
if (mRowId != null) {
bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}
});
}
}
这是 NoteEdit 类布局:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<EditText
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/body" />
<EditText
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<Button
android:id="@+id/confirm"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I am getting the following errors when trying to run my application:
Can you please tell me where I am going wrong :)
Oh and also, how can I find out which line in my code the error is occurring? thanks
02-03 08:01:31.159: ERROR/AndroidRuntime(281): FATAL EXCEPTION: main
02-03 08:01:31.159: ERROR/AndroidRuntime(281): java.lang.RuntimeException: Unable to instantiate application com.android.demo.notepad2.NoteEdit: java.lang.ClassCastException: com.android.demo.notepad2.NoteEdit
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:649)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread.handleBindApplication(ActivityThread.java:4232)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread.access$3000(ActivityThread.java:125)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2071)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.os.Handler.dispatchMessage(Handler.java:99)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.os.Looper.loop(Looper.java:123)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at java.lang.reflect.Method.invokeNative(Native Method)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at java.lang.reflect.Method.invoke(Method.java:521)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at dalvik.system.NativeStart.main(Native Method)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): Caused by: java.lang.ClassCastException: com.android.demo.notepad2.NoteEdit
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.Instrumentation.newApplication(Instrumentation.java:957)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.Instrumentation.newApplication(Instrumentation.java:942)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): at android.app.ActivityThread$PackageInfo.makeApplication(ActivityThread.java:644)
02-03 08:01:31.159: ERROR/AndroidRuntime(281): ... 11 more
This is my NoteEdit class:
package com.android.demo.notepad2;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class NoteEdit extends Activity {
EditText mTitleText;
EditText mBodyText;
Long mRowId;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.note_edit);
setTitle(R.string.edit_note);
mTitleText = (EditText) findViewById(R.id.title);
mBodyText = (EditText) findViewById(R.id.body);
Button confirmButton = (Button) findViewById(R.id.confirm);
mRowId = null;
Bundle extras = getIntent().getExtras();
if (extras != null) {
String title = extras.getString(NotesDbAdapter.KEY_TITLE);
String body = extras.getString(NotesDbAdapter.KEY_BODY);
mRowId = extras.getLong(NotesDbAdapter.KEY_ROWID);
if (title != null) {
mTitleText.setText(title);
}
if (body != null) {
mBodyText.setText(body);
}
}
confirmButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Bundle bundle = new Bundle();
bundle.putString(NotesDbAdapter.KEY_TITLE, mTitleText.getText()
.toString());
bundle.putString(NotesDbAdapter.KEY_BODY, mBodyText.getText()
.toString());
if (mRowId != null) {
bundle.putLong(NotesDbAdapter.KEY_ROWID, mRowId);
}
Intent mIntent = new Intent();
mIntent.putExtras(bundle);
setResult(RESULT_OK, mIntent);
finish();
}
});
}
}
This is the NoteEdit class layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/title" />
<EditText
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/body" />
<EditText
android:id="@+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<Button
android:id="@+id/confirm"
android:text="@string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您需要在清单中添加类似的内容(也就是说,如果它尚未出现在您的清单中)
您的代码似乎没有错误,所以也许这个可以解决您的问题。
I think you need to put something like this in your manifest(that is if it's not yet present in your manifest)
Your code doesn't seem to have an error so maybe this one can solve your problem.
如果您在代码中找不到错误,有时在您完成重大编辑后,Eclipse 中会发生这种情况。您所需要做的就是打开项目的 gen 文件夹。删除 R.java 文件。清理项目,你就可以开始了。
If you cannot find the error in your code, sometimes this happens in eclipse after you've done a major edit. All you need to do is open the gen folder of your project. Delete the R.java file. Clean up the project and you should be good to go.