以下是我所遵循的youtube教程中项目中的所有代码。 (主要部分) ProjectActivity:
package justinehume.Project;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProjectActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
startActivity(new Intent("android.intent.action.TUTORIALONE"));
}
});
}
protected void onPause(){
super.onPause();
}
}
main.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">
<Button android:id="@+id/tutorial1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="Button" />
</LinearLayout>
ProjectManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="justinehume.Project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".ProjectActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:label="@string/app_name"
android:name=".Tutorialone" >
<intent-filter >
<action android:name="android.intent.action.TUTORIALONE" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Tutorialone.java
package justinehume.Project;
import android.app.Activity;
import android.os.Bundle;
public class Tutorialone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
}
}
每当我在SDK中运行应用程序时单击按钮,应用程序崩溃并强制关闭。有任何想法吗? 谢谢!
logcat的
> 04-11 16:19:54.799: D/AndroidRuntime(344): Shutting down VM 04-11
> 16:19:54.809: W/dalvikvm(344): threadid=1: thread exiting with
> uncaught exception (group=0x4001d800) 04-11 16:19:54.819:
> E/AndroidRuntime(344): FATAL EXCEPTION: main 04-11 16:19:54.819:
> E/AndroidRuntime(344): android.content.ActivityNotFoundException: No
> Activity found to handle Intent {
> act=android.intent.action.TUTORIALONE } 04-11 16:19:54.819:
> E/AndroidRuntime(344): at
> android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1408)
> 04-11 16:19:54.819: E/AndroidRuntime(344): at
> android.app.Instrumentation.execStartActivity(Instrumentation.java:1378)
> 04-11 16:19:54.819: E/AndroidRuntime(344): at
> android.app.Activity.startActivityForResult(Activity.java:2817) 04-11
> 16:19:54.819: E/AndroidRuntime(344): at
> android.app.Activity.startActivity(Activity.java:2923) 04-11
> 16:19:54.819: E/AndroidRuntime(344): at
> justinehume.Project.ProjectActivity$1.onClick(ProjectActivity.java:23)
> 04-11 16:19:54.819: E/AndroidRuntime(344): at
> android.view.View.performClick(View.java:2408) 04-11 16:19:54.819:
> E/AndroidRuntime(344): at
> android.view.View$PerformClick.run(View.java:8816) 04-11 16:19:54.819:
> E/AndroidRuntime(344): at
> android.os.Handler.handleCallback(Handler.java:587) 04-11
> 16:19:54.819: E/AndroidRuntime(344): at
> android.os.Handler.dispatchMessage(Handler.java:92) 04-11
> 16:19:54.819: E/AndroidRuntime(344): at
> android.os.Looper.loop(Looper.java:123) 04-11 16:19:54.819:
> E/AndroidRuntime(344): at
> android.app.ActivityThread.main(ActivityThread.java:4627) 04-11
> 16:19:54.819: E/AndroidRuntime(344): at
> java.lang.reflect.Method.invokeNative(Native Method) 04-11
> 16:19:54.819: E/AndroidRuntime(344): at
> java.lang.reflect.Method.invoke(Method.java:521) 04-11 16:19:54.819:
> E/AndroidRuntime(344): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
> 04-11 16:19:54.819: E/AndroidRuntime(344): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-11
> 16:19:54.819: E/AndroidRuntime(344): at
> dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
Tutorialone
的意图过滤器应该包含类别"android.intent.category.DEFAULT"
,而不是"android.intent.category.LAUNCHER"
答案 1 :(得分:0)
好的,根据您的要求,因为这是一个基本的应用程序,我发布了完整的代码。
<强> ProjectActivity.java 强>
package justinehume.Project;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class ProjectActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button tut1 = (Button) findViewById(R.id.tutorial1);
tut1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Jean-Philippe Roy's correction
startActivity(new Intent(ProjectActivity.this, Tutorialone.class));
}
});
}
}
<强> Tutorialone.java 强>
package justinehume.Project;
import android.app.Activity;
import android.os.Bundle;
public class Tutorialone extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.tutorial1);
}
}
<强>的AndroidManifest.xml 强>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="justinehume.Project"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".ProjectActivity"
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:label="@string/app_name" android:name=".Tutorialone"/>
</application>
</manifest>
注意:我已经在模拟器上测试了它并且它没有崩溃。我建议您阅读/查看更多教程以便更好地理解。祝你好运!