I'm trying to write a simple Phonebook Android app, and when I try to run it, it gives me a bunch of errors, and I really don't have any idea where to look for my mistakes. All I'm trying to get it to do right now, is read in a list of names from a file, and display it in a ListView. This is the code:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listview = (ListView) findViewById(R.id.listView1);
final ArrayList<String> names = new ArrayList<String>();
try
{
Scanner sc = new Scanner(getAssets().open(String.format("data.txt")));
while(sc.hasNextLine())
{
names.add(sc.nextLine());
}
} catch (IOException ioe) { System.out.println("Failed to load file"); }
final StableArrayAdapter adapter = new StableArrayAdapter(this,
R.id.listView1, names);
listview.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private class StableArrayAdapter extends ArrayAdapter<String> {
HashMap<String, Integer> mIdMap = new HashMap<String, Integer>();
public StableArrayAdapter(Context context, int textViewResourceId,
List<String> objects) {
super(context, textViewResourceId, objects);
for (int i = 0; i < objects.size(); ++i) {
mIdMap.put(objects.get(i), i);
}
}
@Override
public long getItemId(int position) {
String item = getItem(position);
return mIdMap.get(item);
}
@Override
public boolean hasStableIds() {
return true;
}
}
}
And here is the Logcat:
05-10 06:37:08.785: D/AndroidRuntime(705): Shutting down VM
05-10 06:37:08.785: W/dalvikvm(705): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-10 06:37:08.807: E/AndroidRuntime(705): FATAL EXCEPTION: main
05-10 06:37:08.807: E/AndroidRuntime(705): android.content.res.Resources$NotFoundException: Resource ID #0x7f080001 type #0x12 is not valid
05-10 06:37:08.807: E/AndroidRuntime(705): at android.content.res.Resources.loadXmlResourceParser(Resources.java:1874)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.content.res.Resources.getLayout(Resources.java:731)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.LayoutInflater.inflate(LayoutInflater.java:318)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:332)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.ListView.onMeasure(ListView.java:1109)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.View.measure(View.java:8171)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.View.measure(View.java:8171)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.View.measure(View.java:8171)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.View.measure(View.java:8171)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.View.measure(View.java:8171)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.os.Handler.dispatchMessage(Handler.java:99)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.os.Looper.loop(Looper.java:123)
05-10 06:37:08.807: E/AndroidRuntime(705): at android.app.ActivityThread.main(ActivityThread.java:4627)
05-10 06:37:08.807: E/AndroidRuntime(705): at java.lang.reflect.Method.invokeNative(Native Method)
05-10 06:37:08.807: E/AndroidRuntime(705): at java.lang.reflect.Method.invoke(Method.java:521)
05-10 06:37:08.807: E/AndroidRuntime(705): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-10 06:37:08.807: E/AndroidRuntime(705): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-10 06:37:08.807: E/AndroidRuntime(705): at dalvik.system.NativeStart.main(Native Method)
05-10 06:37:12.796: I/Process(705): Sending signal. PID: 705 SIG: 9
Here's the XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/button1"
android:layout_marginTop="41dp" >
</ListView>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/editText1"
android:layout_alignBottom="@+id/editText1"
android:layout_marginLeft="69dp"
android:layout_toRightOf="@+id/editText1"
android:ems="10"
android:hint="Number"
android:inputType="phone" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/listView1"
android:layout_alignParentTop="true"
android:layout_marginLeft="26dp"
android:layout_marginTop="24dp"
android:ems="10"
android:hint="Name"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginLeft="49dp"
android:layout_marginTop="21dp"
android:text="Add"
android:textSize="36sp" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button1"
android:layout_alignBottom="@+id/button1"
android:layout_alignLeft="@+id/editText2"
android:layout_marginLeft="39dp"
android:text="Delete"
android:textSize="36sp" />
</RelativeLayout>