我的代码如下:
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");
}
});
}
我想点击它时更改 favourite_icon ImageView
的图片。但是,只要点击图标,我就会 NullPointerException
。点击 check_in_img 的原因是什么,但点击 favourite_icon 无效,即使当前布局是包含 favourite_icon 的布局? SO的其他来源说要检查相同的内容。
答案 0 :(得分:1)
favourite_icon
是 recco_list.xml 的一部分,您正试图在 activity_for_me.xml 中找到它。视图在那里不存在,因此也是例外。
R.id.favourite_icon
中没有 activity_for_me.xml
。
如果你想访问listview中的项目,我建议你阅读ListView和getView()。