0

在我的代码中一切正常,直到我将此处显示的这段代码放在其他代码中的任何位置以访问 xml 页脚布局中的 TextView:

  TextView totalTextView = (TextView) footer.findViewById(R.id.total_text);
  totalTextView.setText(totalPrice);

total_text是一个 TextView,它位于我用作页脚的 xml 文件中。

这个名为alternative.xml 的页脚xml 布局显示在ListView 的底部,其中填充了适配器使用不同的xml 布局填充的其他项目。这个页脚布局文件中唯一的东西是名为total_text.

如何在页脚 xml 布局中访问这个TextView调用?total_text

这是来自 Logcat 的堆栈跟踪:

FATAL EXCEPTION`: main
java.lang.RuntimeException: Unable to start activityComponentInfo{com.forever.scrollcheck/com.forever.scrollcheck.MainActivity}:
android.content.res.Resources$NotFoundException: String resource ID #0x543a8
    E/AndroidRuntime(8208): at adroid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
    E/AndroidRuntime(8208): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
    E/AndroidRuntime(8208): at android.app.ActivityThread.access$600(ActivityThread.java:123)
    E/AndroidRuntime(8208): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
    E/AndroidRuntime(8208): at android.os.Handler.dispatchMessage(Handler.java:99)
    E/AndroidRuntime(8208): at android.os.Looper.loop(Looper.java:137)
    E/AndroidRuntime(8208) :at android.app.ActivityThread.main(ActivityThread.java:4424)
    E/AndroidRuntime(8208): at java.lang.reflect.Method.invokeNative(Native Method)
    E/AndroidRuntime(8208): at java.lang.reflect.Method.invoke(Method.java:511)
    E/AndroidRuntime(8208): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)

这是 Activity 类中的代码:

 listView = (ListView) findViewById(R.id.listView);

  View footer = getLayoutInflater().inflate(R.layout.alternative, null);

  TextView totalTextView = (TextView) footer.findViewById(R.id.total_text);
  totalTextView.setText(totalPrice);

  listView.addFooterView(footer); 

  MobileArrayAdapter adapter = new MobileArrayAdapter(this, R.layout.row_layout, ckbInfo);
  listView.setAdapter(adapter);

ArrayAdapter 类中嵌套在 Activity 类中的代码:

  public class MobileArrayAdapter extends ArrayAdapter<CheckBoxInfo>{
      CheckBoxInfo[] objects;
      Context context;
      int textViewResourceId;

    public MobileArrayAdapter(Context context, int textViewResourceId,
    CheckBoxInfo[] objects) {
       super(context, textViewResourceId, objects);
       this.context = context;
       this.textViewResourceId = textViewResourceId;
       this.objects = objects;

       }

 @Override
 public View getView(final int position, View convertView, ViewGroup parent) {
 View row_layout_view = convertView;

 if ((row_layout_view == null)){


  LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  row_layout_view = inflater.inflate(R.layout.row_layout, null);
            }               
 //CheckBoxInfo item = objects.get(position);  // for arrayList
 CheckBoxInfo item = objects[position];

 if(item != null){

 TextView textView = (TextView) row_layout_view.findViewById(R.id.textView1);
 final CheckBox checkBox = (CheckBox) row_layout_view.findViewById(R.id.checkBox1);
 TextView priceTextView = (TextView) row_layout_view.findViewById(R.id.price1);

 checkBox.setOnClickListener(new OnClickListener() {

 @Override
    public void onClick(View arg0) {
    final boolean isChecked = checkBox.isChecked();
    if(isChecked==true){
 Toast.makeText(MainActivity.this,"box is checked for position " + position, Toast.LENGTH_SHORT).show();
 }else if(isChecked==false){
 Toast.makeText(MainActivity.this,"box is NOT checked for " + position, Toast.LENGTH_SHORT).show();
     }
   }
      });

   if(item !=null){
      textView.setText(item.checkBoxName);
      checkBox.setChecked(item.checkBoxState);
      priceTextView.setText(String.valueOf(item.price));
    }
      }
    return row_layout_view;
    }

  }

编辑:这里要求的是来自alternative.xml的代码:

  <?xml version="1.0" encoding="utf-8"?>
  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  android:orientation="vertical"
  android:gravity="center" >

  <TextView
    android:id="@+id/total_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="XXXXXX"
    android:textAppearance="?android:attr/textAppearanceLarge" />

</LinearLayout>
4

2 回答 2

2

根据堆栈跟踪,您似乎正在尝试调用totalTextView.setText()传递int参数。如果是这种情况,则参数被视为资源 id。你可能需要使用

totalTextView.setText(Integer.toString(totalPrice));
于 2013-04-11T08:09:20.320 回答
1

这应该为您解决问题,基本上 TextViews setText 方法需要一个字符串。

totalTextView.setText(String.valueOf((totalPrice));
于 2013-04-11T08:13:29.217 回答