簡體   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