Skip to content
Advertisement

Unable start activity, FATAL EXCEPTION: main [closed]

How can I solve this type of problem? The problem is in setOnClickListener which gives me a NullPointerException. If you need anything else let me know.

This is the error in Log:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mapsprova/com.example.home.HomeActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
    at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
    at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
    at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:223)
    at android.app.ActivityThread.main(ActivityThread.java:7656)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void androidx.cardview.widget.CardView.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
public class HomeActivity extends AppCompatActivity {
    private CardView btn_home;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_home);

        btn_home = findViewById(androidx.appcompat.R.id.home);

        btn_home.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                Intent intent = new Intent(HomeActivity.this, TrovamicoActivity.class);
                startActivity(intent);
            }

        });

    }

Advertisement

Answer

Your problem originate from your ClickListener, here is the problem :

btn_home = findViewById(androidx.appcompat.R.id.home); // This is wrong
// I believe you have to call it this way (only if its id is 'home' in your layout)
btn_home = findViewById(R.id.home);

// This call fail with NullPointerException
btn_home.setOnClickListener(new View.OnClickListener() {
    // Listener code
});

Disclaimer : without your layouts I can’t tell you for sure what is in cause, but stacktrace is pretty clear, clicklistener cannot be called on null value

Edit

First, you don’t have any View with id ‘home’ as declared in your mainActivity. So I suppose (as your code confirms, var is a CardView) you intended to use cardview as (sort of) a button. So you have to replace by this code :

btn_home = findViewById(R.id.trovamico);

Now it should work when you click on your Trovamico Cardview (from HomeActivity

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement