0
I’m trying to call a second screen in Android Studio, but this giving error.
This is the main class:
package br.com.olamundo.parametros;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.*;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.*;
import android.content.*;
public class ActMain extends AppCompatActivity implements View.OnClickListener {
    private EditText txtValor;
    private Button btnOk;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.act_main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        txtValor = (EditText)findViewById(R.id.txtValor);
        btnOk = (Button)findViewById(R.id.btnOk);
        btnOk.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        try {
            Intent it = new Intent(ActMain.this, ActSegundaTela.class);
            it.putExtra("VALOR", txtValor.getText().toString());
            startActivity(it);            
        }
        catch(Exception erro)
        {
        }
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_act_main, menu);
        return true;
    }
    @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();
        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }
}
When you click the OK button, you get the error:
03-13 17:01:59.931 11712-11712/br.com.olamundo.parametros E/AndroidRuntime: FATAL EXCEPTION: main
Process: br.com.olamundo.parametros, PID: 11712
java.lang.RuntimeException: Unable to start activity ComponentInfo{br.com.olamundo.parametros/br.com.olamundo.parametros.ActSegundaTela}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at br.com.olamundo.parametros.ActSegundaTela.onCreate(ActSegundaTela.java:35)
at android.app.Activity.performCreate(Activity.java:6237)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1107)
Aluem knows what may be happening?
Why implement
OnClickListenerin your Activity class? Change the code and do the click assignment as you did with the buttonfab.– user28595
sorry, could exemplify?
– alessandre martins
Do the same thing as in Fab: "Fab.onClickListener" .... " btnOk.onClickListener"...
– itscorey