我是android编程新手。当我点击"打开第二个活动" /"打开第三个活动"时,我开始使用多个活动并运行应用程序。按钮,Genymotion模拟器说:不幸的是,FirstApp已停止。并返回到android菜单..为什么?我收到这样的错误
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:4020)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jahnaviswaroop11gmail.firstapp/com.jahnaviswaroop11gmail.firstapp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1777)
at android.app.Instrumentation.execStartActivity(Instrumentation.java:1501)
at android.app.Activity.startActivityForResult(Activity.java:3745)
at android.app.Activity.startActivityForResult(Activity.java:3706)
at android.support.v4.app.FragmentActivity.startActivityForResult(FragmentActivity.java:820)
at android.app.Activity.startActivity(Activity.java:4016)
at android.app.Activity.startActivity(Activity.java:3984)
at com.jahnaviswaroop11gmail.firstapp.FirstActivity.showGreetings(FirstActivity.java:25)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at android.view.View$1.onClick(View.java:4015)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
我的FirstActivity.java
public class FirstActivity extends AppCompatActivity {
TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
textView = (TextView) findViewById(R.id.greetings_text_view);
}
public void showGreetings(View view){
String button_text = ( (Button) view).getText().toString();
if (button_text.equals("Open Second Activity"))
{
Intent intent = new Intent(this,SecondActivity.class);
startActivity(intent);
}
else if (button_text.equals("Open Third Activity"))
{
Intent intent = new Intent(this,ThirdActivity.class);
startActivity(intent);
}
}
}
我的SecondActvity.java
import android.app.Activity;
import android.os.Bundle;
public class SecondActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_layout);
}
}
我的ThirdActivity.java
import android.app.Activity;
import android.os.Bundle;
public class ThirdActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.third_layout);
}
}
我的firstlayout.xml文件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="greetings appear here"
android:id="@+id/greetings_text_view"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Second Activity"
android:onClick="showGreetings"
android:layout_below="@+id/greetings_text_view"
android:id="@+id/button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Open Third Activity"
android:onClick="showGreetings"
android:id="@+id/button2"
android:layout_below="@+id/button"
android:layout_centerHorizontal="true"
android:layout_marginTop="71dp" />
我的第二个xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to Second Activity"
android:id="@+id/textView"
android:layout_gravity="center_horizontal" />
我的第三个布局xml文件
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to third Activity"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" />
答案 0 :(得分:1)
请在发布问题之前验证异常堆栈跟踪。 例外情况清楚地提到了问题的原因..
Caused by: android.content.ActivityNotFoundException: Unable to find explicit activity class {com.jahnaviswaroop11gmail.firstapp/com.jahnaviswaroop11gmail.firstapp.SecondActivity}; have you declared this activity in your AndroidManifest.xml?
将SecondActvity
添加到<application>
代码
<activity android:name="com.jahnaviswaroop11gmail.firstapp.SecondActivity" />
答案 1 :(得分:0)
将其添加到<application>
标记内的清单中。它应该可以解决你的问题。
<activity android:name="com.jahnaviswaroop11gmail.firstapp.SecondActivity"/>
答案 2 :(得分:0)
Androidmanifest.xml
包含不正确的数据,您必须添加有关第二和第三项活动的信息
<activity
android:name=".SecondActivity"/>
<activity
android:name=".ThirdActivity"/>
答案 3 :(得分:0)
您需要将第一个和第二个活动都放到您的清单......
<activity android:name=".SecondActivity"/>
<activity android:name=".ThirdActivity"/>
并获得文字
Button button = (Button)view;
String button_text= button.getText().toString();
并添加权限
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>