0

这是我遵循的 youtube 教程中项目中的所有代码。(主要部分)项目活动:

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();
    }
}

主要的.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>

项目清单

<?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 中运行应用程序时单击按钮,应用程序都会崩溃并强制关闭。有任何想法吗?谢谢!

日志猫

> 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)
4

2 回答 2

0

好的,由于这是一个基本应用程序,因此应您的要求,我将发布完整的代码。

项目活动.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>

注意:我已经在模拟器上测试过了,它不会崩溃。我建议您阅读/查看更多教程以更好地理解。祝你好运!

于 2012-04-11T15:46:22.463 回答
0

Tutorialone的意图过滤器应该有类别"android.intent.category.DEFAULT"而不是"android.intent.category.LAUNCHER"

于 2012-04-11T15:35:16.553 回答