不幸的是我的应用程序已停止在Android模拟器?

时间:2012-05-21 09:07:19

标签: android

。 - 列出项目

我正在做一个应用程序来添加和删除员工与数据库之间的数据。 当我运行它时,我收到消息“不幸的是我的应用已停止”。请帮我解决这个问题。我的logcat显示以下详细信息:

05-21 14:11:45.799: E/AndroidRuntime(527): FATAL EXCEPTION: main
05-21 14:11:45.799: E/AndroidRuntime(527): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.aswathy.nicemployee/com.aswathy.nicemployee.NICemployeeActivity}: java.lang.ClassCastException: com.aswathy.nicemployee.NICemployeeActivity cannot be cast to android.view.View$OnClickListener
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.ActivityThread.access$600(ActivityThread.java:123)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.os.Looper.loop(Looper.java:137)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.ActivityThread.main(ActivityThread.java:4424)
05-21 14:11:45.799: E/AndroidRuntime(527):  at java.lang.reflect.Method.invokeNative(Native Method)
05-21 14:11:45.799: E/AndroidRuntime(527):  at java.lang.reflect.Method.invoke(Method.java:511)
05-21 14:11:45.799: E/AndroidRuntime(527):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-21 14:11:45.799: E/AndroidRuntime(527):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-21 14:11:45.799: E/AndroidRuntime(527):  at dalvik.system.NativeStart.main(Native Method)
05-21 14:11:45.799: E/AndroidRuntime(527): Caused by: java.lang.ClassCastException: com.aswathy.nicemployee.NICemployeeActivity cannot be cast to android.view.View$OnClickListener
05-21 14:11:45.799: E/AndroidRuntime(527):  at com.aswathy.nicemployee.NICemployeeActivity.onCreate(NICemployeeActivity.java:31)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.Activity.performCreate(Activity.java:4465)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
05-21 14:11:45.799: E/AndroidRuntime(527):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
05-21 14:11:45.799: E/AndroidRuntime(527):  ... 11 more

* NICemployeeActivity.java的代码如下: *

 package com.aswathy.nicemployee;
 import android.app.Activity;
 import android.app.Dialog;
 import android.content.DialogInterface;
 import android.content.DialogInterface.OnClickListener;
 import android.content.Intent;
 import android.os.Bundle;
 import android.view.View;
 import android.widget.Button;
 import android.widget.EditText;
 import android.widget.TextView;
 public class NICemployeeActivity extends Activity implements OnClickListener  {
 Button sqlUpdate, sqlView;
 EditText sqlName, sqlDepartment;

/** Called when the activity is first created. */
@Override
 protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      sqlUpdate = (Button) findViewById(R.id.bSQLUpdate);
      sqlName = (EditText) findViewById(R.id.etSQLName);
      sqlDepartment = (EditText) findViewById(R.id.etSQLDepartment);
      sqlView = (Button) findViewById(R.id.bSQLopenView);
      sqlView.setOnClickListener(this);
      sqlUpdate.setOnClickListener(this);
     }
 public void onClick(View arg0) {
    // TODO Auto-generated method stub
      switch (arg0.getId()) {
         case R.id.bSQLUpdate:
         boolean didItWork = true;
         try{
             String name = sqlName.getText().toString();
             String department = sqlDepartment.getText().toString();

             DBemployee entry = new DBemployee(NICemployeeActivity.this);
             entry.open();
             entry.createEntry(name, department);
             entry.close();
        }catch (Exception e)
    {
        didItWork = false;
    }finally{
         if (didItWork){
             Dialog d = new Dialog(this);
             d.setTitle("Heck Yea!");
             TextView tv = new TextView(this);
             tv.setText("sucess");
             d.setContentView(tv);
             d.show();
        }
    }
         break;
         case R.id.bSQLopenView:
         Intent i = new Intent("com.aswathy.nicemployee.NICview");
         startActivity(i);
         break; 
}

}

 public void onClick(DialogInterface dialog, int which) {
    // TODO Auto-generated method stub

}

}

4 个答案:

答案 0 :(得分:4)

看这里

05-21 14:11:45.799: E/AndroidRuntime(527): Caused by: java.lang.ClassCastException: com.aswathy.nicemployee.NICemployeeActivity cannot be cast to android.view.View$OnClickListener
05-21 14:11:45.799: E/AndroidRuntime(527):  at com.aswathy.nicemployee.NICemployeeActivity.onCreate(NICemployeeActivity.java:31)

现在打开您的NICemployeeActivity.java文件,然后检查第31行。您正在将NICemployeeActivity类型的对象投射到View,但您不能。

答案 1 :(得分:1)

尝试使用

导入

android.view.View.OnClickListener而不是导入android.content.DialogInterface;& import android.content.DialogInterface.OnClickListener;

5月以上进口可能有问题。

答案 2 :(得分:0)

您必须更改RAM大小,然后重新启动模拟器。

然后它会成功运作。

答案 3 :(得分:-1)

您需要在创建新活动时向manifest.xml文件添加活动。