Lundi
Lundi

Reputation: 11

java.lang.ClassCastException While starting activity in app

Making a simple Database app that stores Food Items with their expiration dates. Having trouble setting up the database, I finally think I've sorted it. And then I notoce the app crashes while start the activity used to add items.

Here's the Logcat:

03-11 09:37:00.406: E/AndroidRuntime(410): FATAL EXCEPTION: main 03-11 09:37:00.406: E/AndroidRuntime(410): java.lang.RuntimeException: Unable to start activity 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): at 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): Caused by: java.lang.ClassCastException: grocery.a2.app.AddItems 03-11 09:37:00.406: E/AndroidRuntime(410): at grocery.a2.app.AddItems.onCreate(AddItems.java:28) 03-11 09:37:00.406: E/AndroidRuntime(410): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 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(410): ... 11 more

And the code of the activity that fails to start:

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();
            }
        });
    }

And the layout 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>

Any help will be greatly appreciated.

Upvotes: 1

Views: 270

Answers (5)

Aki143S
Aki143S

Reputation: 819

Try this one:

public class AddItems extends Activity implements 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 

public void onClick(View view) {

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

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

}

}

Upvotes: 0

Paresh Mayani
Paresh Mayani

Reputation: 128428

There may be a problem at lines:

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

Because you have already implemented OnClickListener for the button btnAdd:

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

So remove the above 2 lines from the code, and it will work for you.

And if you still want to use that 1st method, then your activity must have to implements OnClickListener.

Upvotes: 0

Arpit Patel
Arpit Patel

Reputation: 1571

ok i have seen this problem some times. I have add the activity in the menifest. HAVE YOU ADDED THAT?

Upvotes: 0

Konstantin Pribluda
Konstantin Pribluda

Reputation: 12367

Careful reading of your logcat tells me that on line 28 of your activity class you try to cast something to whatever it is not. Without counting lines, I would guess that it was setOnClickListener() , and your activity does not implement proper interface.

Upvotes: 0

Zoon Nooz
Zoon Nooz

Reputation: 5866

If you want to use this:

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

Your activity must implement OnclickListener

Upvotes: 2

Related Questions