我试图打开按钮点击“类别活动”的活动正在打开但是学习活动没有打开。请我开展学习活动。提前谢谢
{
// 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" />
学习活动
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>
表生成器活动
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)
}
答案 0 :(得分:1)
您应该在AndroidManifest.xml
文件中声明任何活动,然后才能启动它。在</application>
添加此内容之前:
<activity android:name=".Study"/>
答案 1 :(得分:1)
在Actvity
内声明学习AndroidManifest.xml
,您的LogCat消息指示异常:
引起:android.content。 ActivityNotFoundException :无法找到显式活动类{com.itcheeta.englishverformsmaster / com.itcheeta.englishverformsmaster.Study}; 您是否在AndroidManifest.xml中声明了此活动?
示例:
<activity
android:name=".Study"
android:label="Study" >