2013-04-11 34 views
0

一切正常,在我的代碼罰款,直到我把這裏的其他代碼的任何位置顯示此代碼來獲取XML頁腳佈局內訪問的TextView:如何訪問腳註xml佈局內的視圖? Android的

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

total_text是位於XML一個TextView我用作頁腳的文件。

這個腳本xml佈局命名爲alternative.xml,它顯示在ListView的底部,其中填充了由適配器使用不同xml佈局填充的其他項目。該頁腳佈局文件中唯一的內容是名爲total_text的TextView。

我該如何訪問這個TextView叫做total_text裏面的頁腳xml佈局?

下面是從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類的內部一個ArrayAdapter類

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); 

代碼:

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> 
+0

發佈您的alternative.xml代碼 – 2013-04-11 08:05:35

+0

是'totalPrice'字符串或int? – Vladimir 2013-04-11 08:05:47

+0

totalPrice是一個int – Kevik 2013-04-11 08:09:03

回答

2

根據堆棧跟蹤,它看起來像你試圖呼叫totalTextView.setText()傳遞int參數。如果是這種情況,參數是treated as a resource id。你可能需要使用

totalTextView.setText(Integer.toString(totalPrice)); 
+0

是的!這是問題,現在它起作用了!!!!!!! – Kevik 2013-04-11 08:11:42

+0

或只是'totalTextView.setText(totalPrice +「」);' – 2013-04-11 08:13:18

+1

@almaz_from_kazan:並給垃圾收集器更多工作要做? – 2013-04-11 09:26:56

1

這應該對你有用,基本上TextViews的setText方法需要一個String。

totalTextView.setText(String.valueOf((totalPrice));