Issue
I am using the tabbed activity template for this android project, and each tab initializes a different fragment, most of which contain ListViews and when a row is tapped, a detail fragment appears. When I press one of the tabs, I get the following error:
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1266)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:2649)
at android.content.res.Resources.getLayout(Resources.java:1082)
at android.view.LayoutInflater.inflate(LayoutInflater.java:412)
at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:371)
at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
at android.widget.AbsListView.obtainView(AbsListView.java:2347)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2151)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutHorizontal(LinearLayout.java:1692)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1468)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v4.view.ViewPager.onLayout(ViewPager.java:1627)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.support.v7.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:437)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1703)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1557)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1466)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15671)
at android.view.ViewGroup.layout(ViewGroup.java:5038)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5885)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5257)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
This does not show me the spot within the code that it crashed or what part of my code is the problem. After logging at the beginning and end of a few functions, I know that the crash is not happening within onCreateView or onCreateOptionsMenu. Based on this crash report, I believe the issue is in the custom adapter that I have here:
public class LinksAdapter extends ArrayAdapter<Link> {
public LinksAdapter(Context context, ArrayList<Link> values) {
super(context, R.layout.rowlayout, values);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
G.LOGGER.log(Level.INFO, "getView");
LayoutInflater inflater = LayoutInflater.from(getContext());
View view;
G.LOGGER.log(Level.INFO, "Creation of row started");
if (convertView == null) {
view = inflater.inflate(R.layout.rowlayout, parent, false);
} else {
view = convertView;
}
Link link = getItem(position);
TextView titleTextview = (TextView) view.getRootView().findViewById(R.id.row_title);
titleTextview.setText(link.getTitle());
TextView subtitleTextview = (TextView) view.getRootView().findViewById(R.id.row_subtitle);
subtitleTextview.setText(link.getUrl());
G.LOGGER.log(Level.INFO, "Creation of row ended");
return view;
}
}
A Link is a simple class with getters and setters for 2 properties: Title and Url, both Strings. I am not even sure that the issue is with this adapter. I am wondering if there is a way to figure out where the app crashed. If more code is needed, just let me know.
EDIT: here is rowlayout.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/selector_rowlayout">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/row_title"
android:textSize="20sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/row_subtitle"
android:textSize="15sp"/>
</LinearLayout>
and the @drawable/selector_rowlayout:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_activated="true" android:drawable="@color/gray_light" />
<item android:state_focused="true" android:drawable="@color/gray_light" />
<item android:drawable="@android:color/transparent" />
</selector>
and the entry in strings.xml for gray_light:
<color name="gray_light">#D8D8D8</color>
here is LinksMasterFragment, which uses LinksAdapter:
public class LinksMasterFragment extends MyFragment implements AdapterView.OnItemClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_links_master, container, false);
LinksAdapter adapter = new LinksAdapter(this.getContext(), getUser().getLinks());
ListView listView = (ListView) view.getRootView().findViewById(R.id.links_master_listview);
G.LOGGER.log(Level.INFO, "links: " + getUser().getLinks());
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
setHasOptionsMenu(true);
G.LOGGER.log(Level.INFO, "created");
return view;
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
inflater.inflate(R.menu.menu_links, menu);
super.onCreateOptionsMenu(menu, inflater);
G.LOGGER.log(Level.INFO, "options menu created");
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.action_links_restoredefaults) {
ArrayList<Link> defaultLinks = new ArrayList<>();
defaultLinks.add(new Link("St. Matthew's Facebook", "http://www.facebook.com/StMatthewSchoolCCSD"));
defaultLinks.add(new Link("St. Matthew's Twitter", "http://twitter.com/StMatthew_CCSD"));
defaultLinks.add(new Link("English Dictionary", "http://dictionary.reference.com"));
defaultLinks.add(new Link("Bescherelle", "http://conjugaison.com"));
defaultLinks.add(new Link("French-English Dictionary", "http://mobile-dictionary.reverso.net/english-french/"));
defaultLinks.add(new Link("Google Translate", "http://translate.google.com"));
getUser().setLinks(defaultLinks);
G.LOGGER.log(Level.INFO, "Links: " + (getUser().getLinks() == defaultLinks));
ListView listView = (ListView)getView().getRootView().findViewById(R.id.links_master_listview);
listView.setAdapter(new LinksAdapter(getContext(), getUser().getLinks()));
}
return false;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// push to detail with selected homework
G.Links.index = position;
LinksDetailFragment fragment = new LinksDetailFragment();
fragment.setSchedule(getSchedule());
fragment.setUser(getUser());
getActivity().getSupportFragmentManager().beginTransaction().replace(R.id.links_detail_container, fragment).commit();
}
}
The Associated xml file is fragment_links_master:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fffbe14d">
<ListView
android:layout_width="200dp"
android:layout_height="match_parent"
android:id="@+id/links_master_listview">
</ListView>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/links_detail_container">
</FrameLayout>
</LinearLayout>
Here is MyFragment, LinksMasterFragment's superclass.
public class MyFragment extends Fragment {
private User user;
public MyFragment() {}
public Context getContext() {
return getActivity().getBaseContext();
}
public User getUser() {
if (user != null) {
return user;
}
return new User(getContext(), "");
}
}
and the User class, where all persistent user data is accessed.
public class User extends DatabaseHelper {
public User(Context context, String id) {
super(context);
this.id = id;
Cursor res = db.query(TABLE_USERS, new String[] {"Name"}, "ID = ?", new String[] {id}, null, null, null);
G.LOGGER.log(Level.INFO, "Count: " + res.getCount());
if (res.getCount() == 0) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("Name", "");
contentValues.put("Links", "");
db.insert(TABLE_USERS, null, contentValues);
setLinks(new ArrayList<Link>() {
});
}
res.close();
}
public ArrayList<Link> getLinks() {
ArrayList<Link> emptyArray= new ArrayList<>();
Cursor cursor = db.query(TABLE_USERS, new String[]{COL_LINKS}, "ID = ?", new String[]{id}, null, null, null);
cursor.moveToFirst();
if (cursor.getCount() == 0) {
return emptyArray;
}
String string = cursor.getString(0);
cursor.close();
Type type = new TypeToken<ArrayList<Link>>(){}.getType();
ArrayList<Link> finalArray = new Gson().fromJson(string, type);
G.LOGGER.log(Level.INFO, "Links: " + finalArray);
if (finalArray != null && finalArray.size() != 0) {
return finalArray;
} else {
return emptyArray;
}
}
}
P.S. I am actually quite new to Android, as I am mainly an iOS Developer, so if there are conventions that I am not following, let me know.
Solution
I solved this issue by adding the getView() method to a different rowlayout class. Not sure why it worked, but it did.
Answered By - RedHoodie_
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.