gpt4 book ai didi

java - 我的应用程序在开始时崩溃并给我一个强制关闭错误

转载 作者:行者123 更新时间:2023-12-01 15:59:57 25 4
gpt4 key购买 nike

维生素表.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;

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

list 文件:

<?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

最佳答案

你的问题已写在日志中

类转换异常

在此位置

onCreate(VitaminTable.java:38)

所以在第 38 行,你将 View 转换到了错误的类型

抱歉,如果没有布局 xml 文件,无法为您提供更多帮助,但请检查您创建的 View 类型,这应该可以解决问题

关于java - 我的应用程序在开始时崩溃并给我一个强制关闭错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4222577/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com