2013-01-31 37 views
2

我正在关注YouTube教程以创建android应用程序。 在其中一篇教程中,他们讲述了如何使用List活动创建Menu类。使用ListActivity时强制关闭应用程序和ActivityNotFoundException

public class Menu extends ListActivity { 

String classes[] = {"Start","example1","example2","example3","example4","example5","example6"}; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    // TODO Auto-generated method stub 
    super.onCreate(savedInstanceState); 
    setListAdapter(new ArrayAdapter<String>(Menu.this, android.R.layout.simple_list_item_1, classes)); 
} 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 
    // TODO Auto-generated method stub 
    super.onListItemClick(l, v, position, id); 
    String cheese = classes[position]; 
    try{ 
     Class ourClass = Class.forName("com.example.add." + cheese); 
     Intent ourIntent = new Intent(Menu.this,ourClass); 
     startActivity(ourIntent); 
    }catch(ClassNotFoundException e){ 
     e.printStackTrace(); 
    } 
} 

并在清单中添加活动。

<activity 
     android:name=".Splash" 
     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:exported="false" 
     android:name=".Menu" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MENU" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:exported="false" 
     android:name="com.example.add.Start" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="com.example.add.START" /> 

      <category android:name="android.intent.category.DEFAULT" /> 
     </intent-filter> 
    </activity> 

我的项目有一个启动类,它增加了一个计数器并显示, 飞溅类定义背景图片,音乐和它的主类。 添加菜单上课之前我的应用程序工作正常,但在我加入这我的应用程序开始强制关闭和logcat的显示,飞溅类的ActivityNotFoundException和错误在第28行

public class Splash extends Activity{ 

MediaPlayer ourSong; 

@Override 
protected void onCreate(Bundle Dab) { 
    // TODO Auto-generated method stub 
    super.onCreate(Dab); 
    setContentView(R.layout.splash); 
    ourSong = MediaPlayer.create(Splash.this, R.raw.nodebeat); 
    ourSong.start(); 
    Thread timer = new Thread(){ 
     public void run(){ //framework to start a new thread 
      try{ 
       sleep(5000); 
      }catch(InterruptedException e){ 
       e.printStackTrace(); 
      }finally{ 
       Intent openStart = new Intent("com.example.add.MENU"); 
       startActivity(openStart); 
      } 
     } 
    }; 
    timer.start(); 
} 

@Override 
protected void onPause() { 
    // TODO Auto-generated method stub 
    super.onPause(); 
    ourSong.release(); 
    finish(); 
} 

所以,请帮助我新的Android应用发展和im袭击在这里。 链接到logcat的日志: - https://docs.google.com/file/d/0BxLaXhL-q50-MHlhUW93SmxNT0U/edit调试日志 https://docs.google.com/file/d/0BxLaXhL-q50-b1pzbVpIcVNQR2s/edit错误日志

感谢。

回答

1

您正在使用错误的意图过滤器来尝试启动。所以,要么改变

<action android:name="android.intent.action.MENU" /> 

<action android:name="com.example.add.MENU" /> 

让一切都匹配。

或者更改呼叫在您的代码,以便它是:

Intent openStart = new Intent("android.intent.action.MENU"); 

你应该阅读有关Intent Filters

你也可以使用explict intent,因为你没有导出这个Activity并且知道它的名字,我没有看到需要一个intent过滤器。

Intent openStart = new Intent(Splash.this, Menu.class); 
startActivity(openStart); 
+0

但在辅导这些方法只是工作得很好,所以我应该怎么去的错误。 –

+0

并非常感谢你,它的工作! –

+0

不客气!至于教程正常工作,总会有错误输入的机会或教程不完​​全正常工作。总是检查评论 - 通常人们抱怨,如果它不起作用:-) –

0

意向行动在下面的(1和2)地方应该是相同的,而不是。使它们相同。

<intent-filter> 
     <action android:name="android.intent.action.MENU" />// (1) 

     <category android:name="android.intent.category.DEFAULT" /> 
    </intent-filter> 

Intent openStart = new Intent("com.example.add.MENU"); (2) 
+0

感谢您的帮助! –