; y应用程序在启动后停止片刻

时间:2017-12-24 08:08:12

标签: android nullpointerexception

我的应用程序启动后片刻。最初,应用程序曾经运行没有问题,但在我在主屏幕之前添加登录屏幕后,它经常停止,出现错误"不幸的是,AutoparesOnline已停止。我是android的新手,我很感激任何帮助。下面是log,activity.xml和java代码。

这是xml。

    <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="wrap_content"
    android:layout_height="wrap_content"
    android:theme="@style/newTheme"
    android:layout_weight="6"
    android:background="@android:color/black">

    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_marginLeft="7dp"
        android:layout_marginStart="7dp">

        <TextView
            android:text="@string/welcome"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tv_welcome"
            android:textStyle="normal|bold"
            android:textColor="@color/btn_login_bg"
            android:textSize="24sp"
            android:textAlignment="center" />
    </LinearLayout>

    <RelativeLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:background="@drawable/circle"
        android:layout_centerVertical="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">

        <ImageView
            android:contentDescription="A mechanic with his tools"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:src="@drawable/handy_man"
            android:id="@+id/imageView9"
            android:layout_alignParentTop="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_marginTop="19dp"
            tools:ignore="HardcodedText" />

    </RelativeLayout>

    <Button
        android:text="@string/Continue"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/btnStartAnotherActivity"
        style="@style/Widget.AppCompat.Button.Colored"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>

这是java代码

    import android.content.Intent;
    import android.os.Bundle;
    import android.support.v7.app.AppCompatActivity;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ProgressBar;


    public class MainActivity extends AppCompatActivity{

    Button btn1;
    private ProgressBar pBar;

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

        btn1 = (Button) findViewById(R.id.btnStartMainActivity);
        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(MainActivity.this, AnotherActivity.class);
                startActivity(intent);
            }
        });
        pBar.setVisibility(View.VISIBLE);



    }

以下是错误的一部分。

FATAL EXCEPTION: main
                                                                  Process: co.autosparesonline.ke, PID: 3011
                                                                  java.lang.RuntimeException: Unable to start activity ComponentInfo{co.autosparesonline.ke/co.autosparesonline.ke.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460)
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                      at android.os.Looper.loop(Looper.java:154)
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6077)
                                                                      at java.lang.reflect.Method.invoke(Native Method)
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
                                                                   Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
                                                                      at co.autosparesonline.ke.MainActivity.onCreate(MainActivity.java:21)
                                                                      at android.app.Activity.performCreate(Activity.java:6662)
                                                                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)
                                                                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707) 
                                                                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1460) 
                                                                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                      at android.os.Looper.loop(Looper.java:154) 
                                                                      at android.app.ActivityThread.main(ActivityThread.java:6077) 
                                                                      at java.lang.reflect.Method.invoke(Native Method) 
                                                                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865) 
                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755) 

1 个答案:

答案 0 :(得分:-1)

btn1 = (Button)btn1.findViewById(R.id.btnStartMainActivity);

将上述内容更改为:

btn1 = (Button) findViewById(R.id.btnStartMainActivity);

此外,在致电pBar

之前,您不会指定setVisibility