应用程序现在在列表视图中崩溃了...但它昨晚都运行了

时间:2015-08-18 18:04:54

标签: java android listview android-listview

它崩溃在这一行:alListView.setAdapter(adapter);它昨晚都工作了,我没有做任何更改......我做了一些格式化更改,也许我删除了一些我不应该删除的内容。

    package com.grumpy.multipages;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

public class MainPage extends AppCompatActivity {
    AlDatabaseAdapter AlHelper;
    private ListView alListView;
    //AlHelper helper;
    private static final String TAG = "MultiPages";
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_page);
        AlHelper = new AlDatabaseAdapter(this);
        //SQLiteDatabase db = AlHelper.getWritableDatabase();
        String myHouses = LoadDB();
        String[] values = myHouses.split("\n");
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
        //alListView.setAdapter(adapter);
        alListView.setAdapter(adapter); // CRASHES HERE
        Log.d(TAG, "Main Page ");
        //Button myBtn = (Button)findViewById(R.id.button1_Button);
        Button myBtn1 = (Button)findViewById(R.id.button1_Button);
        myBtn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(getApplicationContext(), AddHouse.class);
                //intent.putExtra("refer", "Called from Main Page");
                startActivity(intent);
            }
        });
    }
    private String LoadDB() {
        // Need to get list of Houses from Database
        Log.d(TAG, "Calling LoadDB ");
        String myHouses = AlHelper.getData();
        Log.d(TAG, "Returning From LoadDB ");
        Log.d(TAG,myHouses);
        return myHouses;
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        return super.onCreateOptionsMenu(menu);
    }
}

//布局页面

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainPage">

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/btnAddHouse"
    android:id="@+id/button1_Button" />

<ListView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/listView"
    android:layout_gravity="center_horizontal"
    android:choiceMode="singleChoice"
    android:clickable="true"
    tools:listitem="@android:layout/simple_list_item_1" />

2 个答案:

答案 0 :(得分:0)

在:

alListView.setAdapter(adapter); // CRASHES HERE

您应该将alListView分配给您的小部件,并且从您的代码中看起来它在上面的行中为空。

添加:

alListView = (ListView)findViewById(R.id.listView);

答案 1 :(得分:0)

这可能是空指针异常,因为你声明了Listview(即alListView),但你错过了定义它,即alListView =(ListView)findviewById ... 代码并尝试在崩溃的空对象引用中设置适配器。