意图在开始时崩溃

时间:2013-08-25 13:56:16

标签: java android android-intent

我有意图问题。 它突然在开始活动时崩溃了。 这是第一项活动:

package com.logio.bullsandcows;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.Menu;

public class StartingActivity extends Activity{
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.start);
        Handler handler = new Handler(); 
        handler.postDelayed(new Runnable() { 
            public void run() { 
                Intent intent = new Intent(StartingActivity.this, MainActivity.class);
                intent.putExtra("ex", true);
                startActivity(intent);
            } 
        }, 2000);   
    }
}

这是第二个:

package com.logio.bullsandcows;

import android.app.*;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.content.res.AssetFileDescriptor;
import android.content.res.AssetManager;

import java.io.IOException;
import java.util.*;

public class MainActivity extends Activity
{
    SoundPool mSP;
    AssetManager assets;
    int butpress1, butpress2, butpress3;

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

        mSP = new SoundPool(3, AudioManager.STREAM_MUSIC, 0);
        assets = getAssets();

        butpress1 = loadSound("1378.mp3");
        butpress2 = loadSound("1380.mp3");
        butpress3 = loadSound("1382.mp3");

    Button buttonnewgame = (Button) findViewById(R.id.buttonnewgame);
    Button buttonabout = (Button) findViewById(R.id.buttonabout);
    Button buttonhelp = (Button) findViewById(R.id.buttonhelp);


    buttonnewgame.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
                playSound(butpress1);
            switch(v.getId()){
                    case R.id.buttonnewgame:
                    Intent intent6 = new Intent(MainActivity.this, GameMenu.class);
                    startActivity(intent6);
                    break;
                }
    }
    });

        buttonabout.setOnClickListener(new OnClickListener(){
        public void onClick(View v){
            playSound(butpress2);
                switch(v.getId()){
                    case R.id.buttonabout:
                Intent intent = new Intent(MainActivity.this, AboutActivity.class);
                startActivity(intent);
                break;
                }
    }
    });

    buttonhelp.setOnClickListener(new OnClickListener(){
    public void onClick(View v){
        playSound(butpress3);
          switch(v.getId()){
          case R.id.buttonhelp:
          Intent intent = new Intent(MainActivity.this, HelpActivity.class);
          startActivity(intent);
          break;
        }
        }
});

if (getIntent().getBooleanExtra("ex", false)){
    finish();
    return;
}
}

   private int loadSound(String fileName){
   AssetFileDescriptor afd = null;
   try{
       afd = assets.openFd(fileName);
   } catch (IOException e){
       e.printStackTrace();
       Toast.makeText(this, "Oops! '"+fileName+"' not found!", Toast.LENGTH_SHORT).show();
       return -1;
   }
return mSP.load(afd, 1);
   }

   protected void playSound(int sound){
       if(sound>0)
           mSP.play(sound, 1, 1, 1, 0, 1);
   }


protected void shuffleArray(String[] specArray) {
    // TODO Auto-generated method stub
    Random rnd = new Random();
    for(int i = specArray.length-1; i>=0; i--)
    {
        int index = rnd.nextInt(i+1);
        String a= specArray[index];
        specArray[index] = specArray[i];
        specArray[i]=a;
    }
}
  }

这是我的清单的一部分:

    <activity
    android:name="com.logio.bullsandcows.MainActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
       <intent-filter>
        <action android:name="android.intent.action.MAINACTIVITY" />
         <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>            
</activity>

<activity
    android:name="com.logio.bullsandcows.StartingActivity"
    android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
    android:label="@string/app_name"
    android:screenOrientation="portrait">
 <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>   

我能改变什么?它总是在startActivity(intent)上崩溃,即使这两个活动都是在manifest中声明的。

2 个答案:

答案 0 :(得分:1)

更新

if(getIntent().getBooleanExtra("ex", false)){
    Log.e("Hi", "It was false");    
} else {
    Log.e("Hi", "It was true"); 
}
}

试试这个,因为如果 ex 为假

,您正在完成活动

<强>更新

<activity
        android:name="com.logio.bullsandcows.MainActivity"
        android:theme="@android:style/Theme.NoTitleBar.Fullscreen"
        android:label="@string/app_name"
        android:screenOrientation="portrait">
           <intent-filter>
            <action android:name="android.intent.action.MAINACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
        </intent-filter>            
    </activity>

答案 1 :(得分:0)

我用另一种方式解决了问题。 当我启动第二个活动时,我会立即启动第一个活动2秒钟,然后将其销毁。