0

我已经遇到过这个问题并在stackoverflows帮助下解决了它,但它又回来了。当我单击其中一个按钮时,它会打开一个新活动,但应用程序会强制关闭。

主菜单

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;
    Intent j;
    Intent k;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
         j = new Intent(this, majorScales.class);
          k = new Intent(MainMenu.this, minorHarmonic.class);
        mainMenu = (ImageButton) findViewById(R.id.imagelogo);
        majScales = (Button) findViewById(R.id.majorscalesb);
        minHarm = (Button) findViewById(R.id.minorharmonicb);
        minMel = (Button) findViewById(R.id.minormelodicb);
        majScales.setOnClickListener(this);
        mainMenu.setOnClickListener(this);
        minHarm.setOnClickListener(this);
        minMel.setOnClickListener(this);



    }




         @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:

                    startActivity(j);
                    break;
                case R.id.minorharmonicb:

                    startActivity(k);
                    break;
                case R.id.minormelodicb:
                    Intent l = new Intent(MainMenu.this, minorMelodic.class);
                    startActivity(l);
                    break;


    }

}
}

大调

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 majorScales extends Activity implements View.OnClickListener  {
    Button aflatmaj, amaj, bflatmaj, bmaj, cmaj, dflatmaj, dmaj, eflatmaj, emaj, fmaj, fsharpmaj, gmaj;  
    ImageButton mainMenu;
    Intent j;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_menu);
        j  = new Intent(this, display.class);
        mainMenu = (ImageButton) findViewById(R.id.imagelogo);
        aflatmaj = (Button) findViewById(R.id.aflatmajb);
        amaj = (Button) findViewById(R.id.amajb);
        bflatmaj = (Button) findViewById(R.id.bflatmajb);
        bmaj = (Button) findViewById(R.id.bmajb);
        cmaj = (Button) findViewById(R.id.cmajb);
        dflatmaj = (Button) findViewById(R.id.dflatmajb);
        dmaj = (Button) findViewById(R.id.dmajb);
        eflatmaj = (Button) findViewById(R.id.eflatmajb);
        emaj = (Button) findViewById(R.id.emajb);
        fmaj = (Button) findViewById(R.id.fmajb);
        fsharpmaj = (Button) findViewById(R.id.fsharpmajb);
        gmaj = (Button) findViewById(R.id.gmajb);
        mainMenu.setOnClickListener(this);
        aflatmaj.setOnClickListener(this);
        amaj.setOnClickListener(this);
        bflatmaj.setOnClickListener(this);
        bmaj.setOnClickListener(this);
        cmaj.setOnClickListener(this);
        dflatmaj.setOnClickListener(this);
        dmaj.setOnClickListener(this);
        eflatmaj.setOnClickListener(this);
        emaj.setOnClickListener(this);
        fmaj.setOnClickListener(this);
        fsharpmaj.setOnClickListener(this);
        gmaj.setOnClickListener(this);



    }




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

                    startActivity(j);
                    break;
                case R.id.amajb:

                    startActivity(j);
                    break;
                case R.id.bflatmajb:

                    startActivity(j);
                    break;
                case R.id.bmajb:

                    startActivity(j);
                    break;
                case R.id.cmajb:

                    startActivity(j);
                    break;
                case R.id.dflatmajb:

                    startActivity(j);
                    break;
                case R.id.dmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.eflatmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.emajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.fmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.fsharpmajb:
                    ;
                    startActivity(j);
                    break;
                case R.id.gmajb:
                    ;
                    startActivity(j);
                    break;



    }

}
}

小谐波

package com.example.musicbynumbers;

import android.app.Activity;
import android.os.Bundle;

public class minorHarmonic extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.minorharmonic);
    }

}

AndroidManifest

<?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>
         <activity
            android:name="com.example.musicbynumbers.minorHarmonic"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="com.example.musicbynumbers.minorHarmonic" />

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


</manifest>

更新:我在清单中添加了minorHarmonic,并且该活动打开它似乎只是majorScales。

4

1 回答 1

1

您需要在onCreate()被调用后创建您的 Intent。用这个:

Intent j;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main_menu);

    j = new Intent(this, display.class);

原因听起来很复杂:thisIntent 中的引用使用了 Activity 的 Context,但是这个 Context 直到 after 才有效onCreate()


添加:

我注意到您的清单中缺少一些活动:

<activity android:name=".display" />
<activity android:name=".minorHarmonic" />
// etc
于 2012-12-10T00:14:58.873 回答