0

. - List item

I am doing an application to add and delete the datails of employees to and from database. when i run it ,i am getting the message"unfortunately my app has stopped". pls help me to solve this problem. my logcat shows the following details:

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

*code of NICemployeeActivity.java is as following:*

 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

4 回答 4

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 行。您正在将类型的对象转换NICemployeeActivityView,但您不能。

于 2012-05-21T09:10:38.540 回答
1

尝试导入

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

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

于 2013-06-25T17:09:00.023 回答
0

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

然后它将成功运行。

于 2013-04-23T10:28:09.940 回答
-1

You need to add activity to manifest.xml file while you are creating new activity.

于 2012-05-21T09:16:57.457 回答