我在这里要做的是 -
使用asynctask向php发送请求
从php
接收数据并发送回活动
json
数据填充listview
使用适配器listview
项目时会显示contextmenu
但我收到以下错误!请帮忙。
contactActivity.java
public class contactActivity extends Activity{
private List<CNList> imlist = new ArrayList<CNList>();
private ListView listView;
private contactAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.contact);
getLayoutInflater().inflate(R.layout.contact, null);
Intent intent = getIntent();
listView = (ListView)findViewById(R.id.listnum);
adapter = new contactAdapter(this, imlist);
listView.setAdapter(adapter);
registerForContextMenu(listView);
new contactPHP(this).execute();
}
public void contactPhpResponse(JSONArray json,ArrayList<String> numbersList,ArrayList<String> nameList,ArrayList<Uri> imageList){
for (int i = 0; i < json.length(); i++) {
//some jobs
}
adapter.notifyDataSetChanged();
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
CNList obj = (CNList)v.getTag();
int subs = obj.getsubstance();
menu.setHeaderTitle(obj.getname());
menu.add(0, v.getId(), 0, "Add");
menu.add(0, v.getId(), 0, "Block");
}
}
contactPHP.java onPostExecute()
protected void onPostExecute(JSONArray json) {
main.contactPhpResponse(json,numbersList,nameList,imageList);
}
logcat的
03-04 17:15:40.043: E/AndroidRuntime(10010): FATAL EXCEPTION: main
03-04 17:15:40.043: E/AndroidRuntime(10010): Process: com.example.soc, PID: 10010
03-04 17:15:40.043: E/AndroidRuntime(10010): java.lang.StackOverflowError
03-04 17:15:40.043: E/AndroidRuntime(10010): at
logcat的
03-04 17:24:18.193: E/AndroidRuntime(10798): FATAL EXCEPTION: main
03-04 17:24:18.193: E/AndroidRuntime(10798): Process: com.example.soc, PID: 10798
03-04 17:24:18.193: E/AndroidRuntime(10798): java.lang.NullPointerException
03-04 17:24:18.193: E/AndroidRuntime(10798): at com.example.soc.contactActivity.onCreateContextMenu(contactActivity.java:98)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.view.View.createContextMenu(View.java:8356)
03-04 17:24:18.193: E/AndroidRuntime(10798): at com.android.internal.view.menu.ContextMenuBuilder.show(ContextMenuBuilder.java:81)
03-04 17:24:18.193: E/AndroidRuntime(10798): at com.android.internal.policy.impl.PhoneWindow$DecorView.showContextMenuForChild(PhoneWindow.java:2344)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.view.ViewGroup.showContextMenuForChild(ViewGroup.java:658)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.widget.AbsListView.performLongPress(AbsListView.java:2977)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2921)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.os.Handler.handleCallback(Handler.java:733)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.os.Handler.dispatchMessage(Handler.java:95)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.os.Looper.loop(Looper.java:136)
03-04 17:24:18.193: E/AndroidRuntime(10798): at android.app.ActivityThread.main(ActivityThread.java:5021)
03-04 17:24:18.193: E/AndroidRuntime(10798): at java.lang.reflect.Method.invokeNative(Native Method)
03-04 17:24:18.193: E/AndroidRuntime(10798): at java.lang.reflect.Method.invoke(Method.java:515)
03-04 17:24:18.193: E/AndroidRuntime(10798): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:827)
03-04 17:24:18.193: E/AndroidRuntime(10798): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:643)
03-04 17:24:18.193: E/AndroidRuntime(10798): at dalvik.system.NativeStart.main(Native Method)
上面for-loop
contactPhpResponse()
内的CNList和viewtags
for (int i = 0; i < json.length(); i++) {
try {
JSONObject c = json.getJSONObject(i);
CNList iml = new CNList();
String name,phoneNumber = numbersList.get(i);
//long contid;
int avl = Integer.parseInt(c.getString("avl")),subs = Integer.parseInt(c.getString("subs"));
if(avl == 1){
name = c.getString("name");
String uri = c.getString("pp");
iml.setppUrl(uri);
}else{
name = nameList.get(i);
Uri uri = imageList.get(i);
iml.setppUrl(uri);
}
iml.setname(name);
iml.setsubstance(subs);
iml.setnumber(phoneNumber);
iml.setavl(avl);
imlist.add(iml);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
setTag()
位于另一个适配器类
其getView()
方法
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// getting data for the row
CNList m = listItems.get(position);
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null){
if(getItemViewType(position)==0){
convertView = inflater.inflate(R.layout.notavl_cont, null);
}else{
convertView = inflater.inflate(R.layout.avl_cont, null);
}
}
//if (imageLoader == null)
//imageLoader = AppController.getInstance().getImageLoader();
ImageView pp = (ImageView) convertView
.findViewById(R.id.cn_pp);
TextView nme = (TextView) convertView.findViewById(R.id.cn_name);
TextView num = (TextView) convertView.findViewById(R.id.cn_num);
// thumbnail image
//pp.setImageUrl(m.getppUrl(), imageLoader);
if(m.getavl() == 0){
/*Bitmap result = ImageHelper.getRoundedCornerBitmap(m.getppUrl(),80);
pp.setImageBitmap(result);*/
pp.setImageURI(m.getppURI());
}else{
new ImageLoadTask(m.getppUrl(), pp).execute();
}
// content
nme.setText(m.getname());
num.setText(m.getnumber());
convertView.setTag(m);
return convertView;
}