2012-12-02 42 views
0

我还是很新,所以解决方案可能很明显。 任何方式我试图让这个应用程序中的按钮打开新的活动,但是当我运行它时,forcecloses。有谁知道什么是错的?我打算使用名为list view的东西,但是没有使用我为这个类制作的XML。线程退出与未捕获的异常(组= 0x4001d800)致命例外:主

package com.example.musicbynumbers; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.view.Menu; 
import android.view.View; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.ImageButton; 
import android.widget.ListView; 

public class MainMenu extends Activity implements View.OnClickListener { 
    Button majScales, minHarm, minMel; 
    ImageButton mainMenu; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     // TODO Auto-generated method stub 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main_menu); 
     mainMenu = (ImageButton) findViewById(R.id.imagelogo); 
     majScales = (Button) findViewById(R.id.majorscalesb); 


    } 




     @Override 
      public void onClick(View arg0) { 
       // TODO Auto-generated method stub 
       switch(arg0.getId()){ 
       case R.id.imagelogo: 
        Intent i = new Intent(MainMenu.this, MainMenu.class); 
        startActivity(i); 
        break; 
       case R.id.majorscalesb: 
        Intent j = new Intent(MainMenu.this, majorScales.class); 
        startActivity(j); 
        break;} 

    } 

} 

布局:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="80" 
    tools:context=".MainMenu" 

    > 

    <ImageButton 
     android:id="@+id/imagelogo" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:src="@drawable/ic_launcher" 
     android:layout_weight="10" 
     android:gravity="center" /> 

    <Button 
     android:id="@+id/majorscalesb" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:text="Major Scales" 
     android:gravity="center" /> 

    <Button 
     android:id="@+id/minormelodicb" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:text="Minor Melodic Scales" 
     android:gravity="center"/> 

    <Button 
     android:id="@+id/minorharmonicb" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:text="Minor Harmonic Scales" 
     android:gravity="center"/> 

    <Button 
     android:id="@+id/arpeggiosb" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:text="Arpeggios" 
     android:gravity="center" /> 

     <Button 
      android:id="@+id/chromaticscalesb" 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_weight="10" 
      android:text="Chromatic Scales" 
      android:gravity="center" /> 

    <Button 
     android:id="@+id/contraryb" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:text="Contrary Motion" 
     android:gravity="center" /> 

    <Button 
     android:id="@+id/aboutb" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_weight="10" 
     android:text="About" 
     android:gravity="center" 
     > 
    </Button> 

</LinearLayout> 

清单:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.example.musicbynumbers" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="16" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.example.musicbynumbers.MainMenu" 
      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="com.example.musicbynumbers.majorScales" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="com.example.musicbynumbers.majorScales" /> 

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


</manifest> 

回答

1

ImageButton mainMenu; 

mainMenu = (ImageButton) findViewById(R.id.imagelogo); 

更新来解决您的代码:

OK,我发现了错误。你忘了设置OnClickListener为每ButtonImageButton

试试这个

majScales.setOnClickListener(this); 
    mainMenu.setOnClickListener(this); 
+0

谢谢你没有意识到我犯了这么多错误。现在它不会崩溃按钮不工作任何想法?我会更新代码。 –

0

一件事你拼写错误的意图。

Intent j = new Intent("com.exapmle.muscibynumbers.majorScales"); 

应该

Intent j = new Intent("com.example.musicbynumbers.majorScales"); 

另一个原因是,你必须定义两个应用程序。

没有堆栈跟踪,很难知道还有什么可能是错误的。

+0

谢谢,但不要偏食已经注意到,并把一个红色标记旁边? –

+0

不是。它不是语法错误。这是一个完全有效的字符串。 –

相关问题