Android setOnClickListener not working

2019-01-12 11:56发布

After adding button action in android Activity.java application is closing unfortunately. please help in resolving. what code i am missing here.????

public class MainActivity extends ActionBarActivity {


int counter;
Button add, sub;
TextView display;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    counter = 0;
    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.textDisp);

    add.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            counter++;
        }
    });

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


@Override
public boolean onCreateOptionsMenu(Menu menu) {

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

}

2条回答
在下西门庆
2楼-- · 2019-01-12 12:30

It seems you built your views inside fragment_main.xml and not activity_main.xml.

When you first create a new android project, you have these files which are automatically created and opened:

enter image description here

Then, when you begin, you add views (e.g: a TextView) inside fragment_main.xml file. Whereas you tried to do a basically event with this view inside your Activity, something like this:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main); // Using layout activity_main.xml

        // You try to set a simple text on the view (TextView) previously added
        TextView text = (TextView) findViewById(R.id.textView1);
        text.setText("Simple Text");  // And you get an error here!

        /*
         * You do an fragment transaction to add PlaceholderFragment Fragment
         * on screen - this below snippnet is automatically created.
        */
        if(savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    } 

You cannot run your app or sometimes you have only a white screen, because the views that you tried to call/display are into the wrong layout..

Solution: Move all your stuff inside onCreateView method into the Fragment class. Call views and do something in the related fragment and not the parent activity.


For example, for your case:

public static class PlaceholderFragment extends Fragment {

    int counter;
    Button add, sub;
    TextView display;

    public PlaceholderFragment() {
    }

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

        counter = 0;
        // Don't forget to attach your view to the inflated view as "rootView.findViewById()"
        add = (Button) rootView.findViewById(R.id.bAdd);
        sub = (Button) rootView.findViewById(R.id.bSub);
        display = (TextView) rootView.findViewById(R.id.textDisp);

        add.setOnClickListener(new View.OnClickListener() {
                public void onClick(View v) {
                    // Perform action on click
                    counter++;
                }
        });
        return rootView;
    }
}
查看更多
劫难
3楼-- · 2019-01-12 12:36

i got the solution and i tested it too

the problem isn't only moving the code into

but also i have to use the following after moving the code

add = (Button) rootView.findViewById (R.id.bAdd);
  sub = (Button) rootView.findViewById(R.id.bSub);
  display = (TextView) rootView.findViewById(R.id.tvDisplay);

instead of the following

  add = (Button) findViewById (R.id.bAdd);
            sub = (Button) findViewById(R.id.bSub);
            display = (TextView) findViewById(R.id.tvDisplay);

i hope any body comment here for the explanation of that behavior please

查看更多
登录 后发表回答