it-swarm.dev

android.content.res.Resources $ NotFoundException: ID zdroje kódu # 0x0

Vyvíjím aplikaci pro Android, která čte data z databáze MySQL a čelila jsem této chybě. Mám toto uspořádání XML:

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


        <TextView Android:id="@+id/wardNumber" 
            Android:layout_width="wrap_content" 
            Android:layout_height="wrap_content" 
            Android:layout_marginLeft="3dp" 
            Android:text="Ward Number" 
            Android:textSize="22dp"/> 


        <TextView Android:id="@+id/dateTime" 
            Android:layout_width="wrap_content" 
            Android:layout_height="wrap_content" 
            Android:layout_below="@id/wardNumber" 
            Android:layout_alignParentRight="true" 
            Android:layout_marginRight="3dp" 
            Android:text="Date-Time" />

        <TextView
            Android:id="@+id/name"
            Android:layout_width="wrap_content"
            Android:layout_height="wrap_content"
            Android:layout_alignBaseline="@+id/dateTime"
            Android:layout_alignBottom="@+id/dateTime"
            Android:layout_alignLeft="@+id/wardNumber"
            Android:layout_marginLeft="3dp" 
            Android:text="Name" />

</RelativeLayout> 

A to je můj soubor Java:

public ApplicationAdapter(Context context, List<Application> items) {
    super(context, R.layout.app_custom_list, items);
    this.items = items;
}

@Override
public int getCount() {
    return items.size();
}

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

    if(v == null) {
        LayoutInflater li = LayoutInflater.from(getContext());
        v = li.inflate(R.layout.app_custom_list, null);            
    }

    Application app = items.get(position);

    if(app != null) {
      //  ImageView icon = (ImageView)v.findViewById(R.id.appIcon);
        TextView wardNumber = (TextView)v.findViewById(R.id.wardNumber);
        TextView name = (TextView)v.findViewById(R.id.name);
      //  LinearLayout ratingCntr = (LinearLayout)v.findViewById(R.id.ratingCntr);
        TextView dateTime = (TextView)v.findViewById(R.id.dateTime);

  //      if(icon != null) {
  //          Resources res = getContext().getResources();
   //         String sIcon = "com.sj.jsondemo:drawable/" + app.getIcon();
   //         icon.setImageDrawable(res.getDrawable(res.getIdentifier(sIcon, null, null)));
   //     }

        if(wardNumber != null) wardNumber.setText(app.getTitle());

        if(name != null) name.setText(app.getTitle());

        if(dateTime != null) {
            dateTime.setText(app.getTotalDl());
          //  NumberFormat nf = NumberFormat.getNumberInstance();
          //  dateTime.setText(nf.format(app.getTotalDl())+" dl");            
        }


    }

    return v;
}
}

Při volání této aktivity se zobrazí tato chybová zpráva:

11-24 21:12:03.633:  E/AndroidRuntime(1251):  FATAL EXCEPTION: main
11-24 21:12:03.633:  E/AndroidRuntime(1251): Android.content.res.Resources$NotFoundException: String resource ID #0x0
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.content.res.Resources.getText(Resources.Java:201)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.TextView.setText(TextView.Java:2863)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at com.sj.jsondemo.Appl icationAdapter.getView(ApplicationAdapter.Java:53)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.AbsListView.obtainView(AbsListView.Java:1430)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.ListView.measureHeightOfChildren(ListView.Java:1216)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.ListView.onMeasure(ListView.Java:1127)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.View.measure(View.Java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:3138)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.Java:1017)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.LinearLayout.measureHorizontal(LinearLayout.Java:701)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.LinearLayout.onMeasure(LinearLayout.Java:311)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.View.measure(View.Java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:3138)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.FrameLayout.onMeasure(FrameLayout.Java:250)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.View.measure(View.Java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.LinearLayout.measureVertical(LinearLayout.Java:531)
11-24 21:12:03.633: E/AndroidRuntime(1251):     at Android.widget.LinearLayout.onMeasure(LinearLayout.Java:309)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.View.measure(View.Java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at  Android.view.ViewGroup.measureChildWithMargins(ViewGroup.Java:3138)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.widget.FrameLayout.onMeasure(FrameLayout.Java:250)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.View.measure(View.Java:8313)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.ViewRoot.performTraversals(ViewRoot.Java:839)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.view.ViewRoot.handleMessage(ViewRoot.Java:1859)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.os.Handler.dispatchMessage(Handler.Java:99)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.os.Looper.loop(Looper.Java:130)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Android.app.ActivityThread.main(ActivityThread.Java:3683)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Java.lang.reflect.Method.invokeNative(Native Method)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at Java.lang.reflect.Method.invoke(Method.Java:507)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at com.Android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.Java:839)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at com.Android.internal.os.ZygoteInit.main(ZygoteInit.Java:597)
11-24 21:12:03.633:  E/AndroidRuntime(1251):    at dalvik.system.NativeStart.main(Native Method)
11-24 21:12:08.922: I/Process(1251): Sending signal. PID: 1251 SIG: 9

Nevím, jak tuto chybu opravit.

123
user2881604

Změna

dateTime.setText(app.getTotalDl());

Na

dateTime.setText(String.valueOf(app.getTotalDl()));

Existují různé verze setText - jeden vezme String a jeden vezme id ID zdroje. Pokud ji předáte celé číslo, bude se snažit vyhledat odpovídající zdrojový řetězec, který nemůže najít, což je vaše chyba.

Myslím, že app.getTotalDl() vrací int. Musíte specificky říct setText nastavit ji na hodnotu String tohoto int.

setText (int resid) vs setText (text CharSequence)

405
Ken Wolf

Nahradit

dateTime.setText(app.getTotalDl());

S

dateTime.setText(""+app.getTotalDl());
10
Soni Kumar

Při pokusu o nastavení textu v Edittext nebo textview byste měli Předat pouze formát String.

dateTime.setText(app.getTotalDl());

na

dateTime.setText(String.valueOf(app.getTotalDl()));
0

Vyhodnocená hodnota pro settext byla integer, takže šel vidět zdroj, který je k němu připojen, ale nebyl nalezen, chcete nastavit text tak, aby byl řetězec tak převést celé číslo na řetězec připojením .toStringe nebo String.valueOf(int) vyřeší váš problém!

0
blackHawk