好的,我用3张卡制作了基本的列表视图。 但点击任何一张牌似乎都无法开启新活动。 这个问题是什么? 我如何让一张卡片开启一项新活动? android:onClick在卡片视图的xml文件中设置。
我的片段:
public class Home_fragment extends Fragment {
private ListView mListView;
private cardAdapter itemAdapter;
private CardView mCardView;
public Home_fragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
mCardView = (CardView) rootView.findViewById(R.id.cardview);
mListView = (ListView) rootView.findViewById(R.id.list);
itemAdapter = new cardAdapter(getActivity());
mListView.setAdapter(itemAdapter);
mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(getActivity(), Events_Activity.class);
startActivity(intent);
}
});
return rootView;
}
}
class cardAdapter extends BaseAdapter{
private Context context;
String[] menu;
int images[] = {R.drawable.events,R.drawable.workshops,R.drawable.seminars};
public cardAdapter(Context context){
this.context = context;
menu = context.getResources().getStringArray(R.array.Home_menu);
}
@Override
public int getCount() {
return menu.length;
}
@Override
public Object getItem(int i) {
return menu[i];
}
@Override
public long getItemId(int i) {
return i;
}
@Override
public View getView(int i, View view, ViewGroup viewGroup) {
View row = null;
if(view == null){
LayoutInflater inflater = (LayoutInflater) context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.cards_menu,viewGroup,false);
}
else
{
row = viewGroup;
}
TextView titleTextView = (TextView) row.findViewById(R.id.cardtext);
ImageView titleImageView = (ImageView) row.findViewById(R.id.cardimage);
titleTextView.setText(menu[i]);
titleImageView.setImageResource(images[i]);
return row; }
}
编辑:添加了logcat
12-19 13:27:40.830 2204-2204/com.cambridge.patils.citech E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.cambridge.patils.citech, PID: 2204
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cambridge.patils.citech/com.cambridge.patils.citech.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.cambridge.patils.citech.Home_fragment.onCreateView(Home_fragment.java:55)
at android.app.Fragment.performCreateView(Fragment.java:2053)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:894)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067)
at android.app.BackStackRecord.run(BackStackRecord.java:833)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452)
at android.app.Activity.performStart(Activity.java:5948)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)