简体   繁体   中英

Intent crashes on startactivity

i have a problem with an intent. it suddenly crashes on start activity. here is the first activity:

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);   
    }
}

this is the second:

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;
    }
}
  }

this is a part of my manifest:

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

what could i change? It crashes always on startActivity(intent), even though both activities are declared in manifest.

Update

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

try this, because you are finishing the activity if the ex is false

update

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

I solved the problem another way. When I launch the second activity, I immediately launch the first for 2 seconds and then destroy it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM