0

Fiz o layout de uma aplicacao onde utilizo o menu do android. Menu esta na 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);
}

Fiz a chamada do botao na propria 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();
        }
    });
}

O layout esta funcionado tranquilo o seguinte erro acontece quando chamo o salvar.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

Obrigado pela ajuda desde já.

1
  • Veja se isto ajuda.
    – ramaral
    Commented 11/01/2016 às 19:54

1 Resposta 1

1

Para resolver este problema faz assim:

Dentro do teu fragmento PlaceholderFragment, cria o seguinte metodo:

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

Agona no teu main faz o seguinte:

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

Você deve fazer log-in para responder a esta pergunta.

Esta não é a resposta que você está procurando? Pesquise outras perguntas com a tag .