【问题标题】:ERROR/AndroidRuntime(335): Caused by: java.lang.NullPointerException错误/AndroidRuntime(335):由:java.lang.NullPointerException 引起
【发布时间】:2011-09-14 07:17:54
【问题描述】:

嗨,我是安卓的新手。 在执行我的应用程序时出现空点异常错误,不知道为什么会这样。请任何人帮助我摆脱这个问题。提前致谢。

这是我的日志猫:

09-14 12:30:14.027: INFO/System.out(365): Constructor StudentList Adapter...
09-14 12:30:14.027: DEBUG/AndroidRuntime(365): Shutting down VM
09-14 12:30:14.027: WARN/dalvikvm(365): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
09-14 12:30:14.047: ERROR/AndroidRuntime(365): FATAL EXCEPTION: main
09-14 12:30:14.047: ERROR/AndroidRuntime(365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.UserLogin/com.UserLogin.FindPlaces}: java.lang.NullPointerException
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:651)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.widget.TabHost.setCurrentTab(TabHost.java:323)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.widget.TabHost$2.onTabSelectionChanged(TabHost.java:129)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.widget.TabWidget$TabClickListener.onClick(TabWidget.java:453)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.view.View.performClick(View.java:2408)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.view.View$PerformClick.run(View.java:8816)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.os.Handler.handleCallback(Handler.java:587)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.os.Handler.dispatchMessage(Handler.java:92)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.os.Looper.loop(Looper.java:123)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.main(ActivityThread.java:4627)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at java.lang.reflect.Method.invokeNative(Native Method)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at java.lang.reflect.Method.invoke(Method.java:521)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at dalvik.system.NativeStart.main(Native Method)
09-14 12:30:14.047: ERROR/AndroidRuntime(365): Caused by: java.lang.NullPointerException
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at com.UserLogin.FindPlaces.displayLocs(FindPlaces.java:35)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at com.UserLogin.FindPlaces.onCreate(FindPlaces.java:30)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-14 12:30:14.047: ERROR/AndroidRuntime(365):     ... 18 more
09-14 12:30:14.057: WARN/ActivityManager(58):   Force finishing activity com.UserLogin/.Innerpage
09-14 12:30:14.594: WARN/ActivityManager(58): Activity pause timeout for HistoryRecord{4506eef0 com.UserLogin/.Innerpage}
09-14 12:30:16.347: INFO/Process(365): Sending signal. PID: 365 SIG: 9
09-14 12:30:16.367: INFO/ActivityManager(58): Process com.UserLogin (pid 365) has died.
09-14 12:30:16.367: INFO/WindowManager(58): WIN DEATH: Window{4506a6e0 com.UserLogin/com.UserLogin.Innerpage paused=false}
09-14 12:30:16.367: INFO/WindowManager(58): WIN DEATH: Window{451492d0 com.UserLogin/com.UserLogin.UserLogin paused=false}
09-14 12:30:16.397: INFO/UsageStats(58): Unexpected resume of com.android.launcher while already resumed in com.UserLogin

Java 文件:

import java.util.ArrayList;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class FindPlaces extends ListActivity{

    private SQLiteDatabase DbLoc;
    ListView lv;
    private ArrayList<Fields> results = new ArrayList<Fields>();
    @Override
    public void onCreate(Bundle savedInstance) {
        super.onCreate(savedInstance);
        //setContentView(R.layout.places);
        getallLocs();
        displayLocs();

    }
    private void displayLocs() {
        lv = (ListView)findViewById(R.id.listPlaces);
        lv.setAdapter(new StudentListAdapter(this, R.layout.placeslist, results));

        //lv.setAdapter(new ArrayAdapter<Fields>(this,android.R.layout.simple_list_item_1, results));

    }
    class StudentListAdapter extends ArrayAdapter<Fields>{
        private ArrayList<Fields> locationDetails;
        private Context mContext;

        public StudentListAdapter(Context context,int textViewResourceId, ArrayList<Fields> results) {
            super(context, textViewResourceId, results);
            // TODO Auto-generated constructor stub
            System.out.println("Constructor StudentList Adapter...");
            this.locationDetails = results;
            mContext = context;
        }


        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return results.size();
        }

        @Override
        public Fields getItem(int position) {
            // TODO Auto-generated method stub
            return locationDetails.get(position);
        }
        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return super.getItemId(position);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            View v = convertView;
            if(v == null){
                LayoutInflater vl = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                v = vl.inflate(R.layout.placeslist, null);
            }
                            Fields o = results.get(position);

            if (o != null) {

                TextView iv = (TextView)v.findViewById(R.id.toptext);
                TextView tv_sNo = (TextView)v.findViewById(R.id.toptext1);
                iv.setText(o.getLocationName());                            
                tv_sNo.setText(o.getLocationImage());   

            }
            DbLoc.close();
            return v;
        }       
    }
    static class ViewHolder
    {
        TextView Locationname;
        ImageView Locationimage;
    }
    private void getallLocs() {
        // TODO Auto-generated method stub
        try {
            DatabaseHelper dbHelper = new DatabaseHelper(
                    this.getApplicationContext());
            DbLoc = dbHelper.getWritableDatabase();
            Cursor c = DbLoc.rawQuery("SELECT " + DatabaseHelper.LocationName+ " , " + DatabaseHelper.LocationImage + " FROM "
                    + DatabaseHelper.LOCATIONTABLE , null);
                            if (c != null) {
                if (c.moveToFirst()) {
                    do {
                        String LocationName= c.getString(c.getColumnIndex("LocationName"));
                        String Mobile = c.getString(c
                                .getColumnIndex("LocationImage"));
                        Fields p = new Fields(LocationName, Mobile);
                        results.add(p);

                    } while (c.moveToNext());
                }
            }
        } catch (SQLiteException se) {
            Log.e(getClass().getSimpleName(),
            "Could not create or Open the database");
        } 
        finally { if (DbLoc != null) DbLoc.execSQL("DELETE FROM " +
                DatabaseHelper.FRIENDTABLE); DbLoc.execSQL("DELETE FROM " +
                        DatabaseHelper.LOCATIONTABLE);  
                DbLoc.execSQL("DROP TABLE IF EXISTS tablename"+ DatabaseHelper.FRIENDTABLE);
                DbLoc.execSQL("DROP TABLE IF EXISTS tablename"+ DatabaseHelper.LOCATIONTABLE);
                DbLoc.close();
        }
    }
}

Places.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="fill_parent" android:padding="6dip">
    <ListView android:id="@+id/listPlaces" android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

Placeslist.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="fill_parent"
    android:layout_height="wrap_content">
    <TextView android:id="@+id/toptext" android:layout_width="fill_parent"
        android:layout_height="0dip" android:layout_weight="1"
        android:gravity="center_vertical" />
    <TextView android:id="@+id/toptext1" android:layout_width="fill_parent"
        android:layout_height="0dip" android:layout_weight="1"
        android:gravity="center_vertical" />
</LinearLayout>

【问题讨论】:

    标签: android nullpointerexception simplecursoradapter layout-inflater


    【解决方案1】:

    取消注释该行

    setContentView(R.layout.places);
    

    已编辑

    <?xml version="1.0" encoding="utf-8"?>
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent" android:padding="6dip">
        <ListView android:id="@android:id/list" android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
    

    用你的文件替换它(Places.xml)

    更新

     public void onCreate(Bundle savedInstance) {
            super.onCreate(savedInstance);
            //setContentView(R.layout.places);
            getallLocs();
            setListAdapter(new ArrayAdapter<Fields>(this,android.R.layout.simple_list_item_1, results));
    //        displayLocs();
    
        }
        private void displayLocs() {
            lv = (ListView)findViewById(R.id.listPlaces);
            lv.setAdapter(new StudentListAdapter(this, R.layout.placeslist, results));
    
            //lv.setAdapter(new ArrayAdapter<Fields>(this,android.R.layout.simple_list_item_1, results));
    
        }
    

    【讨论】:

    • 取消注释后得到 09-14 12:49:50.902: ERROR/AndroidRuntime(395): Caused by: java.lang.RuntimeException: Your content must have a ListView its id attribute is 'android.R logcat 中的 .id.list'
    • 替换places.xml文件后 lv = (ListView)findViewById(R.id.listPlaces);这将是错误的。
    • 是的。我已经删除了该行,现在得到 09-14 14:13:54.206: ERROR/AndroidRuntime(479): Caused by: java.lang.RuntimeException: 您的内容必须有一个 ID 属性为“android.R.id”的 ListView。列表'
    • 是的。我已替换为您编辑的 xml 文件,现在我已经构建了一次项目并检查现在是否收到此错误 :::: 09-14 14:28:09.247: ERROR/AndroidRuntime(539): java.lang.RuntimeException : 无法启动活动 ComponentInfo{com.UserLogin/com.UserLogin.FindPlaces}: java.lang.NullPointerException 09-14 14:28:09.247: ERROR/AndroidRuntime(539): Caused by: java.lang.NullPointerException
    • 注释 displayLocs();方法
    【解决方案2】:

    如果您没有为活动设置内容视图,则不能使用findViewById()。所以你的lv 将为空。


    您使用的是ListActivity。如果你想添加自己的样式,布局中的ListView需要id为android:list

    <ListView android:id="@android:id/list"
      ...
    />
    

    如图here

    如果使用ListActivity,不使用findViewById()获取ListView,可以使用getListView()-method

    【讨论】:

    • 替换places.xml文件后 lv = (ListView)findViewById(R.id.listPlaces);这将是错误的。
    • 如果你使用ListActivity,并且你想自定义它,ListView需要上面的id。您可以使用 getListView()-method 在 Java 中访问它
    • 我已经在上面评论了这一行,我只是在调用适配器---- lv.setAdapter(new StudentListAdapter(this, R.layout.placeslist, results));----- - 现在我得到 --- 14 14:28:09.247: ERROR/AndroidRuntime(539): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.UserLogin/com.UserLogin.FindPlaces}: java.lang. NullPointerException 09-14 14:28:09.247: 错误/AndroidRuntime(539): 原因:java.lang.NullPointerException
    • 感谢您的帖子。它真的有我的解决方案.. 还有一个如何显示带有图像 url 的图像。
    • 在@hotveryspicy 的帮助下,我可以获取列表视图的值。但我需要自定义列表视图中的这些值。我想通过膨胀布局来显示这些值。你能帮帮我吗?
    猜你喜欢
    • 1970-01-01
    • 2013-10-25
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-02-03
    相关资源
    最近更新 更多