我一直试图在过去的一天用数组填充listview,没有运气。每当我尝试在listview上设置adaper时,应用程序就会一直停止;也没有立即显示错误。只是想知道是否有人能发现我的错误:
我使用populateListView()方法将数组添加到我的listview,这是从onCreate方法调用的:
MainActivity类:
package com.example.application;
import java.util.ArrayList;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
//To allow navigation to the next page, with the information....
public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
ArrayAdapter<String> teamsAdapter ;
ArrayList<String> list = new ArrayList<String>();
ArrayList<String> teams = new ArrayList<String>();
String lstTeams[];
private ListView lstOne;
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
// adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, list);
populateListView();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
private void populateListView() {
lstOne = (ListView) findViewById(R.id.listView1);
ArrayList<String> your_array_list = new ArrayList<String>();
your_array_list.add("foo");
your_array_list.add("bar");
lstOne.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, your_array_list));
}
主要活动的xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.application.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/edit_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="21dp"
android:ems="10"
android:hint="@string/edit_message" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/txt_Password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/edit_message"
android:layout_marginLeft="15dp"
android:ems="10"
android:hint="@string/txt_Password" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/txt_Password"
android:layout_below="@+id/txt_Password"
android:onClick="sendMessage"
android:text="@string/button_send" />
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/button1"
android:layout_centerHorizontal="true" />
<Button
android:id="@+id/getSelectedItems"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/button1"
android:layout_centerHorizontal="true"
android:text="@string/getSelectedItemsButton_Message"
android:onClick="btnGetSelectedItems_Click"
/>
</RelativeLayout>
这是它产生的日志(不确定是否应该有更多)
新的Log-cat错误:
06-16 05:29:29.636: D/dalvikvm(1072): Not late-enabling CheckJNI (already on)
06-16 05:29:31.306: D/dalvikvm(1072): GC_FOR_ALLOC freed 75K, 6% free 2902K/3068K, paused 25ms, total 27ms
06-16 05:29:31.536: I/Choreographer(1072): Skipped 47 frames! The application may be doing too much work on its main thread.
06-16 05:29:31.586: D/gralloc_goldfish(1072): Emulator without GPU emulation detected.
06-16 05:29:45.096: I/Choreographer(1072): Skipped 54 frames! The application may be doing too much work on its main thread.
06-16 05:30:08.926: D/AndroidRuntime(1130): Shutting down VM
06-16 05:30:08.926: W/dalvikvm(1130): threadid=1: thread exiting with uncaught exception (group=0xb2afaba8)
06-16 05:30:08.926: E/AndroidRuntime(1130): FATAL EXCEPTION: main
06-16 05:30:08.926: E/AndroidRuntime(1130): Process: com.example.application, PID: 1130
06-16 05:30:08.926: E/AndroidRuntime(1130): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.MainActivity}: java.lang.NullPointerException
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.os.Looper.loop(Looper.java:136)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 05:30:08.926: E/AndroidRuntime(1130): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 05:30:08.926: E/AndroidRuntime(1130): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 05:30:08.926: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 05:30:08.926: E/AndroidRuntime(1130): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 05:30:08.926: E/AndroidRuntime(1130): at dalvik.system.NativeStart.main(Native Method)
06-16 05:30:08.926: E/AndroidRuntime(1130): Caused by: java.lang.NullPointerException
06-16 05:30:08.926: E/AndroidRuntime(1130): at com.example.application.MainActivity.populateListView(MainActivity.java:64)
06-16 05:30:08.926: E/AndroidRuntime(1130): at com.example.application.MainActivity.onCreate(MainActivity.java:38)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.Activity.performCreate(Activity.java:5231)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 05:30:08.926: E/AndroidRuntime(1130): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 05:30:08.926: E/AndroidRuntime(1130): ... 11 more
06-16 05:30:12.456: I/Process(1130): Sending signal. PID: 1130 SIG: 9
06-16 05:32:46.156: D/AndroidRuntime(1181): Shutting down VM
06-16 05:32:46.156: W/dalvikvm(1181): threadid=1: thread exiting with uncaught exception (group=0xb2afaba8)
06-16 05:32:46.166: E/AndroidRuntime(1181): FATAL EXCEPTION: main
06-16 05:32:46.166: E/AndroidRuntime(1181): Process: com.example.application, PID: 1181
06-16 05:32:46.166: E/AndroidRuntime(1181): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.MainActivity}: java.lang.NullPointerException
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.os.Looper.loop(Looper.java:136)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 05:32:46.166: E/AndroidRuntime(1181): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 05:32:46.166: E/AndroidRuntime(1181): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 05:32:46.166: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 05:32:46.166: E/AndroidRuntime(1181): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 05:32:46.166: E/AndroidRuntime(1181): at dalvik.system.NativeStart.main(Native Method)
06-16 05:32:46.166: E/AndroidRuntime(1181): Caused by: java.lang.NullPointerException
06-16 05:32:46.166: E/AndroidRuntime(1181): at com.example.application.MainActivity.populateListView(MainActivity.java:68)
06-16 05:32:46.166: E/AndroidRuntime(1181): at com.example.application.MainActivity.onCreate(MainActivity.java:38)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.Activity.performCreate(Activity.java:5231)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 05:32:46.166: E/AndroidRuntime(1181): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 05:32:46.166: E/AndroidRuntime(1181): ... 11 more
06-16 05:32:51.466: I/Process(1181): Sending signal. PID: 1181 SIG: 9
06-16 05:42:53.226: D/AndroidRuntime(1229): Shutting down VM
06-16 05:42:53.226: W/dalvikvm(1229): threadid=1: thread exiting with uncaught exception (group=0xb2afaba8)
06-16 05:42:53.236: E/AndroidRuntime(1229): FATAL EXCEPTION: main
06-16 05:42:53.236: E/AndroidRuntime(1229): Process: com.example.application, PID: 1229
06-16 05:42:53.236: E/AndroidRuntime(1229): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.MainActivity}: java.lang.NullPointerException
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.os.Looper.loop(Looper.java:136)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 05:42:53.236: E/AndroidRuntime(1229): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 05:42:53.236: E/AndroidRuntime(1229): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 05:42:53.236: E/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 05:42:53.236: E/AndroidRuntime(1229): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 05:42:53.236: E/AndroidRuntime(1229): at dalvik.system.NativeStart.main(Native Method)
06-16 05:42:53.236: E/AndroidRuntime(1229): Caused by: java.lang.NullPointerException
06-16 05:42:53.236: E/AndroidRuntime(1229): at com.example.application.MainActivity.populateListView(MainActivity.java:79)
06-16 05:42:53.236: E/AndroidRuntime(1229): at com.example.application.MainActivity.onCreate(MainActivity.java:38)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.Activity.performCreate(Activity.java:5231)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 05:42:53.236: E/AndroidRuntime(1229): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 05:42:53.236: E/AndroidRuntime(1229): ... 11 more
06-16 05:42:59.636: I/Process(1229): Sending signal. PID: 1229 SIG: 9
06-16 05:47:54.026: D/AndroidRuntime(1276): Shutting down VM
06-16 05:47:54.026: W/dalvikvm(1276): threadid=1: thread exiting with uncaught exception (group=0xb2afaba8)
06-16 05:47:54.036: E/AndroidRuntime(1276): FATAL EXCEPTION: main
06-16 05:47:54.036: E/AndroidRuntime(1276): Process: com.example.application, PID: 1276
06-16 05:47:54.036: E/AndroidRuntime(1276): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.MainActivity}: java.lang.NullPointerException
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.os.Looper.loop(Looper.java:136)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 05:47:54.036: E/AndroidRuntime(1276): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 05:47:54.036: E/AndroidRuntime(1276): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 05:47:54.036: E/AndroidRuntime(1276): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 05:47:54.036: E/AndroidRuntime(1276): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 05:47:54.036: E/AndroidRuntime(1276): at dalvik.system.NativeStart.main(Native Method)
06-16 05:47:54.036: E/AndroidRuntime(1276): Caused by: java.lang.NullPointerException
06-16 05:47:54.036: E/AndroidRuntime(1276): at com.example.application.MainActivity.populateListView(MainActivity.java:63)
06-16 05:47:54.036: E/AndroidRuntime(1276): at com.example.application.MainActivity.onCreate(MainActivity.java:38)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.Activity.performCreate(Activity.java:5231)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 05:47:54.036: E/AndroidRuntime(1276): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 05:47:54.036: E/AndroidRuntime(1276): ... 11 more
06-16 05:47:57.446: I/Process(1276): Sending signal. PID: 1276 SIG: 9
06-16 06:03:05.696: D/AndroidRuntime(1326): Shutting down VM
06-16 06:03:05.696: W/dalvikvm(1326): threadid=1: thread exiting with uncaught exception (group=0xb2afaba8)
06-16 06:03:05.706: E/AndroidRuntime(1326): FATAL EXCEPTION: main
06-16 06:03:05.706: E/AndroidRuntime(1326): Process: com.example.application, PID: 1326
06-16 06:03:05.706: E/AndroidRuntime(1326): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application/com.example.application.MainActivity}: java.lang.NullPointerException
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.ActivityThread.access$800(ActivityThread.java:135)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.os.Handler.dispatchMessage(Handler.java:102)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.os.Looper.loop(Looper.java:136)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.ActivityThread.main(ActivityThread.java:5017)
06-16 06:03:05.706: E/AndroidRuntime(1326): at java.lang.reflect.Method.invokeNative(Native Method)
06-16 06:03:05.706: E/AndroidRuntime(1326): at java.lang.reflect.Method.invoke(Method.java:515)
06-16 06:03:05.706: E/AndroidRuntime(1326): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
06-16 06:03:05.706: E/AndroidRuntime(1326): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
06-16 06:03:05.706: E/AndroidRuntime(1326): at dalvik.system.NativeStart.main(Native Method)
06-16 06:03:05.706: E/AndroidRuntime(1326): Caused by: java.lang.NullPointerException
06-16 06:03:05.706: E/AndroidRuntime(1326): at com.example.application.MainActivity.populateListView(MainActivity.java:58)
06-16 06:03:05.706: E/AndroidRuntime(1326): at com.example.application.MainActivity.onCreate(MainActivity.java:38)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.Activity.performCreate(Activity.java:5231)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
06-16 06:03:05.706: E/AndroidRuntime(1326): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
06-16 06:03:05.706: E/AndroidRuntime(1326): ... 11 more
06-16 06:03:08.376: I/Process(1326): Sending signal. PID: 1326 SIG: 9
任何帮助都会很棒!
更新
感谢大家的帮助,我已经解决了它。
问题是,我正在加载视图activity_main;但我的listView在fragment_main上;因此意味着找不到'listView1'。所以我将listView添加到'activity_main',确定它有效。
感谢您的所有帮助!
此致
卡勒姆