我是android的新手,我尝试运行我的Android应用程序,它不会在控制台上给出任何错误,但我在模拟器中打开应用程序我得到一个Toast msg 应用程序未安装在您的手机上 same query。在这个论坛中询问,他们说如果您在AndroidManifest.xml中声明了两次MainActivity会发生这种情况,我会尝试但不会
的AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity1"
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=".Activity2"
android:label="@string/app_name">
</activity>
</application>
</manifest>
活性1
package com.example.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity1);
addListenerOnButton();
}
public void addListenerOnButton() {
final Context context = this;
Button b1=(Button)findViewById(R.id.textView1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(context,Activity2.class);
startActivity(intent);
}
});
}
}
Activity2.java
package com.example.intent;
import android.app.Activity;
import android.os.Bundle;
public class Activity2 extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity2);
}
}
activity_activity1.xml - &GT; Activity1.java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="252dp"
android:layout_height="40dp"
android:layout_gravity="center_horizontal"
android:text="@string/t1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/intent"/>
</LinearLayout>
activity_activity2.xml - &gt; Activity2.java
<?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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/t2"
/>
</LinearLayout>
String.xml
<resources>
<string name="app_name">Intent</string>
<string name="intent">Click me to Another Screen</string>
<string name="t1">I am Screen 1!</string>
<string name="t2">I am Screen 2!</string>
<string name="hello_world">Hello world!</string>
<string name="menu_settings">Settings</string>
<string name="title_activity_activity1">Activity1</string>
</resources>
logcat的
09-07 17:10:01.281: D/AndroidRuntime(756): Shutting down VM
09-07 17:10:01.291: W/dalvikvm(756): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-07 17:10:01.301: E/AndroidRuntime(756): FATAL EXCEPTION: main
09-07 17:10:01.301: E/AndroidRuntime(756): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intent/com.example.intent.Activity1}: java.lang.ClassCastException: android.widget.TextView
09-07 17:10:01.301: E/AndroidRuntime(756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.os.Looper.loop(Looper.java:123)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 17:10:01.301: E/AndroidRuntime(756): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 17:10:01.301: E/AndroidRuntime(756): at java.lang.reflect.Method.invoke(Method.java:521)
09-07 17:10:01.301: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 17:10:01.301: E/AndroidRuntime(756): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 17:10:01.301: E/AndroidRuntime(756): at dalvik.system.NativeStart.main(Native Method)
09-07 17:10:01.301: E/AndroidRuntime(756): Caused by: java.lang.ClassCastException: android.widget.TextView
09-07 17:10:01.301: E/AndroidRuntime(756): at com.example.intent.Activity1.addListenerOnButton(Activity1.java:22)
09-07 17:10:01.301: E/AndroidRuntime(756): at com.example.intent.Activity1.onCreate(Activity1.java:17)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 17:10:01.301: E/AndroidRuntime(756): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 17:10:01.301: E/AndroidRuntime(756): ... 11 more
09-07 17:10:10.700: I/Process(756): Sending signal. PID: 756 SIG: 9
09-07 17:15:13.781: D/AndroidRuntime(812): Shutting down VM
09-07 17:15:13.781: W/dalvikvm(812): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-07 17:15:13.801: E/AndroidRuntime(812): FATAL EXCEPTION: main
09-07 17:15:13.801: E/AndroidRuntime(812): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intent/com.example.intent.Activity1}: java.lang.ClassCastException: android.widget.TextView
09-07 17:15:13.801: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.os.Looper.loop(Looper.java:123)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 17:15:13.801: E/AndroidRuntime(812): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 17:15:13.801: E/AndroidRuntime(812): at java.lang.reflect.Method.invoke(Method.java:521)
09-07 17:15:13.801: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 17:15:13.801: E/AndroidRuntime(812): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 17:15:13.801: E/AndroidRuntime(812): at dalvik.system.NativeStart.main(Native Method)
09-07 17:15:13.801: E/AndroidRuntime(812): Caused by: java.lang.ClassCastException: android.widget.TextView
09-07 17:15:13.801: E/AndroidRuntime(812): at com.example.intent.Activity1.addListenerOnButton(Activity1.java:22)
09-07 17:15:13.801: E/AndroidRuntime(812): at com.example.intent.Activity1.onCreate(Activity1.java:17)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 17:15:13.801: E/AndroidRuntime(812): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 17:15:13.801: E/AndroidRuntime(812): ... 11 more
09-07 17:20:13.841: I/Process(812): Sending signal. PID: 812 SIG: 9
09-07 17:24:50.690: D/AndroidRuntime(840): Shutting down VM
09-07 17:24:50.690: W/dalvikvm(840): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-07 17:24:50.710: E/AndroidRuntime(840): FATAL EXCEPTION: main
09-07 17:24:50.710: E/AndroidRuntime(840): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.intent/com.example.intent.Activity1}: java.lang.ClassCastException: android.widget.TextView
09-07 17:24:50.710: E/AndroidRuntime(840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.os.Handler.dispatchMessage(Handler.java:99)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.os.Looper.loop(Looper.java:123)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-07 17:24:50.710: E/AndroidRuntime(840): at java.lang.reflect.Method.invokeNative(Native Method)
09-07 17:24:50.710: E/AndroidRuntime(840): at java.lang.reflect.Method.invoke(Method.java:521)
09-07 17:24:50.710: E/AndroidRuntime(840): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-07 17:24:50.710: E/AndroidRuntime(840): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-07 17:24:50.710: E/AndroidRuntime(840): at dalvik.system.NativeStart.main(Native Method)
09-07 17:24:50.710: E/AndroidRuntime(840): Caused by: java.lang.ClassCastException: android.widget.TextView
09-07 17:24:50.710: E/AndroidRuntime(840): at com.example.intent.Activity1.addListenerOnButton(Activity1.java:22)
09-07 17:24:50.710: E/AndroidRuntime(840): at com.example.intent.Activity1.onCreate(Activity1.java:17)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-07 17:24:50.710: E/AndroidRuntime(840): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-07 17:24:50.710: E/AndroidRuntime(840): ... 11 more
答案 0 :(得分:3)
你在这里做错了
android:name="activity_activity1"
android:name="activity_activity2"
使用此代码
<activity
android:name=".Activity1"
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=".Activity2"
android:label="@string/app_name">
</activity>
答案 1 :(得分:1)
像这样添加活动
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Activity1"
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=".Activity2"
android:label="@string/app_name">
</activity>
</application>
</manifest>
答案 2 :(得分:1)
你做错了
Button b1=(Button)findViewById(R.id.textView1);
将按钮转换为textview
更改为
Button b1=(Button)findViewById(R.id.button1);
还使用活动上下文
Intent intent = new Intent(Activity1.this,Activity2.class);