Android:应用程序在加载listview oncreate时崩溃

时间:2014-05-12 17:29:59

标签: java android eclipse

每当我运行我的SetList方法时,在启动时创建应用程序崩溃(通过点击事件等方式调用该方法时工作正常) 我对android开发很新,所以我不完全确定程序启动的操作顺序。 第130行:lv.setAdapter(适配器);

  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment())
                .commit();
    }
   LoadLogs();
}
ArrayList<String> Logs = new ArrayList<String>();
String path = Environment.getExternalStorageDirectory().getAbsolutePath();
String name = "Logs.txt";
boolean Returned = true;
public void LoadLogs()
 {
    File file = new File(path,name);
    try
    {
        if(!file.exists())
        {
            file.createNewFile();
        }
    }
    catch(Exception e)
    {
          e.printStackTrace();
    }
    try {
          BufferedReader bufferedReader = new BufferedReader(new FileReader(file));

          String l;
          Returned = true;
          while (( l = bufferedReader.readLine()) != null) {
            Returned = l.contains("Returned");
            Logs.add(l);
          }

          } catch (Exception e) {
              e.printStackTrace();
        }
    SetList();
 }

    public void SetList()
{
    ListView lv = (ListView) findViewById(R.id.listView1);
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,Logs);
    lv.setAdapter(adapter);
}

布局:

<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=".MainActivity$PlaceholderFragment" >

<Button
    android:id="@+id/button2"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button1"
    android:layout_centerHorizontal="true"
    android:onClick="onClickLog"
    android:text="Log" />

<Button
    android:id="@+id/button1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/button2"
    android:layout_alignParentBottom="true"
    android:onClick="onClickEmail"
    android:text="Email" />

<ListView
    android:id="@+id/listView1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_above="@+id/button2"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true" >

</ListView>

logcat的

05-12 13:22:49.795: D/AndroidRuntime(2122): Shutting down VM
05-12 13:22:49.795: W/dalvikvm(2122): threadid=1: thread exiting with uncaught exception (group=0xb2ababa8)
05-12 13:22:49.825: E/AndroidRuntime(2122): FATAL EXCEPTION: main
05-12 13:22:49.825: E/AndroidRuntime(2122): Process: com.example.gs4depositalogging, PID: 2122
05-12 13:22:49.825: E/AndroidRuntime(2122): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.gs4depositalogging/com.example.gs4depositalogging.MainActivity}: java.lang.NullPointerException
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.os.Handler.dispatchMessage(Handler.java:102)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.os.Looper.loop(Looper.java:136)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.ActivityThread.main(ActivityThread.java:5017)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at java.lang.reflect.Method.invokeNative(Native Method)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at java.lang.reflect.Method.invoke(Method.java:515)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at dalvik.system.NativeStart.main(Native Method)
05-12 13:22:49.825: E/AndroidRuntime(2122): Caused by: java.lang.NullPointerException
05-12 13:22:49.825: E/AndroidRuntime(2122):     at com.example.gs4depositalogging.MainActivity.SetList(MainActivity.java:130)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at com.example.gs4depositalogging.MainActivity.LoadLogs(MainActivity.java:124)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at com.example.gs4depositalogging.MainActivity.onCreate(MainActivity.java:41)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.Activity.performCreate(Activity.java:5231)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
05-12 13:22:49.825: E/AndroidRuntime(2122):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
05-12 13:22:49.825: E/AndroidRuntime(2122):     ... 11 more

1 个答案:

答案 0 :(得分:0)

您的ListView位于PlaceholderFragment布局中,而不在活动布局中。您无法使用findViewById()onCreate()调用的方法检索它。尝试在空列表视图上调用setListAdapter()是NPE的直接原因。

设置片段视图的最佳位置和时间是片段onCreateView()

还考虑在后台线程上进行文件I / O,例如使用AsyncTask