Error opening another Activity

Asked

Viewed 449 times

0

Error that appears:

2018-10-16 00:31:04.280 9433-9433/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: imobmobile.br.com.imobmobile, PID: 9433
java.lang.RuntimeException: Unable to start activity ComponentInfo{imobmobile.br.com.imobmobile/imobmobile.br.com.imobmobile.MainActivityPrincipal}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' 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:866)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at imobmobile.br.com.imobmobile.MainActivityPrincipal.onCreate(MainActivityPrincipal.java:22)
    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:866) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:756) 

This is my Main Main:

package imobmobile.br.com.imobmobile;

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


public class MainActivityPrincipal extends AppCompatActivity {



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


        Button btnLogar = findViewById(R.id.btnEntrar);

       btnLogar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Intent intent = new Intent(MainActivityPrincipal.this, MainActivityLogin.class);
                startActivity(intent);
            }
        });

    }



}

That and my 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"
    android:background="@color/colorPrimaryDark"
    tools:context=".MainActivityPrincipal">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            android:id="@+id/textView"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:fontFamily="monospace"
            android:gravity="center"
            android:text="@string/imobmobile"
            android:textColor="@color/white"
            android:textSize="35sp"
            tools:targetApi="jelly_bean"
            />

        <Button
            android:id="@+id/btnEntrar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/colorAccent"
            android:text="@string/btnEntrar"
            android:textColor="@color/white"
            android:textSize="20sp" />

    </LinearLayout>
</android.support.constraint.ConstraintLayout>

I don’t know what to do to open the app.

2 answers

0

Here is the problem:

Button btnLogar = findViewById(R.id.btnEntrar);

Replace with:

Button btnLogar = (Button)findViewById(R.id.btnEntrar);

And it should work again, another alternative is to create a method in your Activity and call it in the layout:

public void abreOutraActivity(View v){

}

And in the layout:

<Button
android:id="@+id/btnEntrar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="@string/btnEntrar"
android:textColor="@color/white"
android:textSize="20sp" 
android:onClick="abreOutraActivity" />
  • Overwriting still appears the same error

  • Got it , had two layout of xmlPrincipal , erasing the V16 worked , thanks

  • 1

    @Antoniojunior, edit your question and explain how you solved your problem.

0

Hello! Check that the Activity (Mainactivitylogin) you created is in Manifest.xml, within the application tag:

<application>

    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivityPrincipal ">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

    <activity android:name=".MainActivityLogin" />

</application>

Browser other questions tagged

You are not signed in. Login or sign up in order to post.