开发者

my app crashes at the start and gives me a force close error

VitaminTable.java:

package vitaminTable.xyz.com;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;


public class VitaminTable extends Activity {
Button vitaminA,vitaminB1,vitaminB2,vitaminB3,vitaminB5,vitaminB6,vitaminB7,vitaminB9;
Button vitaminB12,vitaminC,vitaminD,vitaminE,vitaminK;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    vitaminA = (Button) findViewById(R.id.vitaminA);
    vitaminB1 = (Button) findViewById(R.id.vitaminB1);
    vitaminB2 = (Button) findViewById(R.id.vitaminB2);
    vitaminB3 = (Button) findViewById(R.id.vitaminB3);
    vitaminB5 = (Button) findViewById(R.id.vitaminB5);
    vitaminB6 = (Button) findViewById(R.id.vitaminB6);
    vitaminB7 = (Button) findViewById(R.id.vitaminB7);
    vitaminB9 = (Button) findViewById(R.id.vitaminB9);
    vitaminB12 = (Button) findViewById(R.id.vitaminB12);
    vitaminC = (Button) findViewById(R.id.vitaminC);
    vitaminD = (Button) findViewById(R.id.vitaminD);
    vitaminE = (Button) findViewById(R.id.vitaminE);
    vitaminK = (Button) findViewById(R.id.vitaminK);

    vitaminA.setOnClickListener((OnClickListener) this);
    vitaminB1.setOnClickListener((OnClickListener) this);
    vitaminB2.setOnClickListener((OnClickListener) this);
    vitaminB3.setOnClickListener((OnClickListener) this);
    vitaminB5.setOnClickListener((OnClickListener) this);
    vitaminB6.setOnClickListener((OnClickListener) this);
    vitaminB7.setOnClickListener((OnClickListener) this);
    vitaminB9.setOnClickListener((OnClickListener) this);
    vitaminB12.setOnClickListener((OnClickListener) this);
    vitaminC.setOnClickListener((OnClickListener) this);
    vitaminD.setOnClickListener((OnClickListener) this);
    vitaminE.setOnClickListener((OnClickListener) this);
    vitaminK.setOnClickListener((OnClickListener) this);
}


public void onClick(View v) {
    switch (v.getId()) {

    case R.id.vitaminA:
     Intent va = new Intent(VitaminTable.this,vita.class);
     startActivity(va);
    break;

   case R.id.vitaminB1:
      Intent vb1 = new Intent(VitaminTable.this,vitb1.class);
      startActivity(vb1);
    break;

   case R.id.vitaminB2:
      Intent vb2 = new Intent(VitaminTable.this,vitb2.class);
       startActivity(vb2);
     break;

   case R.id.vitaminB3:
      Intent vb3 = new Intent(VitaminTable.this,vitb3.class);
       startActivity(vb3);
     break;

   case R.id.vitaminB5:
      Intent vb5 = new Intent(VitaminTable.this,vitb5.class);
        startActivity(vb5);
      break;

   case R.id.vitaminB6:
      Intent vb6 = new Intent(VitaminTable.this,vitb6.class);
        startActivity(vb6);
      break;

   case R.id.vitaminB7:
      Intent vb7 = new Intent(VitaminTable.this,vitb7.class);
        startActivity(vb7);
      break;

   case R.id.vitaminB9:
      Intent vb9 = new Intent(VitaminTable.this,vitb9.class);
        startActivity(vb9);
      break;

   case R.id.vitaminB12:
      Intent vb12 = new Intent(VitaminTable.this,vitb12.class);
        startActivity(vb12);
      break;

  开发者_C百科 case R.id.vitaminC:
      Intent vc = new Intent(VitaminTable.this,vitc.class);
        startActivity(vc);
      break;

   case R.id.vitaminD:
      Intent vd = new Intent(VitaminTable.this,vitd.class);
        startActivity(vd);
      break;

   case R.id.vitaminE:
      Intent ve = new Intent(VitaminTable.this,vite.class);
        startActivity(ve);
      break;

   case R.id.vitaminK:
      Intent vk = new Intent(VitaminTable.this,vitk.class);
        startActivity(vk);
      break;   
  }
 }
} 

manifest file:

<?xml version="1.0" encoding="utf-8"?>
     <manifest xmlns:android="http://schemas.android.com/apk/res/android"
      package="vitaminTable.xyz.com"
    android:versionCode="1"
      android:versionName="1.0">
     <application android:icon="@drawable/icon" android:label="@string/app_name">
       <activity android:name=".VitaminTable"
              android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
   <activity android:name=".vita"/>
    <activity android:name=".vitb1" />
    <activity android:name=".vitb2" />
    <activity android:name=".vitb3" />
    <activity android:name=".vitb5" />
    <activity android:name=".vitb6" />
    <activity android:name=".vitb7" />
    <activity android:name=".vitb9" />
    <activity android:name=".vitb12" />
    <activity android:name=".vitc" />
    <activity android:name=".vitd" />
    <activity android:name=".vite" />
    <activity android:name=".vitk" />
    <activity android:name=".vitadailyreq" />

   </application>


</manifest>  

main.xml:

 <?xml version="1.0" encoding="utf-8"?>


  <ScrollView
 android:id="@+id/Vitamintable"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 xmlns:android="http://schemas.android.com/apk/res/android"  
 >

 <TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:stretchColumns="1"
 android:background="@drawable/fruit2">

  <TableRow>
 <Button   
 android:id="@+id/vitaminA"
 android:layout_width="240px"
 android:layout_height="36px"
 android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin A"
  android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
  <Button   
 android:id="@+id/vitaminB1"
 android:layout_width="240px"
  android:layout_height="36px"
 android:textSize="15sp"
 android:background="#FFFFFF"
  android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin B1"
 android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminB2"
 android:layout_width="240px"
 android:layout_height="36px"
 android:textSize="15sp"
 android:background="#FFFFFF"
  android:textColor="#D15FEE"
 android:textStyle="bold"
  android:gravity="left"
  android:text="Vitamin B2"
  android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
  <Button   
 android:id="@+id/vitaminB3"
 android:layout_width="240px"
 android:layout_height="36px"
 android:textSize="15sp"
  android:background="#FFFFFF"
  android:textColor="#D15FEE"
  android:textStyle="bold"
  android:gravity="left"
 android:text="Vitamin B3"
 android:fadingEdge="horizontal|vertical"/>

  </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminB5"
 android:layout_width="240px"
 android:layout_height="36px"
  android:textSize="15sp"
  android:background="#FFFFFF"
   android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin B5"
 android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminB6"
  android:layout_width="240px"
 android:layout_height="36px"
  android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin B6"
  android:fadingEdge="horizontal|vertical"/>

 </TableRow>
     <TableRow>
 <Button   
 android:id="@+id/vitaminB7"
 android:layout_width="240px"
 android:layout_height="36px"
  android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin B7" 
 android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
 <Button   
android:id="@+id/vitaminB9"
 android:layout_width="240px"
 android:layout_height="36px"
 android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
  android:text="Vitamin B9"
  android:fadingEdge="horizontal|vertical"/>

  </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminB12"
 android:layout_width="240px"
 android:layout_height="36px"
 android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin B12"
 android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminC"
 android:layout_width="240px"
 android:layout_height="36px"
  android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin C"
  android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminD"
 android:layout_width="240px"
 android:layout_height="36px"
 android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin D"
  android:fadingEdge="horizontal|vertical"/>

  </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminE"
 android:layout_width="240px"
 android:layout_height="36px"
 android:textSize="15sp"
 android:background="#FFFFFF"
 android:textColor="#D15FEE"
 android:textStyle="bold"
 android:gravity="left"
 android:text="Vitamin E"
 android:fadingEdge="horizontal|vertical"/>

 </TableRow>
    <TableRow>
 <Button   
 android:id="@+id/vitaminK"
  android:layout_width="240px"
   android:layout_height="36px"
   android:textSize="15sp"
  android:background="#FFFFFF"
  android:textColor="#D15FEE"
  android:textStyle="bold"
  android:gravity="left"
  android:text="Vitamin K"
   android:fadingEdge="horizontal|vertical"/>

  </TableRow>

  </TableLayout>
  </ScrollView>

 i get the following error:
 11-18 22:09:51.765: ERROR/AndroidRuntime(488): FATAL EXCEPTION: main
 11-18 22:09:51.765: ERROR/AndroidRuntime(488): java.lang.RuntimeException: Unable to start activity ComponentInfo{vitaminTable.xyz.com/vitaminTable.xyz.com.VitaminTable}: java.lang.ClassCastException: vitaminTable.xyz.com.VitaminTable
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.os.Handler.dispatchMessage(Handler.java:99)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.os.Looper.loop(Looper.java:123)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.app.ActivityThread.main(ActivityThread.java:4627)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at java.lang.reflect.Method.invokeNative(Native Method)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at java.lang.reflect.Method.invoke(Method.java:521)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at dalvik.system.NativeStart.main(Native Method)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488): Caused by: java.lang.ClassCastException: vitaminTable.xyz.com.VitaminTable
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at vitaminTable.xyz.com.VitaminTable.onCreate(VitaminTable.java:38)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
 11-18 22:09:51.765: ERROR/AndroidRuntime(488):     ... 11 more


your problem is written in the log

ClassCastException

at this location

onCreate(VitaminTable.java:38)

so on line 38 your casting your view into the wrong type

sorry but can't help you more without your layout xml file but check the type of view you created and that should do the trick


Well, i don't know exactly, but as far as i guess, try changing all lines similar to :

vitaminA.setOnClickListener((OnClickListener) this); to something like this

vitaminA.setOnClickListener(this); ,

and let your Activity implement onCLickListener, and add unimplemented methods, just follow the suggestions that eclipse will provide to you..

Try this may be it may help you..........


You have the packagename backwards. It should be "com.xyz.vitaminTable" or probably better "com.xyz.vitamin". But that should not be a problem unless you use it different in the Manifest. Please provide the contents of the AndroidManifest.xml.

In order to cast this to OnClickListener VitaminTable hast to implement OnClickListener.

public class VitaminTable extends Activity implements OnClickListener {
  ...
  implement OnClickListener-Methods here
  ...
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜