嘿,我正在尝试为 Android 创建一个带有启动画面的简单应用,然后转到主要 Activity,但每次应用完成启动画面并转到其他 Activity 时,它都会崩溃。
ps: 当我尝试移除启动时 MainActivity 正常启动而没有错误
我的MainActivity.java是
package com.example.andava;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter=0;
add = (Button) findViewById(R.id.bttnAdd);
sub = (Button) findViewById(R.id.bttnSub);
display = (TextView) findViewById(R.id.displaycount);
add.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter++;
display.setText(""+counter);
}
});
sub.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter-=1;
display.setText(""+counter);
}
});
}
}
Splash.java
package com.example.andava;
import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
public class Splash extends Activity {
MediaPlayer introSong;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
introSong = MediaPlayer.create(Splash.this, R.raw.introsong);
introSong.start();
Thread timer = new Thread(){
public void run(){
try {
sleep(5000);
} catch (InterruptedException e) {
e.printStackTrace();
} finally {
Intent openMainAct = new Intent("com.example.andava.MainActivity");
startActivity(openMainAct);
}
}
};
timer.start();
}
@Override
protected void onPause() {
super.onPause();
introSong.release();
finish();
}
}
list :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.andava"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.andava.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:name="com.example.andava.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
</application>
</manifest>
提前致谢..
最佳答案
你需要
Intent openMainAct = new Intent(CurrentActivity.this, MainActivity.class);
startActivity(openMainAct);
代替
Intent openMainAct = new Intent("com.example.andava.MainActivity");
startActivity(openMainAct);
关于java - 移动到另一个 Activity 时 Android 应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22640441/