-1

我正在务实地创建一些按钮。使用来自 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);
        }
    }
}
}
4

1 回答 1

3

你还没有初始化myListener,没有什么可以触发的……哎呀 :)

于 2012-11-13T20:53:59.267 回答