我正在尝试创建ListView
并用数据填充它,但是当我启动应用程序时崩溃并向我显示一条消息
不幸已经停止了。
我正在尝试做这个教程。
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
main.java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String[] actions = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
ArrayAdapter<String> value = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
R.id.mylist, actions);
ListView v = (ListView)findViewById(R.id.mylist);
v.setTextFilterEnabled(true);
v.setAdapter(value);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
日志
08-08 15:19:47.364: E/Trace(604): error opening trace file: No such file or directory (2)
08-08 15:19:47.945: I/Choreographer(604): Skipped 40 frames! The application may be doing too much work on its main thread.
08-08 15:19:47.965: D/AndroidRuntime(604): Shutting down VM
08-08 15:19:47.995: W/dalvikvm(604): threadid=1: thread exiting with uncaught exception (group=0x40a13300)
08-08 15:19:48.015: E/AndroidRuntime(604): FATAL EXCEPTION: main
08-08 15:19:48.015: E/AndroidRuntime(604): java.lang.NullPointerException
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:392)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.AbsListView.obtainView(AbsListView.java:2267)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.ListView.measureHeightOfChildren(ListView.java:1244)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.ListView.onMeasure(ListView.java:1156)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.LinearLayout.measureVertical(LinearLayout.java:833)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
08-08 15:19:48.015: E/AndroidRuntime(604): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2148)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.View.measure(View.java:15172)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1848)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1100)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1273)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:998)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4212)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.Choreographer.doFrame(Choreographer.java:525)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.os.Handler.handleCallback(Handler.java:615)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.os.Handler.dispatchMessage(Handler.java:92)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.os.Looper.loop(Looper.java:137)
08-08 15:19:48.015: E/AndroidRuntime(604): at android.app.ActivityThread.main(ActivityThread.java:4745)
08-08 15:19:48.015: E/AndroidRuntime(604): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 15:19:48.015: E/AndroidRuntime(604): at java.lang.reflect.Method.invoke(Method.java:511)
08-08 15:19:48.015: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
08-08 15:19:48.015: E/AndroidRuntime(604): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-08 15:19:48.015: E/AndroidRuntime(604): at dalvik.system.NativeStart.main(Native Method)
08-08 15:19:51.895: I/Process(604): Sending signal. PID: 604 SIG: 9