Question

When i set a button on onClickListener in java file and run the apps, the apps show me that unfortunately stop. I try to make button that send me to other activity. But it's not working. The ADK don't show me any error. But the apps is not working.

Here is my code:

 package com.shanjedul.shanjedulhassan;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.os.Build;



public class Welcome extends ActionBarActivity {

Button okButton;

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

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


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

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub

            Intent in=new Intent(Welcome.this, About.class);
            startActivity(in);

        }
    });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {

    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.welcome, 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_welcome, container, false);
        return rootView;
    }
}

}
Was it helpful?

Solution

Keep the below line of code immediately after the setContentView(blah.....); And also ensure that you've Button defined in activity_welcome.xml file

okButton=(Button) findViewById(R.id.button1);

OTHER TIPS

This is because of you put your Button in activity_welcome xml insteand of fragment_welcome xml.Here fragment attach to activity so you have to set your button to fragment_welcome xml and then get findviewbyid of button in OnCreateView method or OnActivityCreated method in fragment class. Here your Activity like a container and your fragment is like a item so you cant get direct reference to container.

Or Second Way Remove fragment_welcome xml and also remove PlaceholderFragment class from activity then you can directly get activity_welcome xml components.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top