簡體   English   中英

從啟動畫面切換到MainActivity時,應用程序崩潰

[英]App Crashes When Switching from Splash Screen to MainActivity

因此,每次嘗試運行我的應用程序時,我都會不斷收到此錯誤,它會在正確的時間內啟動並啟動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清單:

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

您好,在您開始下一個活動后,請先完成您的活動,嘗試更改此方式,

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

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

嘗試首先在模擬器中直接打開Main Activity(如果正在運行),然后在啟動時嘗試

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

只有在setContentView(int)設置了活動布局之后,才可以調用findViewById(int) 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();
        }
    }
};
}

希望對您有幫助...

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM