setOnClickListener (unfortunately MyApplication has stopped)

0

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

Activity.java:

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)
    
asked by anonymous 18.02.2014 / 23:51

1 answer

1

A NullPointerException occurs when you try to associate a listener with your button. This is because the button is not in the xml of Activity, but in xml of Fragment . To correct, it is enough that the button treatment 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 comments, the findViewById method does not belong to Fragment but rather to View .     

19.02.2014 / 00:13