我正在尝试使用Intent启动一个新活动,但我不断获得java.lang.IllegalStateException: Could not execute method of the activity
我做错了什么?
主要活动
package com.example.wrw;
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 clicked(View v)
{
Intent intent = new Intent(this, newact.class);
startActivity(intent);
}
}
newact.java
package com.example.wrw;
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 newact extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlayout);
}
}
act_main.xml
<LinearLayout 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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start Act"
android:onClick="clicked"
/>
</LinearLayout>
newlayout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.wrw"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
答案 0 :(得分:3)
在Android应用中,首先必须在Activity
文件中配置所有Manifest.xml
,因此您应该在清单中添加newact Activity
<activity android:name=".MainActivity"
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="com.example.wrw.newact"></activity>
答案 1 :(得分:0)
试试这个
Intent intent = new Intent(MainActivity.this, newact.class)
并在清单文件中注册newact
,如下所示
<activity
android:name=".newact"
android:label="@string/app_name">
</activity>
答案 2 :(得分:0)
将此添加到您的清单文件
SELECT * FROM tablename WHERE user_ID IN ($string);
答案 3 :(得分:0)
将其替换为 MainActivity ,这可能会解决您的问题。
Intent intent = new Intent(MainActivity.this, newact.class);
startActivity(intent);
在此之后,不要忘记在你的android menifest文件中添加,
<activity
android:name=".newact"
android:label="@string/app_name">
</activity>