Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/323.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 从初始屏幕切换到MainActivity时应用程序崩溃_Java_Android_Android Activity_Android Studio_Crash - Fatal编程技术网

Java 从初始屏幕切换到MainActivity时应用程序崩溃

Java 从初始屏幕切换到MainActivity时应用程序崩溃,java,android,android-activity,android-studio,crash,Java,Android,Android Activity,Android Studio,Crash,因此,每次我尝试运行我的应用程序时,我都会遇到这个错误,它会启动并启动SplashActivity,持续正确的时间,然后尝试启动MainActivity,但立即失败 据我所知,这是一个空指针错误,但我一辈子也弄不清楚是什么 错误: 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.lab4aoneill3

因此,每次我尝试运行我的应用程序时,我都会遇到这个错误,它会启动并启动SplashActivity,持续正确的时间,然后尝试启动MainActivity,但立即失败

据我所知,这是一个空指针错误,但我一辈子也弄不清楚是什么

错误:

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

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

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

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

尝试在emulator中直接打开主活动(如果它正在运行),然后在splash中尝试此操作

    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() ;
  } 
findViewById(int)
只能在通过
setContentView(int)
设置活动布局后调用。如果膨胀视图,则可以对该视图对象调用
findviewbyd(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();
        }
    }
};
}
希望它能帮助你

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

            startActivity(new Intent(SplashActivity.this,MainActivity.class));
 finish();
        }
    };
    Timer opening = new Timer();
    opening.schedule(splash,5000);
    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() ;
  } 
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();
        }
    }
};
}