2012-03-11 78 views
1

制作一个简单的数据库应用程序,该应用程序存储带有过期日期的食物项目。 无法设置数据库,我终于认为我已经对它进行了排序。然后我在开始用于添加项目的活动时忽略应用程序崩溃。java.lang.ClassCastException当在应用程序中启动活动时

这里的logcat的:

03-11 09:37:00.406:E/AndroidRuntime(410):致命异常:主03-11 09:37:00.406:E/AndroidRuntime(410) :java.lang.RuntimeException: 无法启动活动 ComponentInfo {grocery.a2.app/grocery.a2.app.AddItems}: java.lang.ClassCastException:grocery.a2.app.AddItems 03-11 09: 37:00.406:E/AndroidRuntime(410):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 03-11 09:37:00.406:E/AndroidRuntime(410):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 03-11 09:37:00.406:E/AndroidRuntime(410):at android.app.ActivityThread.access $ 1500(ActivityThread.java:117)03 -11 09:37:00.406:E/AndroidRuntime(410):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931) 03-11 09:37:00.406:E/AndroidRuntime(410) :at android.os.Handler.dispatchMessage(Handler.java:99)03-11 09:37:00.406:E/AndroidRuntime(410):at android.os.Looper.loop(Looper.java:123) 03-11 09:37:00.406: E/AndroidRuntime(410):at android.app.ActivityThread.main(ActivityThread.java:3683)03-11 09:37:00.406:E/AndroidRuntime(410)在 java.lang.reflect.Method.invokeNative(Native Method)03-11 09:37:00.406:E/AndroidRuntime(410):at java.lang.reflect.Method.invoke(Method.java:507)03 -11 09:37:00.406: E/AndroidRuntime(410):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839) 03-11 09:37:00.406:E/AndroidRuntime(410):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)03-11 09:37:00.406:E/AndroidRuntime(410):at dalvik.system.NativeStart。 main(Native Method)03-11 09:37:00.406: E/AndroidRuntime(410):引起:java.lang.ClassCastException: grocery.a2.app.AddItems 03-11 09:37:00.406:E/AndroidRuntime(410):在E/AndroidRuntime(410):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)0317 09:37:00.406: E/AndroidRuntime(410):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) (410): 03-11 09:37:00.406:E/AndroidRuntime(410):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 03-11 09:37:00.406:E/AndroidRuntime ... 11多个

而且活动未能启动代码:

public class AddItems extends Activity { 

    private EditText ItemName; 
    private Long mRowId; 

    Button btnAdd, btnView; 
    EditText txtName, txtDate; 

    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.addnewitem); 

     btnAdd = (Button) findViewById(R.id.btnAdd); 
     txtName = (EditText) findViewById(R.id.txtName); 
     txtDate = (EditText) findViewById(R.id.txtDate); 
     btnView = (Button) findViewById(R.id.btnScan); 

     btnView.setOnClickListener((OnClickListener) this); 
     btnAdd.setOnClickListener((OnClickListener) this); 

     mRowId = null; 

     Bundle extras = getIntent().getExtras(); 

     if (extras != null){ 
      String name = extras.getString(ItemDBAdapter.KEY_ITEMNAME); 
      String date = extras.getString(ItemDBAdapter.KEY_ITEMEXP); 
      mRowId = extras.getLong(ItemDBAdapter.KEY_ID); 
      if (name != null){ 
       ItemName.setText(name); 
      } 
     } 

     btnAdd.setOnClickListener(new OnClickListener(){ 
      public void onClick(View view) { 
       Bundle bundle = new Bundle(); 
       bundle.putString(ItemDBAdapter.KEY_ITEMNAME,ItemName.getText().toString()); 
       if (mRowId != null){ 
        bundle.putLong(ItemDBAdapter.KEY_ID, mRowId); 
       } 
       Intent mIntent = new Intent(); 
       mIntent.putExtras(bundle); 
       setResult(RESULT_OK, mIntent); 
       finish(); 
      } 
     }); 
    } 

而且布局XML:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical"> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Item Name" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_marginTop="12dp" 
     android:textColor="#999999" 
     /> 
     <requestFocus /> 

     <EditText 
      android:id="@+id/txtName" 
      android:layout_width="fill_parent" 
      android:layout_height="wrap_content" 
      android:inputType="textCapSentences" android:layout_marginTop="5dp"/> 


    <Button 
     android:id="@+id/btnScan" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center" 
     android:layout_marginTop="12dp" 
     android:text="Scan Barcode" 
     android:textColor="#000000" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Expiration Date" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:layout_marginTop="12dp" 
     android:textColor="#999999"/> 

    <EditText 
     android:id="@+id/txtDate" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:inputType="date" /> 

    <Button 
     android:id="@+id/btnAdd" 
     android:layout_width="100dp" 
     android:layout_height="wrap_content" 
     android:layout_gravity="center_horizontal" 
     android:text="Add Item" 
     android:layout_marginTop="12dp" 
     android:textColor="#000000"/> 

    <Button 
     android:id="@+id/btnBack" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_gravity="bottom|right" 
     android:layout_marginRight="10dp" 
     android:text="@string/back"/>" 

</LinearLayout> 

任何帮助将不胜感激。

+0

您可以发布布局XML吗? – MByD 2012-03-11 09:53:08

+0

我已经在布局xml中添加了。 – Lundi 2012-03-11 09:57:46

回答

2

如果要使用此:

btnView.setOnClickListener((OnClickListener) this); 
btnAdd.setOnClickListener((OnClickListener) this); 

你的活动必须实现OnClickListener

+0

啊,那是问题所在。非常感谢您的帮助。 – Lundi 2012-03-11 10:02:16

0
您的logcat的

仔细阅读告诉我,你的活动类的第28行试图 投东西,无论不是这样。如果不计算行数,我猜想它是 setOnClickListener(),并且您的活动没有实现适当的接口。

0

好的,我已经看到过这个问题。我已将这项活动添加到清单中。你添加了吗?

+0

是的活动是在清单中。 – Lundi 2012-03-11 10:00:11

0

有可能是线处的问题:

btnView.setOnClickListener((OnClickListener) this); 
btnAdd.setOnClickListener((OnClickListener) this); 

因为你已经实现OnClickListener为按钮btnAdd:

btnAdd.setOnClickListener(new OnClickListener(){ 
      public void onClick(View view) { 
      ...... 
     }); 

所以移除上面代码中的2条线,它将为你工作。

如果你仍然想使用第1种方法,那么你的业务必须要实现OnClickListener。

0

尝试这一个:

公共类为addItems延伸活动实现View.OnClickListener {

private EditText ItemName; 
private Long mRowId; 

Button btnAdd, btnView; 
EditText txtName, txtDate; 

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.addnewitem); 

    btnAdd = (Button) findViewById(R.id.btnAdd); 
    txtName = (EditText) findViewById(R.id.txtName); 
    txtDate = (EditText) findViewById(R.id.txtDate); 
    btnView = (Button) findViewById(R.id.btnScan); 

    btnView.setOnClickListener(this); 
    btnAdd.setOnClickListener(this); 

}

@Override 

公共无效的onClick(查看视图){

switch (view.getId()) { 
    case R.id.btnAdd: 
     //Do something 
     break; 

    case R.id.btnScan: 
    //Do something 
    break; 
    } 

}

}

相关问题