Unfortunately Application has stopped

Asked

Viewed 147 times

1

Good afternoon. I’m starting with Android development and I’m trying to navigate between several screens but there is the error Unfortunately TCC has stopped at the time of running in the emulator. I already researched and could not find the problem. Could someone help me? Thank you

My Activity

public class MenuActivity extends Activity {

private Button btnLancamentos;
private Button btnObjetivos;
private Button btnSobre;
public  Button btnObjInserir;

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

    btnLancamentos = (Button)findViewById(R.id.btnLancamentos);
    btnObjetivos   = (Button)findViewById(R.id.btnObjetivos);
    btnSobre       = (Button)findViewById(R.id.btnSobre);
    btnObjInserir  = (Button)findViewById(R.id.btnObjInserir);

    btnLancamentos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TelaLanc();     
        }
    });

    btnObjetivos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TelaObj();      
        }
    });

    btnSobre.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TelaSobre();        
        }
    }); 

    btnObjInserir.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            TelaObjInserir();       
        }
    }); 

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

}

protected void TelaLanc() {
    Intent intent = new Intent(this, TelaLancActivity.class);
    startActivity(intent);
}

protected void TelaObj() {
    Intent intent = new Intent(this, TelaObjActivity.class);
    startActivity(intent);
}

protected void TelaSobre() {
    Intent intent = new Intent(this, TelaSobreActivity.class);
    startActivity(intent);
}

protected void TelaObjInserir() {
    startActivity(new Intent (MenuActivity.this, TelaObjInserirActivity.class));

}

@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, 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();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

/**
 * A placeholder fragment containing a simple view.
 */
public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_menu, container, false);
        return rootView;
    }
}

}

Screen call class

package pacote.tcc;

public class TelaObjActivity extends Activity{

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tela_obj);
}

}

Screen call class

package pacote.tcc;

public class TelaObjInserirActivity extends Activity{

protected void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
    setContentView(R.layout.tela_obj_inserir);

    Button btVoltar = (Button) findViewById(R.id.btVoltar);
    btVoltar.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                startActivity(new Intent (TelaObjInserirActivity.this, MenuActivity.class));
                finish();
            }
});

}

}

  • 1

    Hi, João, welcome to [en.so]. Please check out http://answall.com/editing-help

  • John, you declared your activities in the manifest?

1 answer

2


You have a fragment in Activity, check whether activity_menu.xml contains the buttons btnLancamentos, btnObjetivos, btnSobre and btonObjInserir or if they are in fragment_menu.xml. If they are in the Fragment, the lines

btnLancamentos = (Button)findViewById(R.id.btnLancamentos);
btnObjetivos   = (Button)findViewById(R.id.btnObjetivos);
btnSobre       = (Button)findViewById(R.id.btnSobre);
btnObjInserir  = (Button)findViewById(R.id.btnObjInserir);

will return null and the next line, btnLancamentos.setOnClickListener(…) launch NullPointerException, causing the crash. If this is the case, move the code into the PlaceholderFragment.

Browser other questions tagged

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