尝试在当前位置设置标记会给我nullpointer异常

时间:2014-07-22 06:34:53

标签: android button background-color

我在程序中做了以下事情:

  1. 我在Buttons课程中以编程方式生成了一些MenuItemsActivity。我在Listview课程的xml中有一个MenuItemsActivity

  2. 当我点击按钮时,Listview中会加载相应的内容。我只是刷新活动,即我使用相同的Listview根据点击的按钮加载不同的内容。

  3. 我想做以下事情:

    1. 单击按钮时,我想将按钮的背景更改为' blue_tab`,并在重新加载相同的活动时保持相同的颜色。任何人都可以指导我一步一步做什么,因为我是Android的新手。

        i = getIntent();
      String Salad=i.getStringExtra("Salad");
      String cat_name_from_fragment=i.getStringExtra("category name");
      final ListView salad_list = (ListView) findViewById(R.id.salads);
      
      category = new ArrayList<HashMap<String, String>>();
      items = new ArrayList<HashMap<String, String>>();
      db = new DbHelper(MenuItemsActivity.this);
      category.clear();
      if (i.getStringExtra("category name") != null) {
          String getcategory = i.getStringExtra("category name").toString();
          items = db.retrieve_item_details(getcategory);
          Log.i("sub items", "" + items);
      }
      else if(cat_name_from_fragment!=null)
      {
          items = db.retrieve_item_details(cat_name_from_fragment);   
      }
      else
      {
          items = db.retrieve_item_details("Salads");
      }
      category = db.retrieve_category_name();
      
      
      count = category.size();
      Log.i("Sqlite database values", "" + count + " " + category);
      if (android.os.Build.VERSION.SDK_INT > 9) {
          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
                  .permitAll().build();
      
          StrictMode.setThreadPolicy(policy);
      }
      
      LinearLayout l1 = (LinearLayout) findViewById(R.id.tableRow1);
      int i = 0;
      for (HashMap<String, String> map : category)
      
          for (Entry<String, String> mapEntry : map.entrySet()) {
      
              String key = mapEntry.getKey();
              String value = mapEntry.getValue();
              TextView tv2 = new TextView(this);
              tv2.setLayoutParams(new LinearLayout.LayoutParams(40, 90));
              Log.i("map", "" + value);
              final Button tv1 = new Button(this);
              tv1.setId(i);
              tv1.setText(value);
              tv1.setTextSize(35);
      
              tv1.setTextColor(Color.parseColor("#1569C7"));
              tv1.setGravity(Gravity.CENTER);
              tv1.setBackgroundDrawable(getResources().getDrawable(R.drawable.popup));
              tv1.setLayoutParams(new LinearLayout.LayoutParams(300,90));
              tv1.setOnClickListener(new View.OnClickListener() {
      
                  @Override
                  public void onClick(View v) {
                      // TODO Auto-generated method stub
                      String text = tv1.getText().toString();
                      Log.e("text message", "" + text);
                      tv1.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_tab));
                      Toast.makeText(MenuItemsActivity.this, "clicked", 1000)
                              .show();
                      Intent i = new Intent(MenuItemsActivity.this,
                              MenuItemsActivity.class);
                      i.putExtra("category name", "" + text);
                      finish();
                      startActivity(i);
                  }
              });
              /*TextView tv2 = new TextView(this);
      
              tv2.setText("      ");
              tv2.setTextSize(10);
      
              tv2.setGravity(Gravity.CENTER);*/
              l1.addView(tv1);
              l1.addView(tv2);
      
              i++;
              Log.e("i count ", "" + i);
          }
      final int imageArra[] = { R.drawable.leftbar_logo ,R.drawable.leftbar_logo};
      
      
      ListAdapter k = new SimpleAdapter(MenuItemsActivity.this, items,
              R.layout.menulist, new String[] { "Item_Name", "Desc",
                      "Currency", "Price","url","veggie","cat" }, new int[] { R.id.cat_name,
                      R.id.textView1, R.id.textView2, R.id.textView3,R.id.url,R.id.veggie,R.id.Category}) {
      
          @Override
          public View getView(int position, View convertView, ViewGroup parent) {
              // TODO Auto-generated method stub
              final View v = super.getView(position, convertView, parent);
      
              final ImageView im=(ImageView)v.findViewById(R.id.imageView1);
              TextView url=(TextView)v.findViewById(R.id.url);
      
              TextView veg=(TextView)v.findViewById(R.id.veggie);
              String vegg=veg.getText().toString();
      
              ImageView imagevegs=(ImageView)v.findViewById(R.id.veggies);
              Log.i("veggie",""+vegg);
              if(vegg.compareToIgnoreCase("Veg")==0)
              {
      
      
                  imagevegs.setImageResource(R.drawable.veg);
                  imagevegs.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
              }
              else
              {imagevegs.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
                  imagevegs.setImageResource(R.drawable.non);
              }
      
              final String urls="http://166.62.17.208/"+url.getText().toString();
              Log.i("urls",""+urls);
      
              imageLoader.DisplayImage(urls,im);
              //return super.getView(position, convertView, parent);
              return v;
          }
      
      };
      salad_list.setAdapter(k);
      

1 个答案:

答案 0 :(得分:0)

您可以使用PreferenceManager保存数据,并在重新加载/重新启动应用时使用它

<强>样品:

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            String text = tv1.getText().toString();
            Log.e("text message", "" + text);
            if(PreferenceManager.getDefaultSharedPreferences(MenuItemsActivity.this).getString("button", "").length != 0)
                tv1.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_tab));
            else
            {
                Editor editor = PreferenceManager.getDefaultSharedPreferences(MenuItemsActivity.this).edit();
                editor.putString("button", "1");
                editor.commit(); 
                tv1.setBackgroundDrawable(getResources().getDrawable(R.drawable.blue_tab)); 
            }
            Toast.makeText(MenuItemsActivity.this, "clicked", 1000)
                    .show();
            Intent i = new Intent(MenuItemsActivity.this,
                    MenuItemsActivity.class);
            i.putExtra("category name", "" + text);
            finish();
            startActivity(i);
        }