java.lang.NullPointerException,listview的FATAL EXCEPTION

时间:2015-02-11 11:22:11

标签: java android eclipse listview

嗨,我是Android的新手并原谅我,因为这是我第一次为我的学校作业开发移动应用程序。我有一个错误,当我尝试启动应用程序时,我的应用程序崩溃了,我的猜测是检索是罪魁祸首。

我正在创建一个活动,该活动应该显示一个空列表,允许用户输入CRUD。我确实理解错误是由于null,但我如何使它允许显示空列表?对于如何解决这个问题,我将不胜感激,谢谢!

这是logcat:

02-11 18:41:53.803: W/dalvikvm(18334): threadid=1: thread exiting with    uncaught exception (group=0x41763da0)
02-11 18:41:53.803: E/AndroidRuntime(18334): FATAL EXCEPTION: main
02-11 18:41:53.803: E/AndroidRuntime(18334): Process: com.example.sgrecipe, PID: 18334
02-11 18:41:53.803: E/AndroidRuntime(18334): java.lang.NullPointerException
02-11 18:41:53.803: E/AndroidRuntime(18334):    at com.example.sgrecipe.FavouriteFragment.fetchData(FavouriteFragment.java:174)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at  com.example.sgrecipe.FavouriteFragment.initControls(FavouriteFragment.java:92)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at com.example.sgrecipe.FavouriteFragment.onStart(FavouriteFragment.java:50)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.app.Fragment.performStart(Fragment.java:1484)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:941)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.app.FragmentManagerImpl.performPendingDeferredStart(FragmentManager.java:807)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.app.FragmentManagerImpl.startPendingDeferredFragments(FragmentManager.java:1112)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1461)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:461)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.view.ViewPager.populate(ViewPager.java:1064)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.view.ViewPager.populate(ViewPager.java:911)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1432)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:382)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5365)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2505)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.View.measure(View.java:17396)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2175)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1316)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1513)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1200)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6401)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:803)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.Choreographer.doCallbacks(Choreographer.java:603)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.Choreographer.doFrame(Choreographer.java:573)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:789)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.os.Handler.handleCallback(Handler.java:733)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.os.Handler.dispatchMessage(Handler.java:95)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.os.Looper.loop(Looper.java:157)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at android.app.ActivityThread.main(ActivityThread.java:5335)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at java.lang.reflect.Method.invokeNative(Native Method)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at java.lang.reflect.Method.invoke(Method.java:515)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
02-11 18:41:53.803: E/AndroidRuntime(18334):    at dalvik.system.NativeStart.main(Native Method)

FavouriteFragment.java:

package com.example.sgrecipe;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;

 public class FavouriteFragment extends Fragment implements OnClickListener{

// Primitive Variables
String selected_ID = "";

// Widget GUI Declare
EditText txtTitle, txtDesc, txtSalary;
Button btnAdd, btnUpdate, btnDelete;
ListView lvNotes;

// DB Objects
DBHelper helper;
SQLiteDatabase db;

// Adapter Object
SimpleCursorAdapter adapter;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.activity_favourite_fragment, container, false);

    return rootView;

}

  @Override
   public void onStart() {
   super.onStart();
   initControls();
   // Init DB Objects
   helper = new DBHelper(getActivity());}

   // Widget GUI Init
   private void initControls(){
   txtTitle = (EditText)getView().findViewById(R.id.txtTitle);
   txtDesc = (EditText)getView().findViewById(R.id.txtDes);
   txtSalary = (EditText)getView().findViewById(R.id.txtSalary);
   lvNotes = (ListView)getView().findViewById(R.id.lvNotes);

   btnAdd = (Button)getView().findViewById(R.id.btnAdd);
   btnUpdate = (Button)getView().findViewById(R.id.btnUpdate);
   btnDelete = (Button)getView().findViewById(R.id.btnDelete);

   // Attached Listener
   btnAdd.setOnClickListener(this);
   btnUpdate.setOnClickListener(this);
   btnDelete.setOnClickListener(this);
   lvNotes.setOnItemClickListener(new OnItemClickListener() {

       @Override
       public void onItemClick(AdapterView<?> adapter, View v,
               int position, long id) {

           String title, desc, salary;

           // Display Selected Row of Listview into EditText widget

           Cursor row = (Cursor) adapter.getItemAtPosition(position);
           selected_ID = row.getString(0);
           title = row.getString(1);
           desc = row.getString(2);
           salary = row.getString(3);

           txtTitle.setText(title);
           txtDesc.setText(desc);
           txtSalary.setText(salary);
       }
   });

   // Fetch Data from database
   fetchData();
 }


@Override
public void onClick(View v) {

    // Perform CRUD Operation

    if (v == btnAdd) {

        // Add Record with help of ContentValues and DBHelper class object
        ContentValues values = new ContentValues();
        values.put(DBHelper.C_TITLE, txtTitle.getText().toString());
        values.put(DBHelper.C_DESCRIPTION, txtDesc.getText().toString());
        values.put(DBHelper.C_SALARY, txtSalary.getText().toString());

        // Call insert method of SQLiteDatabase Class and close after
        // performing task
        db = helper.getWritableDatabase();
        db.insert(DBHelper.TABLE, null, values);
        db.close();

        clearFields();
        Toast.makeText(this.getActivity(), "Successfully Added",
                Toast.LENGTH_LONG).show();

        // Fetch Data from database and display into listview
        fetchData();

    }
    if (v == btnUpdate) {

        // Update Record with help of ContentValues and DBHelper class
        // object

        ContentValues values = new ContentValues();
        values.put(DBHelper.C_TITLE, txtTitle.getText().toString());
        values.put(DBHelper.C_DESCRIPTION, txtDesc.getText().toString());
        values.put(DBHelper.C_SALARY, txtSalary.getText().toString());

        // Call update method of SQLiteDatabase Class and close after
        // performing task
        db = helper.getWritableDatabase();
        db.update(DBHelper.TABLE, values, DBHelper.C_ID + "=?",
                new String[] { selected_ID });
        db.close();

        // Fetch Data from database and display into listview
        fetchData();
        Toast.makeText(this.getActivity(), "Record Updated Successfully",
                Toast.LENGTH_LONG).show();
        clearFields();

    }
    if (v == btnDelete) {

        // Call delete method of SQLiteDatabase Class to delete record and
        // close after performing task
        db = helper.getWritableDatabase();
        db.delete(DBHelper.TABLE, DBHelper.C_ID + "=?",
                new String[] { selected_ID });
        db.close();

        // Fetch Data from database and display into listview
        fetchData();
        Toast.makeText(this.getActivity(), "Record Deleted Successfully",
                Toast.LENGTH_LONG).show();
        clearFields();

    }
}

 // Clear Fields
    private void clearFields() {
        txtTitle.setText("");
        txtDesc.setText("");
        txtSalary.setText("");
    }

 // Fetch Fresh data from database and display into listview
    private void fetchData() {
        db = helper.getReadableDatabase();
        Cursor c = db.query(DBHelper.TABLE, null, null, null, null, null, null);
        adapter = new SimpleCursorAdapter(
                getActivity(),
                R.layout.row,
                c,
                new String[] { DBHelper.C_TITLE, DBHelper.C_SALARY,
                        DBHelper.C_DESCRIPTION },
                new int[] { R.id.lblTitle, R.id.lblSalary, R.id.lblDescription });
        lvNotes.setAdapter(adapter);
    }
    }

activity_favourite_fragment.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:text="Notes"
    android:textAppearance="?android:attr/textAppearanceLarge" >
</TextView>

<EditText
    android:id="@+id/txtTitle"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Title" >

    <requestFocus>
    </requestFocus>
</EditText>

<EditText
    android:id="@+id/txtDes"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Description" >
</EditText>

<EditText
    android:id="@+id/txtSalary"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:hint="Enter Salary" >
</EditText>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/btnAdd"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Add" >
    </Button>

    <Button
        android:id="@+id/btnUpdate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Update Record" >
    </Button>

    <Button
        android:id="@+id/btnDelete"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Delete" >
    </Button>
</LinearLayout>

<ListView
    android:id="@+id/lvNotes"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</ListView>

我刚刚开始使用android开发,任何帮助都会受到赞赏!

3 个答案:

答案 0 :(得分:3)

helper未初始化。

在您的onStart()中,您只是在致电initControls()并调用使用fetchData()的{​​{1}}后对其进行初始化。在helper来电之前移动helper初始化。

答案 1 :(得分:3)

您的问题出在onStart方法:

public void onStart() 
 {
   super.onStart();
   initControls();
   // Init DB Objects
   helper = new DBHelper(getActivity());
 }

请注意,您在调用initControls()之后创建了帮助程序对象,然后调用fetchData()

fetchData中你有这一行:

db = helper.getWritableDatabase();

这是导致空指针异常的行,因为helper为null。

更改onStart方法,如下所示:

 public void onStart() 
         {
           super.onStart();
           // Init DB Objects
           helper = new DBHelper(getActivity());
           initControls();

         }

这可以解决您的问题。

答案 2 :(得分:0)

更好的解决方案,移动代码行:

helper = new DBHelper(getActivity());

onStart()onCreate(),这里是发起对象的正确位置