Dev Android. Null pointer when accessing a button from a fragment by main activity using set contentview

0

I made the layout of an application where I use the android menu. Menu is in main activity:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        setContentView(R.layout.fragment_main);
        return true;
    }
    if (id == R.id.action_list) {
        setContentView(R.layout.fragment_veiculos);
        return true;
    }
    if (id == R.id.action_web) {
        setContentView(R.layout.fragment_web);
        return true;
    }
    return super.onOptionsItemSelected(item);
}

I made the call of the button on the main main:

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

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

    Button salvar = (Button) findViewById(R.id.btnSalvar);

    salvar.setOnClickListener(new View.OnClickListener() {          
        @Override
        public void onClick(View v) {
            BancoController crud = new BancoController(getBaseContext());

            EditText nome = (EditText) findViewById(R.id.nomeVeiculo);
            EditText placa = (EditText) findViewById(R.id.placa);
            EditText renavam = (EditText) findViewById(R.id.renavam);

            String nomStr = nome.getText().toString();
            String placaStr = placa.getText().toString();
            String renavamStr = renavam.getText().toString();
            String resultado;

            resultado = crud.insereDado(nomStr, placaStr, renavamStr);

            Toast.makeText(getApplicationContext(), resultado, Toast.LENGTH_LONG).show();
        }
    });
}

The layout is quiet, the following error happens when I call the save.setOnClickListener. :

  

01-11 14: 25: 46,424: E / AndroidRuntime (1618): FATAL EXCEPTION: main   01-11 14: 25: 46,424: E / AndroidRuntime (1618): Process: com.br.dossiesc,   PID: 1618 01-11 14: 25: 46,424: E / AndroidRuntime (1618):   java.lang.RuntimeException: Unable to start activity   ComponentInfo {com.br.dossiesc / com.br.dossiesc.MainActivity}:   java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.Button.setOnClickListener (android.view.View $ OnClickListener) '   on a null object reference 01-11 14: 25: 46,424: E / AndroidRuntime (1618):     at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2305)   01-11 14: 25: 46,424: E / AndroidRuntime (1618): at   android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2365)   01-11 14: 25: 46,424: E / AndroidRuntime (1618): at   android.app.ActivityThread.access $ 800 (ActivityThread.java:148) 01-11   14: 25: 46.424: E / AndroidRuntime (1618): at   android.app.ActivityThread $ H.handleMessage (ActivityThread.java:1283)   01-11 14: 25: 46,424: E / AndroidRuntime (1618): at   android.os.Handler.dispatchMessage (Handler.java:102) 01-11   14: 25: 46.424: E / AndroidRuntime (1618): at   android.os.Looper.loop (Looper.java:135) 01-11 14: 25: 46,424:   E / AndroidRuntime (1618): at   android.app.ActivityThread.main (ActivityThread.java:5272) 01-11   14: 25: 46.424: E / AndroidRuntime (1618): at   java.lang.reflect.Method.invoke (Native Method) 01-11 14: 25: 46,424:   E / AndroidRuntime (1618): at   java.lang.reflect.Method.invoke (Method.java:372) 01-11 14: 25: 46,424:   E / AndroidRuntime (1618): at   com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run (ZygoteInit.java:909)   01-11 14: 25: 46,424: E / AndroidRuntime (1618): at   com.android.internal.os.ZygoteInit.main (ZygoteInit.java:704) 01-11   14: 25: 46.424: E / AndroidRuntime (1618): Caused by:   java.lang.NullPointerException: Attempt to invoke virtual method 'void   android.widget.Button.setOnClickListener (android.view.View $ OnClickListener) '   on a null object reference 01-11 14: 25: 46,424: E / AndroidRuntime (1618):     at com.br.dossiesc.MainActivity.onCreate (MainActivity.java:28) 01-11   14: 25: 46.424: E / AndroidRuntime (1618): at   android.app.Activity.performCreate (Activity.java:5977) 01-11   14: 25: 46.424: E / AndroidRuntime (1618): at   android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1105)   01-11 14: 25: 46,424: E / AndroidRuntime (1618): at   android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2258)   01-11 14: 25: 46.424: E / AndroidRuntime (1618): ... 10 more

Thanks for the help right away.

    
asked by anonymous 11.01.2016 / 17:37

1 answer

1

To solve this problem it does this:

Within your PlaceholderFragment fragment, create the following method:

public void setSalvarButtonListener(View.OnClickListener listener) {
  Button salvar = (Button) getView().findViewById(R.id.btnSalvar);
  salvar.setOnClickListener(listener);
}

Agona in your main does the following:

PlaceholderFragment fragment = new PlaceholderFragment();
fragment.setSalvarButtonListener(new View.OnClickListener() {          
    @Override
    public void onClick(View v) {
        BancoController crud = new BancoController(getBaseContext());

        EditText nome = (EditText) findViewById(R.id.nomeVeiculo);
        EditText placa = (EditText) findViewById(R.id.placa);
        EditText renavam = (EditText) findViewById(R.id.renavam);

        String nomStr = nome.getText().toString();
        String placaStr = placa.getText().toString();
        String renavamStr = renavam.getText().toString();
        String resultado;

        resultado = crud.insereDado(nomStr, placaStr, renavamStr);

        Toast.makeText(getApplicationContext(), resultado, Toast.LENGTH_LONG).show();
    }
});

if (savedInstanceState == null) {
    getFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
}
    
11.01.2016 / 18:13