Android Dev. Null Pointer when accessing a button of a Fragment by main Activity using set contentview

Asked

Viewed 111 times

0

I made the layout of an application where I use the android menu. Menu is on 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 from the boot on main itself:

@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 run 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 your help so far.

1 answer

1

To solve this problem does so:

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);
}

Now on your main do 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();
}

Browser other questions tagged

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