I'm fairly new to android development and I'm having some issues adding an item to the ListView I have. The application is suppose to, upon clicking the "Add" button, bring up a EditText that takes in the name of the class then it is suppose to add to the list the class name. The problem I'm running into is after I finish entering the class name and try to hit Add (My positiveButton of my AlertDialog.Builder) it does a force close. This is the code I have so far:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:text="Add Class"
android:layout_width="wrap_content"
android:id="@+id/addClassBtn"
android:layout_height="wrap_content"
/>
<Button
android:layout_width="wrap_content"
android:id="@+id/removeClassBtn"
android:text="Remove Class"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/addClassBtn"
/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/classList"
android:layout_below="@+id/addClassBtn"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
and my java file:
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button add = (Button)findViewById(R.id.addClassBtn);
@SuppressWarnings("unused")
final Button remove = (Button)findViewById(R.id.removeClassBtn);
final EditText et = new EditText(getApplicationContext());
final ListView lv = (ListView)findViewById(R.id.classList);
final AlertDialog alert;
final ArrayList<String> classes = new ArrayList<String>();
final ArrayAdapter<String> adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_list_item_1, R.id.classList, classes);
lv.setAdapter(adapter);
final AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(et);
builder.setPositiveButton("Add", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
classes.add(et.getText().toString());
adapter.notifyDataSetChanged();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
@Override
public void onClick(DialogInterface dialog, int which)
{
dialog.cancel();
}
});
alert = builder.create();
add.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
alert.show();
}
});
}
Any suggestions as to what I'm doing wrong or a possible fix would be greatly appreciated.
Here is the logcat of what happens:
09-26 18:12:14.345: DEBUG/AndroidRuntime(879): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
09-26 18:12:14.357: DEBUG/AndroidRuntime(879): CheckJNI is ON
09-26 18:12:14.455: DEBUG/AndroidRuntime(879): --- registering native functions ---
09-26 18:12:14.915: INFO/ActivityManager(58): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.example.ClassList/.ClassListMCPActivity }
09-26 18:12:14.935: INFO/ActivityManager(58): Start proc com.example.ClassList for activity com.example.ClassList/.ClassListMCPActivity: pid=885 uid=10036 gids={}
09-26 18:12:14.985: DEBUG/AndroidRuntime(879): Shutting down VM
09-26 18:12:14.985: DEBUG/dalvikvm(879): Debugger has detached; object registry had 1 entries
09-26 18:12:14.995: INFO/AndroidRuntime(879): NOTE: attach of thread 'Binder Thread #3' failed
09-26 18:12:15.745: INFO/ActivityManager(58): Displayed activity com.example.ClassList/.ClassListMCPActivity: 817 ms (total 817 ms)
09-26 18:12:20.915: DEBUG/dalvikvm(258): GC_EXPLICIT freed 71 objects / 3424 bytes in 101ms
09-26 18:12:25.996: WARN/KeyCharacterMap(885): No keyboard for id 0
09-26 18:12:25.996: WARN/KeyCharacterMap(885): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
09-26 18:12:28.905: DEBUG/dalvikvm(226): GC_EXPLICIT freed 100 objects / 4168 bytes in 107ms
09-26 18:12:29.766: DEBUG/AndroidRuntime(885): Shutting down VM
09-26 18:12:29.766: WARN/dalvikvm(885): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): FATAL EXCEPTION: main
09-26 18:12:29.775: ERROR/AndroidRuntime(885): java.lang.NullPointerException
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:353)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:323)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.AbsListView.obtainView(AbsListView.java:1315)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.ListView.onMeasure(ListView.java:1109)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.View.measure(View.java:8171)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.View.measure(View.java:8171)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.View.measure(View.java:8171)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.View.measure(View.java:8171)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
09-26 18:12:29.7开发者_开发技巧75: ERROR/AndroidRuntime(885): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.View.measure(View.java:8171)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.os.Handler.dispatchMessage(Handler.java:99)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.os.Looper.loop(Looper.java:123)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at java.lang.reflect.Method.invokeNative(Native Method)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at java.lang.reflect.Method.invoke(Method.java:521)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-26 18:12:29.775: ERROR/AndroidRuntime(885): at dalvik.system.NativeStart.main(Native Method)
09-26 18:12:29.785: WARN/ActivityManager(58): Force finishing activity com.example.ClassList/.ClassListMCPActivity
09-26 18:12:30.327: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{44fd7988 com.example.ClassList/.ClassListMCPActivity}
09-26 18:12:40.979: WARN/ActivityManager(58): Activity destroy timeout for HistoryRecord{44fd7988 com.example.ClassList/.ClassListMCPActivity}
09-26 18:12:45.955: INFO/Process(885): Sending signal. PID: 885 SIG: 9
09-26 18:12:45.965: INFO/ActivityManager(58): Process com.example.ClassList (pid 885) has died.
09-26 18:12:45.965: INFO/WindowManager(58): WIN DEATH: Window{44fcdd78 com.example.ClassList/com.example.ClassList.ClassListMCPActivity paused=false}
09-26 18:12:45.965: INFO/WindowManager(58): WIN DEATH: Window{4501a6a0 com.example.ClassList/com.example.ClassList.ClassListMCPActivity paused=false}
09-26 18:12:45.995: WARN/InputManagerService(58): Got RemoteException sending setActive(false) notification to pid 885 uid 10036
09-26 18:12:48.557: WARN/KeyCharacterMap(115): No keyboard for id 0
09-26 18:12:48.557: WARN/KeyCharacterMap(115): Using default keymap: /system/usr/keychars/qwerty.kcm.bin
the issue is related to the adapter. Take a look at this example android helloview
精彩评论