【问题标题】:App Crashes When Switching from Splash Screen to MainActivity从启动画面切换到 MainActivity 时应用程序崩溃
【发布时间】:2016-03-04 20:45:07
【问题描述】:

所以,我每次尝试运行我的应用程序时都会收到此错误,它会启动并在正确的时间内启动 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();
            }
        }
    };
}

【问题讨论】:

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


    【解决方案1】:

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

    希望对你有帮助...

    【讨论】:

      【解决方案2】:

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

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

      【讨论】:

        【解决方案3】:

        尝试直接在模拟器中打开 Main Activity,如果它正在运行,然后在 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() ;
          } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多