Java.NullPointerException Android

时间:2014-07-02 04:59:06

标签: java android

我在运行此代码时遇到问题,因为它在name.setText(cn.getName())中给出了java.nullpointerexception。我想要做的是用sqlite数据库中的文本和图像填充listview。

这是主要活动类:

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

        db = new DatabaseHandler(this);

        int c = db.getItemsCount(); 

        List<ItemList> items = db.getAllItems();      

        View header = (View)getLayoutInflater().inflate(R.layout.productlistview, null);
        TextView name = (TextView)header.findViewById(R.id.name);
        ImageView img = (ImageView)header.findViewById(R.id.photo);
        TextView desc = (TextView)header.findViewById(R.id.description);

        for (ItemList cn : items) 
        {   
            String imageURL = cn.getPhoto();         
            String fileName = imageURL.substring( imageURL.lastIndexOf('/')+1 );
            String imgfile = null;
            String imgURL = null;
            try {
                imgfile = URLEncoder.encode(fileName, "UTF-8").replace("+", "%20");
                imgURL = ("http://www.khind.com.my/i_prod/" + imgfile);
            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }

            name.setText(cn.getName());
            Bitmap bitmap = DownloadImage(imgURL);
            img.setImageBitmap(bitmap);
            desc.setText(cn.getDescription());

            listView.addHeaderView(header);

            Log.d("Here the data:", cn.getName());
        }

这是日志:

07-02 01:01:59.294: E/AndroidRuntime(885): FATAL EXCEPTION: main
07-02 01:01:59.294: E/AndroidRuntime(885): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jmsb.khind/com.jmsb.khind.Fanlist}: java.lang.NullPointerException
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.os.Looper.loop(Looper.java:137)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.ActivityThread.main(ActivityThread.java:5103)
07-02 01:01:59.294: E/AndroidRuntime(885):  at java.lang.reflect.Method.invokeNative(Native Method)
07-02 01:01:59.294: E/AndroidRuntime(885):  at java.lang.reflect.Method.invoke(Method.java:525)
07-02 01:01:59.294: E/AndroidRuntime(885):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
07-02 01:01:59.294: E/AndroidRuntime(885):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
07-02 01:01:59.294: E/AndroidRuntime(885):  at dalvik.system.NativeStart.main(Native Method)
07-02 01:01:59.294: E/AndroidRuntime(885): Caused by: java.lang.NullPointerException
07-02 01:01:59.294: E/AndroidRuntime(885):  at com.jmsb.khind.Fanlist.onCreate(Fanlist.java:111)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.Activity.performCreate(Activity.java:5133)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
07-02 01:01:59.294: E/AndroidRuntime(885):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
07-02 01:01:59.294: E/AndroidRuntime(885):  ... 11 more

这是产品列表的xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
android:id="@+id/relativelayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="@drawable/background01" >

<RelativeLayout

    android:id="@+id/relativelayout2"
    android:layout_width="wrap_content"
    android:layout_height="2dip"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" 
    android:background="@drawable/divider01"/>

<RelativeLayout

    android:id="@+id/relativelayout3"
    android:layout_width="wrap_content"
    android:layout_height="40dip"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" >

    <TextView

        android:id="@+id/name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="10dip"
        android:text="Model Name"
        android:textStyle="bold"
        android:textColor="#FFFFFF"
        android:textSize="18sp" />

</RelativeLayout>

<RelativeLayout

    android:id="@+id/relativelayout4"
    android:layout_width="wrap_content"
    android:layout_height="2dip"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true" 
    android:background="@drawable/divider01"
    android:layout_marginTop="38dip" />

<ScrollView
    android:id="@+id/scroll"
    android:layout_width="wrap_content"
    android:layout_height="fill_parent"
    android:layout_below="@+id/relativelayout4">

    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/photo"
            android:layout_width="150dip"
            android:layout_height="150dip"
            android:layout_alignParentLeft="true"
            android:layout_marginTop="20dp"
            android:layout_marginLeft="20dp"
            android:src="@drawable/no_image" />

        <RelativeLayout

            android:id="@+id/divide"
            android:layout_width="wrap_content"
            android:layout_height="2dip"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true" 
            android:layout_below="@+id/photo"
            android:layout_marginTop="20dp"
            android:background="@drawable/divider01"/>

        <TextView

            android:id="@+id/description"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/divide"
            android:layout_marginLeft="20dip"
            android:layout_marginTop="20dip"
            android:text="Description Here"
            android:textColor="#FFFFFF"
            android:textSize="13sp" />

    </RelativeLayout>
</ScrollView>

这是listview

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">   

<ListView
    android:id="@+id/productlistview"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

<RelativeLayout
    android:id="@+id/bottombar"
    android:layout_width="wrap_content"
    android:layout_height="60dip"
    android:layout_alignParentBottom="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentRight="true"
    android:background="@drawable/tab01_bottom" >

    <Button
        android:id="@+id/tab1"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_toLeftOf="@+id/tab2"
        android:background="@drawable/button04home" />

    <Button
        android:id="@+id/tab2"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_centerHorizontal="true"
        android:layout_marginLeft="40dip"
        android:layout_marginRight="40dip"
        android:background="@drawable/button03product" />

    <Button
        android:id="@+id/tab3"
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:layout_toRightOf="@+id/tab2"
        android:background="@drawable/button02contact" />
</RelativeLayout>

3 个答案:

答案 0 :(得分:0)

在命令行中使用adb logcat,在NullPointerException中使用grep,您应该能够看到堆栈跟踪,您将找到导致异常的行号。

在该行,只需在执行代码之前检查(X!= null)。 了解如何跟踪NPE对于调试应用程序非常基础和至关重要

答案 1 :(得分:0)

  

您已传递相同的资源ID

listView = (ListView) findViewById(R.id.productlistview);
View header = (View)getLayoutInflater().inflate(R.layout.productlistview, null);
  

将此更改为

listView = (ListView) findViewById(id1,null);//id1 for second xml specified
View header = (View)getLayoutInflater().inflate(id2, null);//id2 for first xml specified

答案 2 :(得分:0)

使用logcat尝试找出发生此异常的确切行号。为了避免NullPointerException,你可以捕获它你可以添加检查空值的条件。

try{
     //place code which causes exception
  }catch(NullPointerException e)
  {
  }

如果(一个!= NULL)
    //然后只执行导致异常的代码。