开发者社区> 问答> 正文

花你1分钟来审阅一下我的代码哪里出错了? 400 报错

花你1分钟来审阅一下我的代码哪里出错了? 400 报错

package com.hkkj.csrx.fragment;

import java.util.ArrayList; import java.util.HashMap;

import android.content.Intent; import android.graphics.drawable.BitmapDrawable; import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.PopupWindow; import android.widget.AdapterView.OnItemClickListener; import android.widget.TextView; import com.hkkj.csrx.activity.Privileinfo; import com.hkkj.csrx.activity.R; import com.hkkj.csrx.adapter.PrivilelistAdpater;

public class PrivilegeFragment extends Fragment { private View view; ListView listView, list; private LinearLayout layout; private PopupWindow popupWindow; TextView area, sort; private String title[] = { "全部", "我的微博", "周边", "智能排版", "同学" };

public View onCreateView(LayoutInflater inflater, ViewGroup container,
		Bundle savedInstanceState) {
	view = inflater.inflate(R.layout.fragment_privilege, container, false);
	listView = (ListView) view.findViewById(R.id.lv_dialog);
	list = (ListView) view.findViewById(R.id.privile_list);
	area = (TextView) view.findViewById(R.id.pop_lei);
	sort = (TextView) view.findViewById(R.id.pop_lei2);
	area.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View arg0) {
			area.getBottom();
			int y = 40;
			int x = 0;
			showPopupWindow(x, y);

		}
	});
	sort.setOnClickListener(new OnClickListener() {

		@Override
		public void onClick(View v) {
			area.getTop();
			int y = 40;
			int x = 0;
			showPopupWindow(x, y);
		}
	});
	// 优惠列表
	ArrayList<HashMap<String, String>> item1 = new ArrayList<HashMap<String, String>>();

	HashMap<String, String> item2 = new HashMap<String, String>();
	item2.put("textItems", "迪奥咖啡北环路店");
	item1.add(item2);
	PrivilelistAdpater privilelistAdpater = new PrivilelistAdpater(item1,
			getActivity());
	System.out.println("..............................."+privilelistAdpater);
	// list.setAdapter(privilelistAdpater);
	list.setAdapter(privilelistAdpater);//这一句一直报空指针
	return view;
}

public void showPopupWindow(int x, int y) {
	layout = (LinearLayout) LayoutInflater.from(getActivity()).inflate(
			R.layout.privile, null);
	listView = (ListView) layout.findViewById(R.id.lv_dialog);
	listView.setAdapter(new ArrayAdapter<String>(getActivity(),
			R.layout.privile_txt, R.id.tv_text, title));

	popupWindow = new PopupWindow(getActivity());
	popupWindow.setBackgroundDrawable(new BitmapDrawable());
	popupWindow.setWidth(getActivity().getWindowManager()
			.getDefaultDisplay().getWidth());
	popupWindow.setHeight(getActivity().getWindowManager()
			.getDefaultDisplay().getHeight());
	popupWindow.setOutsideTouchable(true);
	popupWindow.setFocusable(true);
	popupWindow.setContentView(layout);
	// showAsDropDown会把里面的view作为参照物,所以要那满屏幕parent
	popupWindow.showAsDropDown(view.findViewById(R.id.pop_lei), x, y);
	listView.setOnItemClickListener(new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
				long arg3) {
			area.setText(title[arg2]);
			Intent intent = new Intent();
			intent.setClass(getActivity(), Privileinfo.class);
			PrivilegeFragment.this.startActivity(intent);
			popupWindow.dismiss();
			popupWindow = null;
		}
	});
}

}

list.setAdapter(privilelistAdpater);//这一句一直报空指针






展开
收起
爱吃鱼的程序员 2020-05-30 21:32:35 400 0
1 条回答
写回答
取消 提交回答
  • https://developer.aliyun.com/profile/5yerqm5bn5yqg?spm=a2c6h.12873639.0.0.6eae304abcjaIB

    把你的R.layout.fragment_privilege代码贴出来

    ######

    这个就是 getActivity()为空,在fragment使用getActivity()经常遇到为空的情况.原因可以仔细追究下

    ######

    list = (ListView) view.findViewById(R.id.privile_list);

    得到的是空?

    ######

    引用来自“那年秋天”的评论

    list = (ListView) view.findViewById(R.id.privile_list);

    得到的是空?

    不是,这个id是有的######

    list null

    ######

    应该是list.setAdapter这个方法里报的异常吧,跟踪下不就知道了?

    加个日志看下结果

    ######list.setadpater是安卓的方法,要说里面没东西我也做了输出,有东西输出啊,而且那个array已经传到我写的baseadpater里面了,我也做了输出,最有可能的就是getActivity()这个是有错误的,但继承了Fragment不是就是这么用的么?######

    引用来自“ZYud”的评论

    应该是list.setAdapter这个方法里报的异常吧,跟踪下不就知道了?

    加个日志看下结果

    log输出######

     final PrivilelistAdpater privilelistAdpater = new PrivilelistAdpater(item1,
    068                 getActivity());
    069         System.out.println("..............................."+privilelistAdpater);
    070         // list.setAdapter(privilelistAdpater);
    071         list.setAdapter(privilelistAdpater);//这一句一直报空指针

    ######

    debug一下不就知道了,应该是list是空的

    ######

    debug

    2020-05-30 21:32:36
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
《0代码搭应用》 立即下载
不止代码 立即下载
低代码开发师(初级)实战教程 立即下载