java.lang.NullPointerException,列表视图中的致命异常

Posted

技术标签:

【中文标题】java.lang.NullPointerException,列表视图中的致命异常【英文标题】:java.lang.NullPointerException, FATAL EXCEPTION at listview 【发布时间】:2015-04-11 17:59:01 【问题描述】:

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

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

这里是日志:

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)

FavoriteFragment.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_
android:layout_
android:orientation="vertical" >

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

<EditText
    android:id="@+id/txtTitle"
    android:layout_
    android:layout_
    android:hint="Title" >

    <requestFocus>
    </requestFocus>
</EditText>

<EditText
    android:id="@+id/txtDes"
    android:layout_
    android:layout_
    android:hint="Description" >
</EditText>

<EditText
    android:id="@+id/txtSalary"
    android:layout_
    android:layout_
    android:hint="Enter Salary" >
</EditText>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_
    android:layout_ >

    <Button
        android:id="@+id/btnAdd"
        android:layout_
        android:layout_
        android:text="Add" >
    </Button>

    <Button
        android:id="@+id/btnUpdate"
        android:layout_
        android:layout_
        android:text="Update Record" >
    </Button>

    <Button
        android:id="@+id/btnDelete"
        android:layout_
        android:layout_
        android:text="Delete" >
    </Button>
</LinearLayout>

<ListView
    android:id="@+id/lvNotes"
    android:layout_
    android:layout_ >
</ListView>

我刚刚开始进行 android 开发,任何帮助将不胜感激!

【问题讨论】:

【参考方案1】:

您的问题出在onStart 方法中:

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

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

fetchData 你有这行:

db = helper.getWritableDatabase();

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

像这样更改您的 onStart 方法:

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

         

这应该可以解决您的问题。

【讨论】:

【参考方案2】:

helper 未初始化。

在您的onStart() 中,您只需在调用initControls() 后初始化它,该fetchData() 调用使用helperfetchData()。将 helper 初始化移到 initControls() 调用之前。

【讨论】:

【参考方案3】:

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

helper = new DBHelper(getActivity());

onStart()onCreate() 哪里是启动对象的正确位置

【讨论】:

onStart() 每次从不可见(背景)到可见(前景)都会被调用,但 onCreate() 只调用一次,所以你认为应该在哪里启动你的helper

以上是关于java.lang.NullPointerException,列表视图中的致命异常的主要内容,如果未能解决你的问题,请参考以下文章

亲測,Eclipse报&quot;An error has occurred,See error log for more details. java.lang.NullPointerExce

来自 LayerDrawable 的异常

Microsoft Access 和 Java JDBC-ODBC 错误

如何使用其id获取单个SQLite行数据?