为什么setOnClick会崩溃这个应用程序?

时间:2014-06-26 22:19:23

标签: java android crash

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

2 个答案:

答案 0 :(得分:0)

您的按钮和textview采用片段布局,而不是活动布局,因此您必须将代码移至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();
    }
}

答案 1 :(得分: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);
        }});

到PlaceholderFragment的onCreateView方法。

或者,您可以让Activity的布局包含所有控件,然后删除占位符片段。

< views>片段有其优点,但eclipse生成的样板文件PlaceholderFragment可能很烦人,特别是对于简单的原型制作练习。 < /&意见GT;