无法启动活动ComponentInfo {}:java.lang.NullPointerException

时间:2017-03-17 18:50:26

标签: java android

当我搜索错误时,我找到了错误的原因。但是,我在代码中找不到错误。你能救我吗?

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Intent intent = new Intent(this,IntroActivity.class);
    startActivity(intent);
}
}

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout     xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="net.nevzatgunay.pathytest.MainActivity">

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

IntroActivity.java

public class IntroActivity extends AppIntro {
@Override
protected void onCreate(Bundle savedInstanceState){
    addSlide(SampleSlide.newInstance(R.layout.slide_1));
    addSlide(SampleSlide.newInstance(R.layout.slide_2));
    addSlide(SampleSlide.newInstance(R.layout.slide_3));

    showSkipButton(false);
    setFadeAnimation();
}

@Override
public void onSkipPressed(Fragment currentFragment){
    super.onSkipPressed(currentFragment);
    finish();
}

@Override
public void onDonePressed(Fragment currentFragment){
    super.onDonePressed(currentFragment);
    finish();
}

@Override
public void onSlideChanged(@Nullable Fragment oldFragment, @Nullable Fragment newFragment){
    super.onSlideChanged(oldFragment,newFragment);
}
}

另外,我有三个xml文件,如slide_1,slide_2,slide_3。

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/colorPrimary">
<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="This is Slide 1"
    android:textColor="#ffffff"
    android:textSize="54sp"
    android:gravity="center"/>

</LinearLayout>

logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{net.nevzatgunay.pathytest/net.nevzatgunay.pathytest.IntroActivity}: java.lang.NullPointerException
 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
                                                                         at android.app.ActivityThread.access$600(ActivityThread.java:141)
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
                                                                         at android.os.Handler.dispatchMessage(Handler.java:99)
                                                                         at android.os.Looper.loop(Looper.java:137)
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5103)
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)
                                                                         at java.lang.reflect.Method.invoke(Method.java:525)
                                                                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
                                                                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
                                                                         at dalvik.system.NativeStart.main(Native Method)
                                                                      Caused by: java.lang.NullPointerException
                                                                         at com.github.paolorotolo.appintro.AppIntroBase.addSlide(AppIntroBase.java:389)
                                                                         at net.nevzatgunay.pathytest.IntroActivity.onCreate(IntroActivity.java:16)
                                                                         at android.app.Activity.performCreate(Activity.java:5133)
                                                                         at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
                                                                         at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
                                                                         at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261) 
                                                                         at android.app.ActivityThread.access$600(ActivityThread.java:141) 
                                                                         at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256) 
                                                                         at android.os.Handler.dispatchMessage(Handler.java:99) 
                                                                         at android.os.Looper.loop(Looper.java:137) 
                                                                         at android.app.ActivityThread.main(ActivityThread.java:5103) 
                                                                         at java.lang.reflect.Method.invokeNative(Native Method)

2 个答案:

答案 0 :(得分:0)

在致电super.onCreate()之前,您尚未从onCreate()的{​​{1}}致电IntroActivity。有关示例,请参阅the library documentation

答案 1 :(得分:0)

您需要在super.onCreate()的{​​{1}}功能中致电onCreate()。它应该是IntroActivity函数的第一行,需要初始化Activity,否则你所做的onCreate()注释是没有意义的。