点击fragment
时,我想将一个button
替换为另一个。
在Fragment
班级中,他们是listview
。 Listview
逻辑清晰但应用程序崩溃。
我的代码如下。
package com.FragmentClasses;
import java.util.ArrayList;
import com.AdapterClasses.Favourite_sports_List_Adapter;
import com.ModelClasses.Model_Sport_Info;
import com.ModelClasses.Model_User_info;
import com.briggnright.R;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
public class Frag_My_favourite_sports extends Fragment
{
//Declare Variables
View rootView;
ListView list_sport_list;
ArrayList<Model_Sport_Info> arraylist_sport_info;
Model_Sport_Info model_sport_info;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
rootView = inflater.inflate(R.layout.frag_favourite_sports, container, false);//inflate layout on fragment Home Fragment
getActivity().getActionBar().setTitle(" Favourite Sports");
setUI();
arraylist_sport_info =new ArrayList<Model_Sport_Info>();
model_sport_info=new Model_Sport_Info();
model_sport_info.setSport_Image(R.drawable.soccer);
model_sport_info.setSport_Name("Soccer");
arraylist_sport_info.add(model_sport_info);
model_sport_info=new Model_Sport_Info();
model_sport_info.setSport_Image(R.drawable.basketball);
model_sport_info.setSport_Name("Basketball");
arraylist_sport_info.add(model_sport_info);
model_sport_info=new Model_Sport_Info();
model_sport_info.setSport_Image(R.drawable.baseball);
model_sport_info.setSport_Name("Baseball");
arraylist_sport_info.add(model_sport_info);
model_sport_info=new Model_Sport_Info();
model_sport_info.setSport_Image(R.drawable.football);
model_sport_info.setSport_Name("Football");
arraylist_sport_info.add(model_sport_info);
model_sport_info=new Model_Sport_Info();
model_sport_info.setSport_Image(R.drawable.hockey);
model_sport_info.setSport_Name("Hockey");
arraylist_sport_info.add(model_sport_info);
list_sport_list.setAdapter(new Favourite_sports_List_Adapter(getActivity(), arraylist_sport_info));
return rootView;
}
private void setUI()
{
list_sport_list=(ListView)rootView.findViewById(R.id.list_favourite_sports);
}
}
我的适配器类就像,
package com.AdapterClasses;
import java.util.ArrayList;
import com.ModelClasses.Model_Sport_Info;
import com.briggnright.R;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.graphics.Typeface;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class Favourite_sports_List_Adapter extends BaseAdapter
{
private Context context;
private ArrayList<Model_Sport_Info> arraylist_sports_info;
Typeface myTypeface;
public Favourite_sports_List_Adapter(Context context, ArrayList<Model_Sport_Info> arraylist_sports_info){
this.context = context;
this.arraylist_sports_info = arraylist_sports_info;
}
@Override
public int getCount() {
return arraylist_sports_info.size();
}
@Override
public Object getItem(int position) {
return arraylist_sports_info.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
private class ViewHolder
{
TextView txt_sport_name,txt_select_team;
Button btn_checked_unchecked;
RelativeLayout btn_select_team;
ImageView img_sports_image;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View v=convertView;
myTypeface = Typeface.createFromAsset(context.getAssets(),
"fonts/robotoregular.ttf");
if (v == null)
{
LayoutInflater inflator = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflator.inflate(R.layout.list_item_favourite_sport, parent,
false);
ViewHolder holder = new ViewHolder();
holder.txt_sport_name=(TextView)v.findViewById(R.id.txt_list_item_favourute_sports_text);
holder.txt_sport_name.setTypeface(myTypeface);
holder.img_sports_image=(ImageView)v.findViewById(R.id.img_list_item_favourite_sports_image);
holder.btn_checked_unchecked=(Button)v.findViewById(R.id.btn_list_item_favourite_sports_checked);
// holder.btn_select_team=(RelativeLayout)v.findViewById(R.id.rel_btn_list_item_favourite_sports_select_team);
holder.txt_select_team=(TextView)v.findViewById(R.id.txt_btn_list_item_favourite_sports_select_team);
holder.txt_select_team.setTypeface(myTypeface);
v.setTag(holder);
}
final ViewHolder holder = (ViewHolder) v.getTag();
//Set info.
holder.txt_sport_name.setText(arraylist_sports_info.get(position).getSport_Name()+"");
holder.img_sports_image.setImageResource(arraylist_sports_info.get(position).getSport_Image());
holder.btn_checked_unchecked.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v)
{
// holder.btn_checked_unchecked.setBackgroundResource(R.drawable.checked);
// holder.btn_select_team.setBackgroundResource(R.drawable.button_only_rounded_edges);
// holder.txt_select_team.setTextColor(Color.parseColor("#192131"));
}
});
return convertView;
}
public void fragmentSet(int id,Fragment fragment)//Fragment replacing method.
{
FragmentManager fragmentmanager= ((Activity) context).getFragmentManager();
FragmentTransaction ft = fragmentmanager.beginTransaction();
ft.replace(id, fragment).commit();
}
}
它给出了以下错误,
04-18 12:47:49.201: E/AndroidRuntime(8254): FATAL EXCEPTION: main
04-18 12:47:49.201: E/AndroidRuntime(8254): Process: com.briggnright, PID: 8254
04-18 12:47:49.201: E/AndroidRuntime(8254): java.lang.NullPointerException
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.AbsListView.obtainView(AbsListView.java:2265)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.ListView.measureHeightOfChildren(ListView.java:1263)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.ListView.onMeasure(ListView.java:1175)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1052)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.LinearLayout.measureVertical(LinearLayout.java:847)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.support.v4.widget.DrawerLayout.onMeasure(DrawerLayout.java:651)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-18 12:47:49.201: E/AndroidRuntime(8254): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5125)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
04-18 12:47:49.201: E/AndroidRuntime(8254): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.View.measure(View.java:16497)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1916)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1113)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1295)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5670)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.Choreographer.doFrame(Choreographer.java:544)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.os.Handler.handleCallback(Handler.java:733)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.os.Handler.dispatchMessage(Handler.java:95)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.os.Looper.loop(Looper.java:136)
04-18 12:47:49.201: E/AndroidRuntime(8254): at android.app.ActivityThread.main(ActivityThread.java:5017)
04-18 12:47:49.201: E/AndroidRuntime(8254): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 12:47:49.201: E/AndroidRuntime(8254): at java.lang.reflect.Method.invoke(Method.java:515)
04-18 12:47:49.201: E/AndroidRuntime(8254): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-18 12:47:49.201: E/AndroidRuntime(8254): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-18 12:47:49.201: E/AndroidRuntime(8254): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
您需要返回膨胀的视图。 convertView
在您的情况下为空
更改此
return convertView;
到
return v;