[已解决]findviewbyid android.content.res.Resources$NotFoundException

来源:互联网 发布:灌篮高手 知乎 编辑:程序博客网 时间:2024/05/29 11:50

setText的时候报错,显示android.content.res.Resources$NotFoundException,

这个是因为setText的时候里面的是数字了,而数字setText默认的认为就是id了,所以例如我这里是setText(2),那么它会去找2的这个id的,而不是直接设置文字为2

应该是setText("2");这样就可以了

就是说setText里面最好是字符串



参考:http://cache.baiducontent.com/c?m=9f65cb4a8c8507ed4fece76310579135480ddd276b97844b22918448e435061e5a25a4ec66644b598f84616406a44f5decf4366f6d5e65a09bbed91981ac925e749f2742234dd71653920eafbd007f9637902cadf44ea2fbe732e5f58e92840204dd537327d4e78b2c5303ca19a4496ca5f1d95f15295ae5b3276589072462c26406a1478fb36e3847dcad8b48099e6a866e4bcaea22b04f43e64fb31f6a7f53a03de259007171a158278e02675385eb2ca3712e0562a148c2e29df7b01edbcbea40c5aedcaf5ec677e6b4bea42c042712ed25cfe1e2ec2a64345c8a81835182&p=ce67c64ad4934eac52e7883d5a4a95&newp=8b2a975f97af11a05be79e6f59408c231601d13523808c0a3e83fe48996e5155113d8eff7062515f8e99736300a84856e8f33371300327bd9dc38a40d7b8866e42c970767f4bda17089275&user=baidu&fm=sc&query=How+can+I+access+the+views+inside+of+a+footer+xml+layout%3F&qid=a8f2c5b70000b8d5&p1=1




How can I access the views inside of a footer xml layout? Android

up vote 0 down vote favorite

Everything works fine in my code until I put this code shown here anywhere in the other code to get access to the TextView inside the xml footer layout:

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

total_text is a TextView that is located in the xml file that I use as a footer.

This footer xml layout named, alternative.xml shows up at the bottom of the ListView that is populated with other items filled by the adapter using a different xml layout. the only thing inside this footer layout file is the TextView named total_text.

How can I access this TextView called total_text inside the footer xml layout?

Here is the stacktrace from Logcat:

FATAL EXCEPTION`: mainjava.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)

Here is the code in the Activity class:

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

code in the ArrayAdapter class that is nested inside of the Activity class:

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

EDIT: As requested here is the code from 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>
share|improve this question
 
 
post your alternative.xml code – Sankar V Apr 11 '13 at 8:05
 
is totalPrice String or int? – Vladimir Apr 11 '13 at 8:05
 
totalPrice is an int – Kevik Apr 11 '13 at 8:09

2 Answers

active oldest votes
up vote 2 down voteaccepted

According to stack trace, it looks like you are trying to call totalTextView.setText() passing int argument. If this is the case, argument is treated as a resource id. You probably need to use

totalTextView.setText(Integer.toString(totalPrice));
share|improve this answer
 
 
YES!!!! that is the problem, it works now!!!!!!! – Kevik Apr 11 '13 at 8:11
 
or just totalTextView.setText(totalPrice+""); – almaz_from_kazan Apr 11 '13 at 8:13
1 
@almaz_from_kazan: and give the garbage collector more work to do? – MH. Apr 11 '13 at 9:26
up vote 1 down vote

This should do the trick for you, basically the TextViews setText method requires a String.

totalTextView.setText(String.valueOf((totalPrice));
share|improve this answer
 
 


How can I access the views inside of a footer xml layout? Android

up vote 0 down vote favorite

Everything works fine in my code until I put this code shown here anywhere in the other code to get access to the TextView inside the xml footer layout:

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

total_text is a TextView that is located in the xml file that I use as a footer.

This footer xml layout named, alternative.xml shows up at the bottom of the ListView that is populated with other items filled by the adapter using a different xml layout. the only thing inside this footer layout file is the TextView named total_text.

How can I access this TextView called total_text inside the footer xml layout?

Here is the stacktrace from Logcat:

FATAL EXCEPTION`: mainjava.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)

Here is the code in the Activity class:

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

code in the ArrayAdapter class that is nested inside of the Activity class:

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

EDIT: As requested here is the code from 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>
share|improve this question
 
   
post your alternative.xml code – Sankar V Apr 11 '13 at 8:05
   
is totalPrice String or int? – Vladimir Apr 11 '13 at 8:05
   
totalPrice is an int – Kevik Apr 11 '13 at 8:09

2 Answers

active oldest votes
up vote 2 down voteaccepted

According to stack trace, it looks like you are trying to call totalTextView.setText() passing int argument. If this is the case, argument is treated as a resource id. You probably need to use

totalTextView.setText(Integer.toString(totalPrice));
share|improve this answer
 
   
YES!!!! that is the problem, it works now!!!!!!! – Kevik Apr 11 '13 at 8:11
   
or just totalTextView.setText(totalPrice+""); – almaz_from_kazan Apr 11 '13 at 8:13
1 
@almaz_from_kazan: and give the garbage collector more work to do? – MH. Apr 11 '13 at 9:26
up vote 1 down vote

This should do the trick for you, basically the TextViews setText method requires a String.

totalTextView.setText(String.valueOf((totalPrice));
share|improve this answer
 
 

0 0