从启动画面切换到MainActivity时,应用程序崩溃

时间:2015-12-01 02:47:01

标签: java android android-activity android-studio crash

因此,每次尝试运行我的应用程序时,我都会一直收到此错误,启动并启动SplashActivity一段正确的时间,然后尝试启动MainActivity并立即失败。

据我所知,这是一个NullPointer错误,但我不能为我的生活弄清楚是什么。

错误:

11-30 21:03:33.146 3216-3216/? D/dalvikvm: Not late-enabling CheckJNI (already on)
11-30 21:03:34.646 3216-3216/edu.wmich.lab4aoneill3193 W/dalvikvm: VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;)
11-30 21:03:34.646 3216-3216/edu.wmich.lab4aoneill3193 I/dalvikvm: Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.view.WindowCallbackWrapper.onSearchRequested
11-30 21:03:34.656 3216-3216/edu.wmich.lab4aoneill3193 W/dalvikvm: VFY: unable to resolve interface method 14517: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z
11-30 21:03:34.656 3216-3216/edu.wmich.lab4aoneill3193 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-30 21:03:34.656 3216-3216/edu.wmich.lab4aoneill3193 I/dalvikvm: Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.view.WindowCallbackWrapper.onWindowStartingActionMode
11-30 21:03:34.656 3216-3216/edu.wmich.lab4aoneill3193 W/dalvikvm: VFY: unable to resolve interface method 14521: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode;
11-30 21:03:34.656 3216-3216/edu.wmich.lab4aoneill3193 D/dalvikvm: VFY: replacing opcode 0x72 at 0x0002
11-30 21:03:34.856 3216-3216/edu.wmich.lab4aoneill3193 D/dalvikvm: GC_FOR_ALLOC freed 129K, 7% free 2772K/2972K, paused 57ms, total 59ms
11-30 21:03:34.856 3216-3216/edu.wmich.lab4aoneill3193 I/dalvikvm-heap: Grow heap (frag case) to 5.308MB for 2658320-byte allocation
11-30 21:03:34.916 3216-3225/edu.wmich.lab4aoneill3193 D/dalvikvm: GC_FOR_ALLOC freed <1K, 4% free 5367K/5572K, paused 48ms, total 48ms
11-30 21:03:35.646 3216-3216/edu.wmich.lab4aoneill3193 I/dalvikvm: Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.widget.TintTypedArray.getChangingConfigurations
11-30 21:03:35.646 3216-3216/edu.wmich.lab4aoneill3193 W/dalvikvm: VFY: unable to resolve virtual method 409: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
11-30 21:03:35.646 3216-3216/edu.wmich.lab4aoneill3193 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-30 21:03:35.656 3216-3216/edu.wmich.lab4aoneill3193 I/dalvikvm: Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.widget.TintTypedArray.getType
11-30 21:03:35.656 3216-3216/edu.wmich.lab4aoneill3193 W/dalvikvm: VFY: unable to resolve virtual method 431: Landroid/content/res/TypedArray;.getType (I)I
11-30 21:03:35.656 3216-3216/edu.wmich.lab4aoneill3193 D/dalvikvm: VFY: replacing opcode 0x6e at 0x0002
11-30 21:03:35.866 3216-3216/edu.wmich.lab4aoneill3193 W/EGL_emulation: eglSurfaceAttrib not implemented
11-30 21:03:35.886 3216-3216/edu.wmich.lab4aoneill3193 D/OpenGLRenderer: Enabling debug mode 0
11-30 21:03:41.596 3216-3216/edu.wmich.lab4aoneill3193 D/AndroidRuntime: Shutting down VM
11-30 21:03:41.596 3216-3216/edu.wmich.lab4aoneill3193 W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0xb3ac0ba8)
11-30 21:03:41.666 3216-3216/edu.wmich.lab4aoneill3193 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                         Process: edu.wmich.lab4aoneill3193, PID: 3216
                                                                         java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.wmich.lab4aoneill3193/edu.wmich.lab4aoneill3193.MainActivity}: java.lang.NullPointerException
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:135)
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                             at android.os.Looper.loop(Looper.java:136)
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5017)
                                                                             at java.lang.reflect.Method.invokeNative(Native Method)
                                                                             at java.lang.reflect.Method.invoke(Method.java:515)
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
                                                                             at dalvik.system.NativeStart.main(Native Method)
                                                                          Caused by: java.lang.NullPointerException
                                                                             at android.app.Activity.findViewById(Activity.java:1884)
                                                                             at edu.wmich.lab4aoneill3193.MainActivity.<init>(MainActivity.java:36)
                                                                             at java.lang.Class.newInstanceImpl(Native Method)
                                                                             at java.lang.Class.newInstance(Class.java:1208)
                                                                             at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
                                                                             at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
                                                                             at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
                                                                             at android.app.ActivityThread.access$800(ActivityThread.java:135) 
                                                                             at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
                                                                             at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                             at android.os.Looper.loop(Looper.java:136) 
                                                                             at android.app.ActivityThread.main(ActivityThread.java:5017) 
                                                                             at java.lang.reflect.Method.invokeNative(Native Method) 
                                                                             at java.lang.reflect.Method.invoke(Method.java:515) 
                                                                             at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
                                                                             at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
                                                                             at dalvik.system.NativeStart.main(Native Method) 
11-30 21:08:42.916 3216-3216/edu.wmich.lab4aoneill3193 I/Process: Sending signal. PID: 3216 SIG: 9

Android Manifest:

<?xml version="1.0" encoding="utf-8"?>

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".SplashActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

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

SplashActivity.java:

    package edu.wmich.lab4aoneill3193;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.util.Timer;
import java.util.TimerTask;

public class SplashActivity extends AppCompatActivity {

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

        TimerTask splash = new TimerTask() {
            @Override
            public void run() {
                finish();
                startActivity(new Intent(SplashActivity.this,MainActivity.class));
            }
        };
        Timer opening = new Timer();
        opening.schedule(splash,5000);
    }
}

MainActivity.Java:

package edu.wmich.lab4aoneill3193

import android.app.DatePickerDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.RadioButton;
import android.widget.TextView;
import android.widget.Toast;

import java.text.DateFormat;
import java.util.Calendar;

public class MainActivity extends AppCompatActivity {

    private TextView reservation;

    final RadioButton chcOne = (RadioButton) findViewById(R.id.choiceOne);
    final RadioButton chcTwo = (RadioButton) findViewById(R.id.choiceTwo);
    final RadioButton chcThree = (RadioButton) findViewById(R.id.choiceThree);

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

        reservation = (TextView) findViewById(R.id.rsltText);
        Button btnDate = (Button) findViewById(R.id.btnDate);

        btnDate.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new DatePickerDialog(MainActivity.this, date, cal.get(Calendar.YEAR),
                        cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
            }
        });

    }

    Calendar cal = Calendar.getInstance();
    DateFormat fmtDate = DateFormat.getDateInstance();
    DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
            cal.set(Calendar.YEAR, year);
            cal.set(Calendar.MONTH, monthOfYear);
            cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
            if (chcOne.isChecked()) {
                reservation.setText("A Massive Asteroid Will Impact The Earth on "
                        + fmtDate.format(cal.getTime()));
            } else {Toast.makeText(MainActivity.this, "Choose Something!",Toast.LENGTH_LONG).show();
            }
            if (chcTwo.isChecked()){
                reservation.setText("Massive Tsunamis Will Engulf The World on "
                        + fmtDate.format(cal.getTime()));
            } else {Toast.makeText(MainActivity.this, "Choose Something!",Toast.LENGTH_LONG).show();
            }
            if (chcThree.isChecked()) {
                reservation.setText("A Fatal Disease Will OverWhelm The World on "
                        + fmtDate.format(cal.getTime()));
            } else {Toast.makeText(MainActivity.this, "Choose Something!",Toast.LENGTH_LONG).show();
            }
        }
    };
}

3 个答案:

答案 0 :(得分:0)

嗨,在开始下一个活动后,首先完成您的活动,尝试改变这种方式,

TimerTask splash = new TimerTask() {
        @Override
        public void run() {

            startActivity(new Intent(SplashActivity.this,MainActivity.class));
 finish();
        }
    };
    Timer opening = new Timer();
    opening.schedule(splash,5000);

答案 1 :(得分:0)

如果正在运行,请尝试直接在模拟器中打开主要活动,然后在启动时尝试此操作

    Thread timer= new Thread () {
   public void run() {
   try{sleep(2000);}catch (InteruptedException e) {e.printstacktrace() ;}
   finally {Intent intent=new Intent(splash.this, MainActivity.class)     startActivity(intent) ;}
}
 };
 timer.start();
 @Override
 public void onPause() {
super.onPause() ;
 finish() ;
  } 

答案 2 :(得分:0)

只有在findViewById(int)设置活动布局后才能调用{p> setContentView(int)。如果您夸大了观看次数,则可以在该视图对象上调用findViewById(int)

所以将代码更改为

<强> MainActivity.Java: 公共类MainActivity扩展了AppCompatActivity {

private TextView reservation;

RadioButton chcOne;
RadioButton chcTwo;
RadioButton chcThree;

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

    reservation = (TextView) findViewById(R.id.rsltText);
    Button btnDate = (Button) findViewById(R.id.btnDate);
    chcOne = (RadioButton) findViewById(R.id.choiceOne);
    chcTwo = (RadioButton) findViewById(R.id.choiceTwo);
    chcThree = (RadioButton) findViewById(R.id.choiceThree);

    btnDate.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            new DatePickerDialog(MainActivity.this, date, cal.get(Calendar.YEAR),
                    cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)).show();
        }
    });

}

Calendar cal = Calendar.getInstance();
DateFormat fmtDate = DateFormat.getDateInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
        cal.set(Calendar.YEAR, year);
        cal.set(Calendar.MONTH, monthOfYear);
        cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);
        if (chcOne.isChecked()) {
            reservation.setText("A Massive Asteroid Will Impact The Earth on "
                    + fmtDate.format(cal.getTime()));
        } else {Toast.makeText(MainActivity.this, "Choose Something!",Toast.LENGTH_LONG).show();
        }
        if (chcTwo.isChecked()){
            reservation.setText("Massive Tsunamis Will Engulf The World on "
                    + fmtDate.format(cal.getTime()));
        } else {Toast.makeText(MainActivity.this, "Choose Something!",Toast.LENGTH_LONG).show();
        }
        if (chcThree.isChecked()) {
            reservation.setText("A Fatal Disease Will OverWhelm The World on "
                    + fmtDate.format(cal.getTime()));
        } else {Toast.makeText(MainActivity.this, "Choose Something!",Toast.LENGTH_LONG).show();
        }
    }
};
}

希望它对你有所帮助......