2012-04-11 170 views
0

這裏是我所遵循的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

請發佈LogCat輸出。 – Abhijit 2012-04-11 15:07:13

+1

嘗試使用'new Intent(ProjectActivity.this,Tutorialon.class)'開始您的活動,而不是現在正在使用的內容。此外,發佈你的logcat堆棧跟蹤的力量關閉。 – 2012-04-11 15:07:46

+0

logcat現在存在 – JTH 2012-04-11 15:21:03

回答

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> 

注意:我已經在模擬器上測試過了,它不會崩潰。我建議您閱讀/查看更多教程以更好地理解。祝你好運!

0

Tutorialone的意圖過濾器應具有"android.intent.category.DEFAULT"類別,而不是"android.intent.category.LAUNCHER"