花你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);//这一句一直报空指针
把你的R.layout.fragment_privilege代码贴出来
######这个就是 getActivity()为空,在fragment使用getActivity()经常遇到为空的情况.原因可以仔细追究下
######list = (ListView) view.findViewById(R.id.privile_list);
得到的是空?
######list = (ListView) view.findViewById(R.id.privile_list);
得到的是空?
list null
######应该是list.setAdapter这个方法里报的异常吧,跟踪下不就知道了?
加个日志看下结果
######list.setadpater是安卓的方法,要说里面没东西我也做了输出,有东西输出啊,而且那个array已经传到我写的baseadpater里面了,我也做了输出,最有可能的就是getActivity()这个是有错误的,但继承了Fragment不是就是这么用的么?######应该是list.setAdapter这个方法里报的异常吧,跟踪下不就知道了?
加个日志看下结果
final PrivilelistAdpater privilelistAdpater = new PrivilelistAdpater(item1, |
068 | getActivity()); |
069 | System.out.println("..............................."+privilelistAdpater); |
070 | // list.setAdapter(privilelistAdpater); |
071 | list.setAdapter(privilelistAdpater);//这一句一直报空指针 |
debug一下不就知道了,应该是list是空的
######debug
版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。