繁体   English   中英

Android setOnClickListener无法正常工作

[英]Android setOnClickListener not working

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

}

看来您是在fragment_main.xml而不是activity_main.xml中建立视图的。

首次创建新的android项目时,您会拥有以下这些文件,这些文件会自动创建并打开:

在此处输入图片说明

然后,当您开始时,您可以在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;
    }
}

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

问题不只是将代码移入

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM