-3

My application a few moments after launching. Initially, the app used to run without problems, but after i added a login screen before the main screen, it frequently stops, with an error "Unfortunately AutoparesOnline has stopped. I'm new to android and i'd appreciate any help. below is the log, the activity.xml and the java code.

This is the 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>

This is the java code

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



    }

Here is part of the error.

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) 
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
pete mwangi
  • 61
  • 1
  • 7

1 Answers1

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

Change above to:

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

Also you do not assign pBar before calling setVisibility

Niko
  • 8,093
  • 5
  • 49
  • 85