setOnClickListener (Unfortunately Myapplication has stopped)

Asked

Viewed 1,723 times

0

Whenever I try to use setOnClickListener the application to, I have tried several methods to try to fix this problem, but I could not. the Login button is properly set in R, also in the layout, the problem is always with setOnClickListener.

Java activity.:

package myapplication.app;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;

public class Activity extends ActionBarActivity {

Button login;

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

    if (savedInstanceState == null){
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }

    login = (Button) findViewById(R.id.entrar);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            setContentView(R.layout.principal);
        }
    });
}

Log:

02-18 17:39:27.356    1614-1614/myapplication.app E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: myapplication.app, PID: 1614
java.lang.RuntimeException: Unable to start activity ComponentInfo{myapplication.app/myapplication.app.Activity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at myapplication.app.Activity.onCreate(Activity.java:30)
        at android.app.Activity.performCreate(Activity.java:5231)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
  • The button R.id.entrar is in Fragment or Activity xml?

  • It’s in the Fragment. I found this message in Exception: "Invalid element:Androidlightfield:"

  • Another @Raphael thing, since you are using fragments, your navigation should be done using the Fragment!

  • Can I only use Activity? @Igorcastañedaferreira

  • Of course. Just remove the block that is inside the if (savedInstanceState == null). But the navigation by Fragment is a standard that helps make apps that will work on smatphones and tablets. And the correct way to navigate between Acitivities you can see in this link

1 answer

1


There occurs a NullPointerException when you try to associate a Listener to your button. That’s because the button is not in the xml of Activity and yes in xml of Fragment. To correct, simply the button processing is done inside the Fragment. And not in Activity.

Activity:

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity);

    if (savedInstanceState == null){
        getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();
    }
}

public void changeView() {
    setContentView(R.layout.principal);
}

Fragment:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
    Button login = (Button) rootView.findViewById(R.id.entrar);

    login.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ((Activity) PlaceholderFragment.this.getActivity()).changeView();
        }
    });
    return rootView;
}

Edited

As pointed out by @Fernando in the comments, the findViewById method does not belong to the Fragment and yes to the View.

  • 1

    findViewById is not a method belonging to the Fragment and yes to View. Hence the correct in the scope of the onCreateView of Fragment would be: Button login = (Button) rootView.findViewById(R.id.entrar); instead of Button login = (Button) findViewById(R.id.entrar);

Browser other questions tagged

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