在更改初始启动活动后,应用程序无法启动

时间:2016-04-05 22:24:41

标签: android eclipse

所以我创建了一个小问答游戏的应用程序,我有3个活动,每个活动都有自己的问题类型。最初我将地理区域作为启动屏幕,但是当我尝试将启动屏幕更改为"主页时#34;该应用程序无法启动。 Eclipse显示0个错误。

新的HomeScreen代码:

package com.example.geoquizlab1solution;

import android.app.Activity;
import android.content.Intent;
//import android.content.SharedPreferences;
import android.os.Bundle;
//import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
//import android.widget.ImageButton;
import android.widget.TextView;
//import android.widget.Toast;
import android.media.MediaPlayer;

public class HomeScreen extends Activity {

private TextView mWelcome;
private Button mSportsButton;
private Button mGeoButton;
private Button mMusicButton;
final MediaPlayer mp = MediaPlayer.create(this, R.raw.welcome);






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


    mWelcome = (TextView)findViewById(R.id.welcome_text);
    ( (View) mWelcome) .setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            mp.start();
        }
    });

    mGeoButton = (Button)findViewById(R.id.geo_button);
    mGeoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent1 = new Intent(HomeScreen.this, QuizActivity.class);
            if (intent1 != null) {
                startActivity(intent1);
        }
        }
    });

    mSportsButton = (Button)findViewById(R.id.sports_button);
    mSportsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent1 = new Intent(HomeScreen.this, SportsQuiz.class);
            if (intent1 != null) {
                startActivity(intent1);
            }
        }
    });

    mMusicButton = (Button)findViewById(R.id.music_button);
    mMusicButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent1 = new Intent(HomeScreen.this, MusicQuiz.class);
            if (intent1 != null) {
                startActivity(intent1);
            }
        }
    });

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_quiz, menu);
    return true;
}



}

旧的启动画面代码:

package com.example.geoquizlab1solution;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
import android.media.MediaPlayer;

public class QuizActivity extends Activity {

private Button mTrueButton;
private Button mFalseButton;
private ImageButton mNextButton;
private ImageButton mPreviousButton;
private TextView mQuestionTextView;
private Button mResetButton;
private Button mSportsButton;
private Button mGeoButton;
private Button mMusicButton;
private int counter = 0;


private TrueFalse[] mQuestionBank = new TrueFalse[] {
        new TrueFalse(R.string.question_oceans, true),
        new TrueFalse(R.string.question_mideast, false),
        new TrueFalse(R.string.question_africa, false),
        new TrueFalse(R.string.question_americas, true),
        new TrueFalse(R.string.question_asia, true)
};


private int mCurrentIndex=0;

private void updateQuestion(){
    int question = mQuestionBank[mCurrentIndex].getQuestion();
    mQuestionTextView.setText(question);
}


private void checkAnswer(boolean userPressedTrue) {
    boolean answerIsTrue = mQuestionBank[mCurrentIndex].isTrueQuestion();
    int messageResId = 0;
    final MediaPlayer mp = MediaPlayer.create(this, R.raw.ding);
    final MediaPlayer mp1 = MediaPlayer.create(this, R.raw.wrong);

    SharedPreferences app_preferences = 
            PreferenceManager.getDefaultSharedPreferences(this);



    if (userPressedTrue == answerIsTrue) {
        mp.start();
        messageResId = R.string.correct_toast;
        counter++;
        if (counter % 5 == 0) {
            final MediaPlayer mp2 = MediaPlayer.create(this, R.raw.applause);
            mp2.start();
        }
    }
        else {
            mp1.start();
            messageResId = R.string.incorrect_toast;
        }
     SharedPreferences.Editor editor = app_preferences.edit();
        editor.putInt("count", counter);
    Toast.makeText(this, messageResId, Toast.LENGTH_SHORT).show();
    Toast.makeText(this,"Score:" + counter, Toast.LENGTH_SHORT).show();
    editor.commit();
        }

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


    mGeoButton = (Button)findViewById(R.id.geo_button);
    mGeoButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter = 0;
            Toast.makeText(getApplicationContext(), "Score: 0",
                    Toast.LENGTH_SHORT).show();
            mCurrentIndex = 0;
            updateQuestion();
        }
    });

    mSportsButton = (Button)findViewById(R.id.sports_button);
    mSportsButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent1 = new Intent(QuizActivity.this, SportsQuiz.class);
            if (intent1 != null) {
                startActivity(intent1);
            }
        }
    });

    mMusicButton = (Button)findViewById(R.id.music_button);
    mMusicButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            final Intent intent1 = new Intent(QuizActivity.this, MusicQuiz.class);
            if (intent1 != null) {
                startActivity(intent1);
            }
        }
    });

    mTrueButton = (Button)findViewById(R.id.true_button);
    mTrueButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswer(true);

        }
    }); 

    mFalseButton = (Button)findViewById(R.id.false_button);
    mFalseButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            checkAnswer(false);
            }
    });
    mQuestionTextView = (TextView)findViewById(R.id.question_text_view);
    ((View) mQuestionTextView) .setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
    });
    updateQuestion();

    mPreviousButton = (ImageButton)findViewById(R.id.previous_button);
    ((View) mPreviousButton) .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
                if (mCurrentIndex != 0) {
                    mCurrentIndex = (mCurrentIndex - 1) % mQuestionBank.length;
                updateQuestion();
                }
                else {
                    mCurrentIndex = (mCurrentIndex + 4) % mQuestionBank.length;
                }
        }
    });
    updateQuestion();


    mNextButton = (ImageButton)findViewById(R.id.next_button);
    ((View) mNextButton).setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            mCurrentIndex = (mCurrentIndex + 1) % mQuestionBank.length;
            updateQuestion();
        }
        });
    updateQuestion();

    mResetButton = (Button)findViewById(R.id.reset_button);
    ((View) mResetButton).setOnClickListener(new View.OnClickListener () {
        @Override
        public void onClick(View v) {
            counter = 0;
            Toast.makeText(getApplicationContext(), "Score: 0",
                    Toast.LENGTH_SHORT).show();
            mCurrentIndex = 0;
            updateQuestion();
        } 
    });
}




@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_quiz, menu);
    return true;
}



}

AndroidManifest文件:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.geoquizlab1solution"
android:versionCode="2"
android:versionName="1.1" 
xmlns:tools="http://schemas.android.com/tools" tools:ignore="OldTargetApi">


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

<application
    android:allowBackup="true"
    android:icon="@drawable/quiz"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.geoquizlab1solution.HomeScreen"
        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:screenOrientation="unspecified" android:name="com.example.geoquizlab1solution.QuizActivity"/>
  <activity android:screenOrientation="unspecified" android:name="com.example.geoquizlab1solution.SportsQuiz"/>
  <activity android:screenOrientation="unspecified" android:name="com.example.geoquizlab1solution.MusicQuiz"/>
    </application>
    </manifest>

新主屏幕布局文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" 
android:background="@drawable/spotlight">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:textAlignment="gravity"
    android:gravity="center"
    android:textStyle="italic"
    android:id="@+id/welcome_text"
    android:textColor="#5E5E5E"
    android:background="@drawable/welcomeview"/>

<LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:layout_weight="6">

    <Button
             android:id="@+id/geo_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/geo_button" 
             android:textColor="#2ADB20"
             android:background="#275393"
             android:layout_margin="25dp"/>
        <Button
             android:id="@+id/sports_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/sports_button" 
             android:textColor="#FFFFFF"
             android:background="#ED1515"
            android:layout_margin="25dp"/>

        <Button
             android:id="@+id/music_button"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text="@string/music_button" 
             android:textColor="#FFD700"
             android:background="#544F4F"
            android:layout_margin="25dp"/>


</LinearLayout>

日志:

    04-05 15:46:29.861: E/AndroidRuntime(22191): FATAL EXCEPTION: main
04-05 15:46:29.861: E/AndroidRuntime(22191): Process: com.example.geoquizlab1solution, PID: 22191
04-05 15:46:29.861: E/AndroidRuntime(22191): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.geoquizlab1solution/com.example.geoquizlab1solution.HomeScreen}: java.lang.NullPointerException
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2124)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.app.ActivityThread.access$800(ActivityThread.java:139)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.os.Handler.dispatchMessage(Handler.java:102)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.os.Looper.loop(Looper.java:136)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.app.ActivityThread.main(ActivityThread.java:5097)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at java.lang.reflect.Method.invokeNative(Native Method)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at java.lang.reflect.Method.invoke(Method.java:515)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at dalvik.system.NativeStart.main(Native Method)
04-05 15:46:29.861: E/AndroidRuntime(22191): Caused by: java.lang.NullPointerException
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.content.ContextWrapper.getResources(ContextWrapper.java:89)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:78)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.media.MediaPlayer.create(MediaPlayer.java:844)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at com.example.geoquizlab1solution.HomeScreen.<init>(HomeScreen.java:22)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at java.lang.Class.newInstanceImpl(Native Method)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at java.lang.Class.newInstance(Class.java:1208)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.app.Instrumentation.newActivity(Instrumentation.java:1084)
04-05 15:46:29.861: E/AndroidRuntime(22191):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2115)
04-05 15:46:29.861: E/AndroidRuntime(22191):    ... 11 more
04-05 15:46:32.298: D/PhoneStatusBar(1186): StatusBarWindow onTouchListener.onTouch

1 个答案:

答案 0 :(得分:0)

删除&#39; final MediaPlayer mp = MediaPlayer.create(this, R.raw.welcome);&#39;并将其嵌入onCrete()方法中,在创建活动之前设置值会导致此错误