gpt4 book ai didi

android - 某些 Activity 未打开 : java. lang.IllegalStateException:无法执行 Activity 的方法

转载 作者:太空狗 更新时间:2023-10-29 13:16:28 25 4
gpt4 key购买 nike

我正在尝试通过单击按钮打开 Activity “类别 Activity ”正在打开,但学习 Activity 未打开。请我打开学习 Activity 。提前致谢

{
// Main Activity


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;


public class MainActivity extends Activity {



@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}


public void categories (View categories)
{
Intent intent = new Intent(this, Study.class);
startActivity(intent);

}

public void study (View view)
{
Intent intent = new Intent(this, Study.class);
startActivity(intent);

}
}
}


Main xml



<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.itcheeta.englishverformsmaster.MainActivity" >

<Button
android:id="@+id/categories"
android:layout_width="match_parent"
android:layout_height="50dp"
android:text="Categories"
android:onClick="categories" />

<Button
android:id="@+id/study"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/categories"
android:layout_centerHorizontal="true"
android:layout_marginTop="52dp"
android:text="study"
android:onClick="study" />

学习 Activity

import android.app.Activity;
import android.os.Bundle;
import android.widget.ScrollView;

public class Study extends Activity {
private ScrollView layMain;
private TableGenerator mTable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_study);
showTable();
}

private void showTable() {
mTable = new TableGenerator(getApplicationContext());
layMain = (ScrollView)findViewById(R.id.table);

String[] firstRow = {"Present", "Past", "Future" };
String[] secondRow = {"Do", "Did", "Done" };
String[] thridRow = {"Come", "Came", "Come"};
String[] fourthRow = {"Go", "Went", "Gone"};

mTable.addRow(firstRow);
mTable.addRow(secondRow);
mTable.addRow(thridRow);
mTable.addRow(fourthRow);

layMain.removeAllViews();
layMain.addView(mTable.getTable());
}


}

学习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" >

<ScrollView
android:id="@+id/table"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</ScrollView>

</LinearLayout>

表生成器 Activity

import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TextView;


public class TableGenerator {
private final Context mContext;
private TableLayout mTable;

private TableLayout.LayoutParams rowParams = new TableLayout.LayoutParams();
private TableRow.LayoutParams colParams = new TableRow.LayoutParams();

public TableGenerator(Context context) {
mContext = context;
mTable = new TableLayout(context);
rowParams.setMargins(0, 0, 0, 1);
colParams.setMargins(0, 0, 1, 0);

TableLayout.LayoutParams lptable = new TableLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
mTable.setLayoutParams(lptable);

mTable.setStretchAllColumns(true);
mTable.setBackgroundColor(mContext.getResources().getColor(
R.color.table_background));
}

public void addRow(String[] data) {
TableRow tr = new TableRow(mContext);
tr.setBackgroundColor(mContext.getResources().getColor(
R.color.table_background));

tr.setLayoutParams(rowParams);

for (int iCol = 0; iCol < data.length; iCol++) {
TextView tvCol = new TextView(mContext);
tvCol.setText(data[iCol]);
tvCol.setGravity(Gravity.CENTER | Gravity.CENTER);
tvCol.setPadding(3, 3, 3, 3);
tvCol.setTextColor(mContext.getResources().getColor(
R.color.text_black));
tvCol.setLayoutParams(colParams);
tvCol.setBackgroundColor(mContext.getResources().getColor(
R.color.row_background));
tr.addView(tvCol);
}

mTable.addView(tr);
}

public TableLayout getTable() {
return mTable;
}
}

LogCat 消息:

{
12-12 02:29:37.191: E/AndroidRuntime(24473): FATAL EXCEPTION: main
12-12 02:29:37.191: E/AndroidRuntime(24473): java.lang.IllegalStateException: Could not execute method of the activity
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.view.View$1.onClick(View.java:3617)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.view.View.performClick(View.java:4222)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.view.View$PerformClick.run(View.java:17620)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.os.Handler.handleCallback(Handler.java:800)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.os.Handler.dispatchMessage(Handler.java:100)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.os.Looper.loop(Looper.java:194)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.app.ActivityThread.main(ActivityThread.java:5391)
12-12 02:29:37.191: E/AndroidRuntime(24473): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 02:29:37.191: E/AndroidRuntime(24473): at java.lang.reflect.Method.invoke(Method.java:525)
12-12 02:29:37.191: E/AndroidRuntime(24473): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
12-12 02:29:37.191: E/AndroidRuntime(24473): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
12-12 02:29:37.191: E/AndroidRuntime(24473): at dalvik.system.NativeStart.main(Native Method)
12-12 02:29:37.191: E/AndroidRuntime(24473): Caused by: java.lang.reflect.InvocationTargetException
12-12 02:29:37.191: E/AndroidRuntime(24473): at java.lang.reflect.Method.invokeNative(Native Method)
12-12 02:29:37.191: E/AndroidRuntime(24473): at java.lang.reflect.Method.invoke(Method.java:525)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.view.View$1.onClick(View.java:3612)
12-12 02:29:37.191: E/AndroidRuntime(24473): ... 11 more
12-12 02:29:37.191: E/AndroidRuntime(24473): Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.itcheeta.englishverformsmaster/com.itcheeta.englishverformsmaster.Study}; have you declared this activity in your AndroidManifest.xml?
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1693)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1492)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.app.Activity.startActivityForResult(Activity.java:3388)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.app.Activity.startActivityForResult(Activity.java:3349)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.app.Activity.startActivity(Activity.java:3584)
12-12 02:29:37.191: E/AndroidRuntime(24473): at android.app.Activity.startActivity(Activity.java:3552)
12-12 02:29:37.191: E/AndroidRuntime(24473): at com.itcheeta.englishverformsmaster.MainActivity.study(MainActivity.java:32)
}

最佳答案

在您的 AndroidManifest.xml 中声明 Study Actvity,您的 LogCat 消息指示异常:

Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.itcheeta.englishverformsmaster/com.itcheeta.englishverformsmaster.Study}; have you declared this activity in your AndroidManifest.xml?

例子:

 <activity      
android:name=".Study"
android:label="Study" >

关于android - 某些 Activity 未打开 : java. lang.IllegalStateException:无法执行 Activity 的方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34233132/

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