Android setOnClickListener无效

时间:2014-04-19 17:03:48

标签: java android button

在Android Activity.java中添加按钮操作后,应用程序正在关闭。 请帮忙解决。我在这里缺少什么代码。????

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 个答案:

答案 0 :(得分:3)

您似乎在 fragment_main.xml 而非 activity_main.xml 中构建了视图。

当你第一次创建一个新的android项目时,你会自动创建和打开这些文件:

enter image description here

然后,当您开始时,在fragment_main.xml文件中添加视图(例如:TextView)。虽然您尝试在Activity内使用此视图执行基本事件,但这样的事情:

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

您无法运行自己的应用,或者有时只有白屏,因为您尝试调用/显示的视图布局错误。

  

解决方案:将onCreateView方法中的所有内容移动到Fragment类中。调用视图并在相关片段中执行某些操作,而不是父活动


例如,对于您的情况:

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

答案 1 :(得分:1)

我得到了解决方案,我也进行了测试

问题不仅在于将代码移入

但移动代码后我必须使用以下内容

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

而不是以下

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

我希望任何机构在这里发表评论以解释这种行为