我正在使用actionbarsherlock
一个tabhost
片段,在我的一个选项卡中,我显示了一个名为的片段segmentedControlFrag
,其中包括 4 个分段控制按钮(标记为“2hrs”、“8hrs”、“24hrs”和“1wk”)和一个占据其余部分的框架的屏幕。
When one of the buttons from the segmentedcontrol
group is selected a async
request grabs some important info , the frame is replaced with a new (visualFragmentFrag)
fragment which includes the important info from the request in as a extra.
它工作得很好,我可以单击分段控制组中的每个按钮,它们每个都可以加载自己的变体visualTrendsFrag
。我什至可以选择一个完全不同的选项卡并返回,它会加载到我上次选择的分段控件上。
只有当我在以下代码onCreateView
中添加这行代码时,问题才会开始segmentedControlFrag
:
segmentText.check(R.id.button_one);
当我添加这行代码时,logcat 中的问题是与上下文相关的空指针错误。虽然如果我省略上面的行它确实有效,但我怀疑我管理片段的方式segmentedControlFrag
也是错误的。
分段控制片段:
public class TrendsSegmentedControlFrame extends SherlockFragment {
private ImageButton update_btn;
private View view;
private LayoutInflater myInflater;
private ViewGroup myContainer;
SegmentedRadioGroup segmentText;
SegmentedRadioGroup segmentImg;
private String hoursVar="2";
private static final int TWO_HRS = 1;
private static final int EIGHT_HRS = 2;
private static final int TWENTYFOUR_HRS = 3;
private static final int ONE_WEEK = 4;
private int mTabState=0;
@Override
public LayoutInflater getLayoutInflater(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("TrendsSegControlFrag", "getLayoutInflater");
return super.getLayoutInflater(savedInstanceState);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("TrendsSegControlFrag", "onActivityCreated");
super.onActivityCreated(savedInstanceState);
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Log.i("TrendsSegControlFrag", "onCreate");
super.onCreate(savedInstanceState);
}
@Override
public void onDestroy() {
// TODO Auto-generated method stub
Log.i("TrendsSegControlFrag", "onDestroy");
super.onDestroy();
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
Log.i("TrendsSegControlFrag", "onDestroyView");
super.onDestroyView();
}
@Override
public void onPause() {
Log.i("TrendsSegControlFrag", "onPause");
// TODO Auto-generated method stub
super.onPause();
}
@Override
public void onResume() {
Log.i("TrendsSegControlFrag", "onResume");
// TODO Auto-generated method stub
super.onResume();
}
@Override
public void onSaveInstanceState(Bundle outState) {
// TODO Auto-generated method stub
Log.i("TrendsSegControlFrag", "onSaveInstanceState");
super.onSaveInstanceState(outState);
}
@Override
public void onStart() {
// TODO Auto-generated method stub
Log.i("TrendsSegControlFrag", "onStart");
super.onStart();
}
@Override
public void onStop() {
Log.i("TrendsSegControlFrag", "onStop");
// TODO Auto-generated method stub
super.onStop();
}
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.trends_segmented_control_frame, container, false);
segmentText = (SegmentedRadioGroup) view.findViewById(R.id.segment_text);
RadioButton btn = (RadioButton)view.findViewById(R.id.button_one);
segmentText.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
Log.i("TrendsSegControlFrag-onCheckChangedCalled","");
LocationInfo locationInfo = new LocationInfo(getActivity());
String latitude = Float.toString(locationInfo.lastLat);
String longitude = Float.toString(locationInfo.lastLong);
if (checkedId == R.id.button_one) {
Log.i("TrendsSegControlFrag-onCheckChanged","button_one | 2hrs");
hoursVar="2";
RequestParams params= new RequestParams();
params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
params.put("hours", hoursVar);
params.put("long", longitude);
params.put("lat", latitude);
RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, JSONObject arg1) {
//NEED TO ADD CODE IN CASE OF FAIL
}
@Override
public void onSuccess(JSONObject json) {
String trend_count="0";
int trend_count_int=0;
try {
trend_count=json.getString("trend_count");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
trend_count_int=Integer.parseInt(trend_count);
if(trend_count_int>0){
Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");
Fragment visualTrends = new VisualTrendsFrag();
Bundle args = new Bundle();
args.putString("hoursVar", hoursVar);
args.putString("trend_count", trend_count);
visualTrends.setArguments(args);
FragmentManager fm = getChildFragmentManager();
if (fm != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, visualTrends);
ft.commitAllowingStateLoss();
}
}else{
Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
}
}
});
//mToast.setText("Displaying all users you've been near in the last 2 hours");
} else if (checkedId == R.id.button_two) {
hoursVar="8";
Log.i("TrendsSegControlFrag-onCheckChanged","button_two | 8hrs");
RequestParams params= new RequestParams();
params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
params.put("hours", hoursVar);
params.put("long", longitude);
params.put("lat", latitude);
RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, JSONObject arg1) {
//NEED TO ADD CODE IN CASE OF FAIL
}
@Override
public void onSuccess(JSONObject json) {
String trend_count="0";
int trend_count_int=0;
try {
trend_count=json.getString("trend_count");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
trend_count_int=Integer.parseInt(trend_count);
if(trend_count_int>0){
Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");
Fragment visualTrends = new VisualTrendsFrag();
Bundle args = new Bundle();
args.putString("hoursVar", hoursVar);
args.putString("trend_count", trend_count);
visualTrends.setArguments(args);
FragmentManager fm = getChildFragmentManager();
if (fm != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, visualTrends);
ft.commitAllowingStateLoss();
}
}else{
Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
}
}
});
//mToast.setText("Displaying all users you've been near in the last 2 hours");
} else if (checkedId == R.id.button_three) {
hoursVar="24";
Log.i("TrendsSegControlFrag-onCheckChanged","button_three | 24hrs");
RequestParams params= new RequestParams();
params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
params.put("hours", hoursVar);
params.put("long", longitude);
params.put("lat", latitude);
RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, JSONObject arg1) {
//NEED TO ADD CODE IN CASE OF FAIL
}
@Override
public void onSuccess(JSONObject json) {
String trend_count="0";
int trend_count_int=0;
try {
trend_count=json.getString("trend_count");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
trend_count_int=Integer.parseInt(trend_count);
if(trend_count_int>0){
Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");
Fragment visualTrends = new VisualTrendsFrag();
Bundle args = new Bundle();
args.putString("hoursVar", hoursVar);
args.putString("trend_count", trend_count);
visualTrends.setArguments(args);
FragmentManager fm = getChildFragmentManager();
if (fm != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, visualTrends);
ft.commitAllowingStateLoss();
}
}else{
Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
}
}
});
//mToast.setText("Displaying all users you've been near in the last 2 hours");
}else if (checkedId == R.id.button_four) {
hoursVar="168";
Log.i("TrendsSegControlFrag-onCheckChanged","button_four | 168hrs");
RequestParams params= new RequestParams();
params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
params.put("hours", hoursVar);
params.put("long", longitude);
params.put("lat", latitude);
RestClient.post(TabHostFragmentActivity.token,TREND_COUNT_URL, params, new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, JSONObject arg1) {
//NEED TO ADD CODE IN CASE OF FAIL
}
@Override
public void onSuccess(JSONObject json) {
String trend_count="0";
int trend_count_int=0;
try {
trend_count=json.getString("trend_count");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
trend_count_int=Integer.parseInt(trend_count);
if(trend_count_int>0){
Log.i("hoursVar:"+hoursVar+" | trend_count:"+trend_count, "TrendsSegmentedContrlolFrag");
Fragment visualTrends = new VisualTrendsFrag();
Bundle args = new Bundle();
args.putString("hoursVar", hoursVar);
args.putString("trend_count", trend_count);
visualTrends.setArguments(args);
FragmentManager fm = getChildFragmentManager();
if (fm != null) {
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.fragment_content, visualTrends);
ft.commitAllowingStateLoss();
}
}else{
Log.i("trends_count was 0", "TrendsSegmentedContrlolFrag");
}
}
});
//mToast.setText("Displaying all users you've been near in the last 2 hours");
}
}
});
if (savedInstanceState == null) {
segmentText.check(R.id.button_one);
}
//code goes here
return view;
}
}
VisualTrendsFrag(嵌套在segmentedControlFrag框架内的片段:
public class VisualTrendsFrag extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if (getArguments() != null) {
hoursVar=getArguments().getString("hoursVar");
trend_count=getArguments().getString("trend_count");
trend_count_int=Integer.parseInt(trend_count);
Log.i("hoursVar",hoursVar);
Log.i("trend_count",trend_count);
}
Random randomGenerator = new Random();
int randomInt = randomGenerator.nextInt(5);
if(randomInt==0){
view = inflater.inflate(R.layout.trends_linear_layout_ten_a, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","ten_a");
}else if(randomInt==1){
view = inflater.inflate(R.layout.trends_linear_layout_ten_b, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","ten_b");
}else if(randomInt==2){
view = inflater.inflate(R.layout.trends_linear_layout_ten_c, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","ten_c");
}else if(randomInt==3){
view = inflater.inflate(R.layout.trends_linear_layout_ten_d, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","ten_d");
}else if(randomInt==4){
view = inflater.inflate(R.layout.trends_linear_layout_ten_e, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","ten_e");
}else if(randomInt==5){
view = inflater.inflate(R.layout.trends_linear_layout_ten_f, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","ten_f");
}else if(randomInt==6){
view = inflater.inflate(R.layout.trends_linear_layout_ten_g, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","ten_g");
}
if(trend_count_int==0){
view = inflater.inflate(R.layout.trends_linear_layout_empty, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_empty");
}else if(trend_count_int==1){
view = inflater.inflate(R.layout.trends_linear_layout_one_view, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_one_view");
}else if(trend_count_int==2){
view = inflater.inflate(R.layout.trends_linear_layout_two_views, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_two_views");
}else if(trend_count_int==3){
view = inflater.inflate(R.layout.trends_linear_layout_three_views, container, false);
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_three_views");
}else if(trend_count_int==4){
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_four_views");
view = inflater.inflate(R.layout.trends_linear_layout_four_views, container, false);
}else if(trend_count_int==5){
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_five_views");
view = inflater.inflate(R.layout.trends_linear_layout_five_views, container, false);
}else if(trend_count_int==6){
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_six_views");
view = inflater.inflate(R.layout.trends_linear_layout_six_views, container, false);
}else if(trend_count_int==7){
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_seven_views");
view = inflater.inflate(R.layout.trends_linear_layout_seven_views, container, false);
}else if(trend_count_int==8){
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_eight_views");
view = inflater.inflate(R.layout.trends_linear_layout_eight_views, container, false);
}else if(trend_count_int==9){
Log.i("VisualTrendsFrag | Selected Layout: ","trends_linear_layout_nine_views");
view = inflater.inflate(R.layout.trends_linear_layout_nine_views, container, false);
}
Log.i("trend_count_int:",String.valueOf(trend_count_int));
if(trend_count_int>=1){
TrendBtn1 = (Button)view.findViewById(R.id.Button01);
TrendBtn1.setVisibility(TrendBtn1.GONE);
}
if(trend_count_int>=2){
TrendBtn2 = (Button)view.findViewById(R.id.Button02);
TrendBtn2.setVisibility(TrendBtn2.GONE);
}
if(trend_count_int>=3){
TrendBtn3 = (Button)view.findViewById(R.id.Button03);
TrendBtn3.setVisibility(TrendBtn3.GONE);
}
if(trend_count_int>=4){
TrendBtn4 = (Button)view.findViewById(R.id.Button04);
TrendBtn4.setVisibility(TrendBtn4.GONE);
}
if(trend_count_int>=5){
TrendBtn5 = (Button)view.findViewById(R.id.Button05);
TrendBtn5.setVisibility(TrendBtn5.GONE);
}
if(trend_count_int>=6){
TrendBtn6 = (Button)view.findViewById(R.id.Button06);
TrendBtn6.setVisibility(TrendBtn6.GONE);
}
if(trend_count_int>=7){
TrendBtn7 = (Button)view.findViewById(R.id.Button07);
TrendBtn7.setVisibility(TrendBtn7.GONE);
}
if(trend_count_int>=8){
TrendBtn8 = (Button)view.findViewById(R.id.Button08);
TrendBtn8.setVisibility(TrendBtn8.GONE);
}
if(trend_count_int>=9){
TrendBtn9 = (Button)view.findViewById(R.id.Button09);
TrendBtn9.setVisibility(TrendBtn9.GONE);
}
if(trend_count_int>=10){
TrendBtn10 = (Button)view.findViewById(R.id.Button10);
TrendBtn10.setVisibility(TrendBtn10.GONE);
}
token = TabHostFragmentActivity.token;
LocationInfo locationInfo = new LocationInfo(getActivity());
String latitude = Float.toString(locationInfo.lastLat);
String longitude = Float.toString(locationInfo.lastLong);
Log.i("Lat", latitude);
Log.i("long", longitude);
Log.i("VisualTrends is attempting to make a request",hoursVar);
Log.i("HOURS VAR", hoursVar);
try {
new Requests().refreshTrends(hoursVar);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//code goes here
return view;
}
class Requests {
public void refreshTrends(String hours) throws JSONException {
LocationInfo locationInfo = new LocationInfo(getSherlockActivity());
String latitude = Float.toString(locationInfo.lastLat);
String longitude = Float.toString(locationInfo.lastLong);
Log.i("Lat", latitude);
Log.i("long", longitude);
pdialog = ProgressHUD.show(getSherlockActivity(),"Loading", false,false);
// Creating JSON Parser instance
RequestParams params = new RequestParams();
params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
params.put("lat", latitude);
params.put("long", longitude);
params.put("hours", hours);
RestClient.post(token,TRENDS_URL, params, new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, JSONObject arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
}
@Override
public void onSuccess(JSONObject json) {
// Pull out the first event on the public timeline
pdialog.dismiss();
try {
trends = json.getJSONArray("trends");
//int num_of_trends = Integer.parseInt(json.getString("trends_count"));
//int num_of_views=10;
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/*if(trends == null){
final LinearLayout main_layout = (LinearLayout) findViewById(R.id.my_main_layout);
main_layout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
noFriends_tv = (TextView) findViewById(R.id.noFriends_tv);
noFriends_tv.setTextColor(getResources().getColor(android.R.color.white));
noFriends_tv.setText("No topics or hashtags trending nearby currently. Post a status using a hashtag to start a local trend!");
noFriends_tv.setVisibility(noFriends_tv.VISIBLE);
}*/
if(trends != null){
try{
Log.i("friends response", trends.toString());
// looping through All Contacts
for(int i = 0; i < trends.length(); i++){
JSONObject c = trends.getJSONObject(i);
// Storing each json item in variable
String hashtag = c.getString("hashtag");
String count = c.getString("COUNT( * )");
int trendObjectCount=i+1;
if(trendObjectCount==1){
TrendBtn1.setText(hashtag+"("+count+")");
TrendBtn1.setVisibility(TrendBtn1.VISIBLE);
TrendBtn1.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==2){
TrendBtn2.setText(hashtag+"("+count+")");
TrendBtn2.setVisibility(TrendBtn2.VISIBLE);
TrendBtn2.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==3){
TrendBtn3.setText(hashtag+"("+count+")");
TrendBtn3.setVisibility(TrendBtn3.VISIBLE);
TrendBtn3.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==4){
TrendBtn4.setText(hashtag+"("+count+")");
TrendBtn4.setVisibility(TrendBtn4.VISIBLE);
TrendBtn4.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==5){
TrendBtn5.setText(hashtag+"("+count+")");
TrendBtn5.setVisibility(TrendBtn5.VISIBLE);
TrendBtn5.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==6){
TrendBtn6.setText(hashtag+"("+count+")");
TrendBtn6.setVisibility(TrendBtn6.VISIBLE);
TrendBtn6.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==7){
TrendBtn7.setText(hashtag+"("+count+")");
TrendBtn7.setVisibility(TrendBtn7.VISIBLE);
TrendBtn7.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==8){
TrendBtn8.setText(hashtag+"("+count+")");
TrendBtn8.setVisibility(TrendBtn8.VISIBLE);
TrendBtn8.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==9){
TrendBtn9.setText(hashtag+"("+count+")");
TrendBtn9.setVisibility(TrendBtn9.VISIBLE);
TrendBtn9.setOnClickListener(new quickActionMenu(hashtag));
}
if(trendObjectCount==10){
TrendBtn10.setText(hashtag+"("+count+")");
TrendBtn10.setVisibility(TrendBtn10.VISIBLE);
TrendBtn10.setOnClickListener(new quickActionMenu(hashtag));
}
}
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
}
}
public class quickActionMenu implements OnClickListener{
private int position;
private String clicked_uid;
QuickAction mQuickAction ;
String selectedTopic;
public quickActionMenu(String topic){
mQuickAction = new QuickAction(getActivity());
selectedTopic=topic;
ActionItem viewStatuses = new ActionItem(ID_VIEW, "View Statuses", getResources().getDrawable(R.drawable.ic_add));
ActionItem hashtagSubscribe = new ActionItem(ID_SUBSCRIBE, "Subscribe", getResources().getDrawable(R.drawable.ic_up));
mQuickAction.addActionItem(viewStatuses);
mQuickAction.addActionItem(hashtagSubscribe);
//setup the action item click listener
mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
private TrendsAdapter hashtagAdapter;
@Override
public void onItemClick(QuickAction quickAction, int pos, int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos);
if (actionId == ID_VIEW) {
FragmentManager fm = getSherlockActivity().getSupportFragmentManager();
HashtagSearchFeedDialog hashDialog = new HashtagSearchFeedDialog().newInstance(selectedTopic);
hashDialog.show(fm, "dialog");
} else if(actionId == ID_SUBSCRIBE){
pdialog = ProgressHUD.show(getSherlockActivity(),"Loading", false,false);
RequestParams params = new RequestParams();
params.put("loggedin_uid", TabHostFragmentActivity.loggedin_uid);
params.put("hashtag", selectedTopic);
RestClient.post(token,SUBSCRIBE_URL, params, new JsonHttpResponseHandler() {
@Override
public void onFailure(Throwable arg0, JSONObject arg1) {
// TODO Auto-generated method stub
super.onFailure(arg0, arg1);
pdialog.dismiss();
Log.i("Request Failed:", arg1.toString());
Toast.makeText(getSherlockActivity(), arg1.toString() , Toast.LENGTH_LONG).show();
}
@Override
public void onSuccess(JSONObject requestResponse) {
pdialog.dismiss();
if(requestResponse!=null){
Toast.makeText(getSherlockActivity(),
selectedTopic+" has been added to your hashtag subscriptions", Toast.LENGTH_LONG).show();
}
}
});
}
}
});
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
mQuickAction.show(v);
Log.i("Hashtag Selected:"+selectedTopic, "VisualTrendsFrag");
}
}
}
编辑:我开始认为它不是一个上下文问题,因为它是一个 LayoutInflator 问题,这是我使用的导致“下面的 logcat 中的弹出窗口错误”的库: https ://github.com/lorensiuswlt/NewQuickAction