ERROR / AndroidRuntime(335):由:显示java.lang.NullPointerException [英] ERROR/AndroidRuntime(335): Caused by: java.lang.NullPointerException

查看:232
本文介绍了ERROR / AndroidRuntime(335):由:显示java.lang.NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是新蜜蜂到Android。 在执行我的应用我收到nullpoint异常错误,不知道为什么它正在发生。请任何一个能帮助我摆脱这个问题。在此先感谢。

这是我的logcat:

  12月9号至14号:30:14.027:信息/的System.out(365):构造StudentList适配器...
十二月九号至14号:30:14.027:DEBUG / AndroidRuntime(365):关闭虚拟机
十二月九号至14号:30:14.027:WARN / dalvikvm(365):主题ID = 1:螺纹退出与未捕获的异常(组= 0x4001d800)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):致命异常:主要
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):java.lang.RuntimeException的:无法启动的活动ComponentInfo {com.UserLogin / com.UserLogin.FindPlaces}:显示java.lang.NullPointerException
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.app.ActivityThread.startActivityNow(ActivityThread.java:2503)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.widget.TabHost $ IntentContentStrategy.getContentView(TabHost.java:651)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.widget.TabHost.setCurrentTab(TabHost.java:323)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.widget.TabHost $ 2.onTabSelectionChanged(TabHost.java:129)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.widget.TabWidget $ TabClickListener.onClick(TabWidget.java:453)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.view.View.performClick(View.java:2408)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.view.View $ PerformClick.run(View.java:8816)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.os.Handler.handleCallback(Handler.java:587)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.os.Handler.dispatchMessage(Handler.java:92)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.os.Looper.loop(Looper.java:123)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.app.ActivityThread.main(ActivityThread.java:4627)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在java.lang.reflect.Method.invokeNative(本机方法)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在java.lang.reflect.Method.invoke(Method.java:521)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在dalvik.system.NativeStart.main(本机方法)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):由:显示java.lang.NullPointerException
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在com.UserLogin.FindPlaces.displayLocs(FindPlaces.java:35)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在com.UserLogin.FindPlaces.onCreate(FindPlaces.java:30)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
十二月九号至14号:30:14.047:ERROR / AndroidRuntime(365):18 ...更多
十二月九号至14号:30:14.057:WARN / ActivityManager(58):强制整理活动com.UserLogin / .Innerpage
十二月九号至14号:30:14.594:WARN / ActivityManager(58):活动暂停超时HistoryRecord {4506eef0 com.UserLogin / .Innerpage}
十二月九号至14号:30:16.347:信息/程序(365):发送信号。 PID:365 SIG:9
十二月九号至14号:30:16.367:信息/ ActivityManager(58):过程com.UserLogin(PID 365)已经死亡。
十二月九号至14号:30:16.367:信息/窗口管理器(58):WIN死亡:窗口{4506a6e0 com.UserLogin / com.UserLogin.Innerpage暂停= FALSE}
十二月九号至14号:30:16.367:信息/窗口管理器(58):WIN死亡:窗口{451492d0 com.UserLogin / com.UserLogin.UserLogin暂停= FALSE}
十二月九号至14号:30:16.397:信息/ UsageStats(58):com.android.launcher意外的简历时,如果已经恢复了com.UserLogin
 

的Java文件:

 进口的java.util.ArrayList;

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

公共类FindPlaces扩展ListActivity {

    私人SQLiteDatabase DbLoc;
    ListView的LV;
    私人的ArrayList<场>结果=新的ArrayList<场>();
    @覆盖
    公共无效的onCreate(包savedInstance){
        super.onCreate(savedInstance);
        //setContentView(R.layout.places);
        getallLocs();
        displayLocs();

    }
    私人无效displayLocs(){
        LV =(ListView控件)findViewById(R.id.listPlaces);
        lv.setAdapter(新StudentListAdapter(这一点,R.layout.placeslist,结果));

        //lv.setAdapter(new ArrayAdapter<场>(这一点,android.R.layout.simple_list_item_1,结果));

    }
    类StudentListAdapter扩展ArrayAdapter<场> {
        私人的ArrayList<场> locationDetails;
        私人语境mContext;

        公共StudentListAdapter(上下文的背景下,INT textViewResourceId,ArrayList的<场>的结果){
            超(背景下,textViewResourceId,结果);
            // TODO自动生成构造函数存根
            的System.out.println(构造StudentList适配器......);
            this.locationDetails =结果;
            mContext =背景;
        }


        @覆盖
        公众诠释getCount将(){
            // TODO自动生成方法存根
            返回results.size();
        }

        @覆盖
        公共领域的getItem(INT位置){
            // TODO自动生成方法存根
            返回locationDetails.get(位置);
        }
        @覆盖
        众长getItemId(INT位置){
            // TODO自动生成方法存根
            返回super.getItemId(位置);
        }

        @覆盖
        公共查看getView(INT位置,查看convertView,ViewGroup中父){
            // TODO自动生成方法存根
            视图V = convertView;
            如果(V == NULL){
                LayoutInflater VL =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                V = vl.inflate(R.layout.placeslist,NULL);
            }
                            田O = results.get(位置);

            如果(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();
            返回伏;
        }
    }
    静态类ViewHolder
    {
        TextView的LOCATIONNAME;
        ImageView的Locationimage;
    }
    私人无效getallLocs(){
        // TODO自动生成方法存根
        尝试 {
            DatabaseHelper dbHelper =新DatabaseHelper(
                    this.getApplicationContext());
            DbLoc = dbHelper.getWritableDatabase();
            光标C = DbLoc.rawQuery(选择+ DatabaseHelper.LocationName +,+ DatabaseHelper.LocationImage +FROM
                    + DatabaseHelper.LOCATIONTABLE,NULL);
                            如果(C!= NULL){
                如果(c.moveToFirst()){
                    做 {
                        串LOCATIONNAME = c.getString(c.getColumnIndex(LOCATIONNAME));
                        字符串移动= c.getString(C
                                .getColumnIndex(LocationImage));
                        场P =新的领域(LOCATIONNAME,手机);
                        results.add(对);

                    }而(c.moveToNext());
                }
            }
        }赶上(SQLiteException SE){
            Log.e(的getClass()。getSimpleName(),
            无法创建或打开数据库);
        }
        终于{如果(DbLoc!= NULL)DbLoc.execSQL(DELETE FROM+
                DatabaseHelper.FRIENDTABLE); DbLoc.execSQL(DELETE FROM+
                        DatabaseHelper.LOCATIONTABLE);
                DbLoc.execSQL(DROP TABLE IF EXISTS表名+ DatabaseHelper.FRIENDTABLE);
                DbLoc.execSQL(DROP TABLE IF EXISTS表名+ DatabaseHelper.LOCATIONTABLE);
                DbLoc.close();
        }
    }
}
 

Places.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT机器人:填充=6dip>
    < ListView的机器人:ID =@ + ID / listPlaces机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 

Placeslist.xml

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT>
    < TextView的机器人:ID =@ + ID /的TopText机器人:layout_width =FILL_PARENT
        机器人:layout_height =0dip机器人:layout_weight =1
        机器人:重力=center_vertical/>
    < TextView的机器人:ID =@ + ID / toptext1机器人:layout_width =FILL_PARENT
        机器人:layout_height =0dip机器人:layout_weight =1
        机器人:重力=center_vertical/>
< / LinearLayout中>
 

解决方案

取消注释这一行

 的setContentView(R.layout.places);
 

EDITED

 < XML版本=1.0编码=UTF-8&GT?;
< LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:方向=垂直机器人:layout_width =FILL_PARENT
    机器人:layout_height =FILL_PARENT机器人:填充=6dip>
    < ListView的机器人:ID =@机器人:ID /列表机器人:layout_width =FILL_PARENT
        机器人:layout_height =WRAP_CONTENT/>
< / LinearLayout中>
 

你的文件替换本(Places.xml)

更新时间:

 公共无效的onCreate(包savedInstance){
        super.onCreate(savedInstance);
        //setContentView(R.layout.places);
        getallLocs();
        setListAdapter(新ArrayAdapter<场>(这一点,android.R.layout.simple_list_item_1,结果));
// displayLocs();

    }
    私人无效displayLocs(){
        LV =(ListView控件)findViewById(R.id.listPlaces);
        lv.setAdapter(新StudentListAdapter(这一点,R.layout.placeslist,结果));

        //lv.setAdapter(new ArrayAdapter<场>(这一点,android.R.layout.simple_list_item_1,结果));

    }
 

Hi i'm new bee to android. while executing my application I'm getting nullpoint exception error, dont know why it is happening. please any one help me to get rid from this issue. thanks in advance.

This is my logcat:

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 file:

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>

解决方案

uncomment this line

setContentView(R.layout.places);

EDITED:

<?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>

replace this with your file(Places.xml)

UPDATED

 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));

    }

这篇关于ERROR / AndroidRuntime(335):由:显示java.lang.NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆