Java 为什么setOnClick会使此应用程序崩溃?

Java 为什么setOnClick会使此应用程序崩溃?,java,android,crash,Java,Android,Crash,我是android新手(对java并不完全陌生)。试着跟随一个有点过时的教程。我不明白为什么setOnClickListener会破坏这个应用程序。我知道某个地方存在空指针异常,但我不确定如何修复它。另外,请注意,为什么我的类要扩展ActionBarActivity,而不是像我到目前为止看到的所有示例中那样扩展Activity public class StartingPoint extends ActionBarActivity { int counter; Button a

我是android新手(对java并不完全陌生)。试着跟随一个有点过时的教程。我不明白为什么setOnClickListener会破坏这个应用程序。我知道某个地方存在空指针异常,但我不确定如何修复它。另外,请注意,为什么我的类要扩展ActionBarActivity,而不是像我到目前为止看到的所有示例中那样扩展Activity

public class StartingPoint extends ActionBarActivity {

    int counter;
    Button add, sub;
    TextView display;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_starting_point);

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

        counter = 0;

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

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                counter++;
                display.setText("Counter equals" + counter);

            }
        });


    } // 



    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

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

您的按钮和文本视图位于片段布局中,而不是活动布局中,因此您必须将代码移动到onCreateView:

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }

    int counter=0;

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

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

        add.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                counter++;
                display.setText("Counter equals" + counter);
            }
        });

        return rootView;
    }
}
此外,您必须清除onCreate,只需离开:

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

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

我的灵能告诉我,你的“添加”和“子”按钮是碎片布局文件的一部分,而不是活动布局文件(只包括碎片)

因此,移动此代码块:

    add = (Button) findViewById(R.id.bAdd);
    sub = (Button) findViewById(R.id.bSub);
    display = (TextView) findViewById(R.id.tvDisplay);
    add.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            counter++;
            display.setText("Counter equals" + counter);
        }});
到占位符片段的onCreateView方法

或者,您可以让活动的布局包含所有控件,只需去掉占位符片段


片段有它们的优点,但是eclipse生成的片段可能很烦人,特别是对于一个简单的原型练习

发布错误消息发布您的活动\u起点。xml是活动\u起点布局?的一部分,如果不是,您应该先将其充气谢谢。我想我可以用活动的布局来代替。这很好。@user3781123-欢迎使用SO。如果你喜欢这个答案,给它投一票或者点击复选框。这是一个伟大的调整。您必须将final添加到display,或者在onCreateView之外声明display。谢谢你的快速回复。是的,你说得对。你必须加上期末考试。我更新了答案。谢谢我应该得到被接受的答案吗?