OnClickListener没有触发

时间:2012-11-13 20:50:45

标签: android

我正在用实际创建一些按钮。使用来自2个不同活动的相同代码,按钮触发不会对其中一个活动起作用。对于我必须忽视的事情,我感到有些困惑。

public class Home_Page_Activity extends Activity {

JSONArray nameArray;
JSONArray valArray;
String[] arrayButtonTitles;
String font;
Context context = null;
LinearLayout.LayoutParams params_textview;
LinearLayout.LayoutParams params_button;
LinearLayout.LayoutParams params_linearlayout;
LinearLayout home_page_layout;
OnClickListener myListener;
Typeface face;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home_page_activity);
    WebView wv_Welcome_Message = (WebView) findViewById(R.id.wv_Welcome_Message);

    context = getApplicationContext();
    params_linearlayout = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
    params_linearlayout.gravity = Gravity.CENTER;
    // Set Font
    font = "fonts/aescrawl.ttf";
    face = Typeface.createFromAsset(getAssets(), font);

    // Reference Layout
    home_page_layout = (LinearLayout) findViewById(R.id.home_page_layout);

    // Set Layout Parameters for XML Elements. (Height, Width,
    // Weight,Padding)
    params_button = new LinearLayout.LayoutParams(
            LinearLayout.LayoutParams.MATCH_PARENT, 32, 1.0f);
    params_button.setMargins(10, 0, 10, 30);

    // Load webview if there is connectivity
    if (Repeatables.isNetworkAvailable(context) == true) {
        new InitTask().execute();
    } else {
        wv_Welcome_Message.setVisibility(View.GONE);
    }

    String[] arrayButtonTitles = { "Sub 1", "Sub 2", "Final 1", "Final 2",
            "Category 5", "Category 6", "Category 7", "Category 8",
            "Category 9", "Category 10" };
    Load_Main_Menu(arrayButtonTitles);

myListener = new OnClickListener() {
          public void onClick(View v) {
            Object tag = v.getTag();
            // Do something depending on the value of the tag

                Log.e("TAG","Button "+tag+" pushed");
              if("0".equals(tag.toString())){
                  Log.e("TAG","Hello 1");
              }else  if("21".equals(tag.toString())){
                    Intent i = new Intent(getApplicationContext(), Sub_Menu_Categories_Main_Activity.class);
                    i.putExtra("whatSubMenuToLoad","none");
                    i.putExtra("whatScreenTypeToLoad","FINALCATEGORY");
                    i.putExtra("categoryId","313");
                    startActivity(i);
              }
          }
        };

}

// Build Home Page XML Layout
public void Load_Main_Menu(String[] buttons) {

    arrayButtonTitles = buttons;
    int numberOfButtons = arrayButtonTitles.length;
    LinearLayout[] ll = new LinearLayout[numberOfButtons];

    // Add Elements to Layout
    for (int i = 0; i < numberOfButtons; i++) {

        Button btn = new Button(this);
        btn.setText(arrayButtonTitles[i]);
        btn.setTag(i);
        btn.setOnClickListener(myListener);
        btn.setTypeface(face);
        btn.setTextSize(18);
        btn.setTextColor(context.getResources().getColor(
                R.color.all_button_font_color));
        btn.setBackgroundResource(R.drawable.category_button_background_shape);
        if (i % 2 == 0) {
            ll[i] = new LinearLayout(this);
            ll[i].setOrientation(LinearLayout.HORIZONTAL);
            ll[i].addView(btn, params_button);
            home_page_layout.addView(ll[i], params_linearlayout);
        } else {
            ll[i - 1].addView(btn, params_button);
        }
    }
}
}

1 个答案:

答案 0 :(得分:3)

你还没有初始化myListener,没有什么可以解雇......哎呀:)