问题
I am Creating an ArrayAdapter's Super class in which I am binding the data I receive from the object and put them into the TextViews.
The code is not any complicated and I have double checked the ids of all the TextViews in my XML.
And I have checked all the other questions with the same title.
None of them seem to work in this case.
Attaching my code below.
public class EarthquakeAdapter extends ArrayAdapter<Earthquake>
{
public EarthquakeAdapter(Context context, int resource) {
super(context, resource);
}
public EarthquakeAdapter(Activity activity, ArrayList<Earthquake> Earthquakes)
{
super(activity,0, (List<Earthquake>) Earthquakes);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
super.getView(position, convertView, parent);
View ListItemView = convertView;
if(ListItemView == null)
{
ListItemView = LayoutInflater.from(getContext()).inflate(
R.layout.earthquake_list_item, parent, false);
}
Earthquake currentItem = getItem(position);
TextView magnitude = (TextView)
ListItemView.findViewById(R.id.magnitude);
//Double m = ;
magnitude.setText(""+currentItem.getmMag());
TextView location = (TextView) ListItemView.findViewById(R.id.Location);
location.setText(currentItem.getmLocation());
TextView date = (TextView) ListItemView.findViewById(R.id.date);
date.setText(currentItem.getmDate());
return ListItemView;
}
and then there is the xml file.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:id="@+id/list_view_container">
<TextView
android:id="@+id/magnitude"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
tools:text="7.6"/>
<TextView
android:id="@+id/Location"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
tools:text="San Fransisco"/>
<TextView
android:id="@+id/date"
android:layout_width="0dp"
android:layout_height="60dp"
android:layout_weight="1"
tools:text="Date"/>
</LinearLayout>
This is my Earthquake Class.
public class Earthquake
{
private double mMag;
private String mLocation;
private String mDate;
public Earthquake(double Mag, String Location, String Date){
mMag = Mag;
mLocation = Location;
mDate = Date;
}
public String getmDate() {
return mDate;
}
public double getmMag() {
return mMag;
}
public String getmLocation() {
return mLocation;
}
}
and This is the Error LOG.
06-11 20:38:36.570 9359-9359/com.example.android.quakereport W/ResourceType: No package identifier when getting value for resource number 0x00000000 06-11 20:38:36.571 9359-9359/com.example.android.quakereport D/AndroidRuntime: Shutting down VM 06-11 20:38:36.581 9359-9359/com.example.android.quakereport E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.android.quakereport, PID: 9359
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1369)
at android.content.res.MiuiResources.getValue(MiuiResources.java:145)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2824)
at android.content.res.Resources.getLayout(Resources.java:1183)
at android.view.LayoutInflater.inflate(LayoutInflater.java:425)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:378)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
at com.example.android.quakereport.EarthquakeAdapter.getView(EarthquakeAdapter.java:28)
at android.widget.AbsListView.obtainView(AbsListView.java:2346)
at android.widget.ListView.makeAndAddView(ListView.java:1876)
at android.widget.ListView.fillDown(ListView.java:702)
at android.widget.ListView.fillFromTop(ListView.java:763)
at android.widget.ListView.layoutChildren(ListView.java:1685)
at android.widget.AbsListView.onLayout(AbsListView.java:2148)
at android.view.View.layout(View.java:16653)
at android.view.ViewGroup.layout(ViewGroup.java:5438)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16653)
at android.view.ViewGroup.layout(ViewGroup.java:5438)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
at android.view.View.layout(View.java:16653)
at android.view.ViewGroup.layout(ViewGroup.java:5438)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at android.view.View.layout(View.java:16653)
at android.view.ViewGroup.layout(ViewGroup.java:5438)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
at android.view.View.layout(View.java:16653)
at android.view.ViewGroup.layout(ViewGroup.java:5438)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2680)
at android.view.View.layout(View.java:16653)
at android.view.ViewGroup.layout(ViewGroup.java:5438)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2198)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1958)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1134)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6045)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:860)
at android.view.Choreographer.doCallbacks(Choreographer.java:672)
at android.view.Choreographer.doFrame(Choreographer.java:608)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:846)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5441)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:738)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:628)
回答1:
The problem is in the getView() method of EarthquakeAdapter class the statement super.getView() was causing some issue. I removed it and its working fine now.
Refer to the comment of @MikeM in the question itself.
Remove the super call in getView(). You've passed an invalid layout resource ID in the three-argument constructor - which is leading to that Exception when ArrayAdapter tries to inflate it - and you're not using the super return anyway, since you're inflating the layout yourself
回答2:
I think you need to fix your constructors. You only need one.
public class EarthquakeAdapter extends ArrayAdapter<Earthquake> {
public EarthquakeAdapter(Context context, ArrayList<Earthquake> earthquakes) {
super(context, 0, earthquakes);
}
And you should not need to call super.getView
@Override
public View getView(int position, View convertView, ViewGroup parent) {
final Earthquake earthquake = getItem(position);
View v = convertView;
if(v == null) {
v = LayoutInflater.from(getContext()).inflate(
R.layout.earthquake_list_item, parent, false);
}
return v;
}
Otherwise, you need to determine what is happening at line 28 and what is zero valued there.
You may follow this example to define custom adapter
You may need to clean and rebuild the project to regenerate your resources.
回答3:
Try this
Earthquake currentItem = new Earthquake(3.33,"someplace","05-05-05")
currentItem=getItem(position);
in place of
Earthquake currentItem = get(position); `//getItem(position);`
if it work you need to set values of the variables declared in Earthquake class
This might solve your problem Declare member variables
Context ctx;
ArrayList<Earthquake> Earthquake;
public EarthquakeAdapter(Activity activity, ArrayList<Earthquake> Earthquakes)
{
super(activity,0, (List<Earthquake>) Earthquakes);
Ctx=activity;
Earthquake=this.Earthquake;
}
now where you are doing Earthquake currentItem = getItem(position);
do this
Earthquake currentItem=Earthquake.get(position); `//getItem(position);`
basically you are not passing values received by EarthquakeAdapter.
Also check in this
public Object getItem(int position) {
return Earthquake.get(position);
}
public int getCount() {
return Earthquake.size();
}
public long getItemId(int position) {
return position;
}
If this does not work show the code where you are setting the ArrayList<Earthquake>
and calling adapter public EarthquakeAdapter(Activity activity, ArrayList<Earthquake> Earthquakes)
来源:https://stackoverflow.com/questions/44486096/android-content-res-resourcesnotfoundexception-resource-id-0x0-android-error