简体   繁体   English

为什么我在ImageView上收到NullPointerException?

[英]Why I am getting NullPointerException on ImageView?

My code is as follows: 我的代码如下:

activity_for_me.xml

<RelativeLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

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

     <ImageView
         android:id="@+id/checkin"
         android:layout_width="wrap_content"
         android:layout_height="80dp"
         android:layout_alignParentBottom="true"
         android:layout_centerHorizontal="true"
         android:layout_marginBottom="20dp"
         android:onClick="ClickCheckIn"
         android:src="@drawable/checkin" />

</RelativeLayout>

recco_list.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@layout/nine_patch" >

     <ImageView 
        android:id="@+id/outlet_icon"
        android:layout_marginLeft="15dp"
        android:layout_height="55dp"
        android:layout_width="55dp"
        android:layout_marginTop="10dp"/>


     <TextView
        android:id="@+id/distance"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_marginLeft="15dp"
        android:hint="Distance"
        />
    <TextView
        android:id="@+id/outlet_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_toRightOf="@id/outlet_icon"
        android:layout_toLeftOf="@id/distance"
        android:layout_marginLeft = "15dip"
        android:hint="outlet"
        />

    <TextView
        android:id="@+id/reward"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/outlet_name"
        android:layout_toRightOf="@id/outlet_icon"
        android:layout_toLeftOf="@id/distance"
        android:layout_marginLeft = "15dip"
        android:layout_marginTop="5dp"
        android:textColor="@color/abc_search_url_text_holo"
        android:hint="Reward"
        />

     <View
         android:id="@+id/rule"
         android:layout_below="@id/reward"
         android:layout_toRightOf="@id/outlet_icon"
         android:layout_toLeftOf="@id/distance"
         android:layout_width="fill_parent"
         android:layout_marginLeft = "15dip"
         android:layout_height="1dp"
         android:background="#c0c0c0"/>

       <ImageView 
         android:id="@+id/favourite_icon"
         android:layout_below="@id/rule"
         android:layout_marginLeft="15dp"
         android:layout_marginTop="10dp"
         android:layout_height="15dp"
         android:layout_width="15dp"
         android:src="@drawable/heart_empty"/>

    <ImageView
        android:id="@+id/loc_icon"
        android:layout_width="25dp"
        android:layout_height="25dp" 
        android:layout_below="@id/rule"
        android:layout_marginLeft = "15dip"
        android:layout_marginTop="5dp"
        android:layout_toRightOf="@id/outlet_icon"
        android:src="@drawable/map_pointer"/>

     <TextView
        android:id="@+id/locality"
        android:layout_toRightOf="@id/loc_icon"
        android:layout_toLeftOf="@id/distance"
        android:layout_below="@id/rule"
        android:layout_marginRight="15dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft = "5dip"
        android:layout_marginTop="5dp"
        android:hint="Locality"
        />


</RelativeLayout>

Activity.java

public class ForMeActivity extends Fragment {


    @SuppressLint("ClickableViewAccessibility")
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        RelativeLayout rootView = (RelativeLayout) inflater.inflate(
                R.layout.activity_for_me, container, false);

        ImageView favourite_heart = (ImageView) rootView
                .findViewById(R.id.favourite_icon);

    favourite_heart.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//          Toast.makeText(YourActivityName.this,
//                  "The favorite list would appear on clicking this icon",
//                  Toast.LENGTH_LONG).show();
            System.out.println("Favourite Icon clicked");
        }
    });

        list = (ListView) rootView.findViewById(R.id.recco_list);
        ImageView check_in_img = (ImageView) rootView
                .findViewById(R.id.checkin);
        ImageView outlet_logo = (ImageView) rootView
                .findViewById(R.id.outlet_icon);
        final String data = getArguments().getString("data");
        ForMeFile = new File(getActivity().getExternalFilesDir(filepath),
                filename);

        final String cookie = getArguments().getString("cookie");
        System.out.print(cookie);

        check_in_img.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent in = new Intent(getActivity(), CheckInView.class);
                startActivity(in);
                System.out.println("Checkin Icon clicked");
            }
        });
}

I want to change image for favourite_icon ImageView when I click on it. 我想在单击favourite_icon ImageView时更改图像。 But as soon as I click on the icon I get NullPointerException . 但是,一旦我单击该图标,我就会得到NullPointerException What is the reason that click of check_in_img is working but click of favourite_icon is not working even though the current layout is the one which contains favourite_icon ? 尽管当前布局是包含favourite_icon的布局,但check_in_img的单击仍可正常工作,而favourite_icon的单击却无法正常工作的原因是什么? Other sources of SO says to check the same. SO的其他来源说要检查相同。

favourite_icon is a part of recco_list.xml and you are trying to find it in activity_for_me.xml . favourite_iconrecco_list.xml的一部分,您正在尝试在activity_for_me.xml中找到它。 The view does not exist there and hence, the exception. 该视图不存在,因此是例外。

R.id.favourite_icon is not there in activity_for_me.xml . R.id.favourite_iconactivity_for_me.xml不存在。

And if you want to access the items in the listview, I suggest you read about ListView and getView(). 而且,如果您想访问列表视图中的项目,建议您阅读有关ListView和getView()的文章。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM