用tabhost做了3个界面可切换的那种第二个界面我用listfragment从网络或其数据当一二界面来回切换多次时就会报错说adapter空指针 how can tell me
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.example.info.UserInfo;
import com.example.service.MsgService;
import com.util.Tools.UrlServerTool;
import android.R.string;
import android.annotation.SuppressLint;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Toast;
@SuppressLint("HandlerLeak")
public class Fragment2 extends ListFragment implements OnClickListener {
//2014-3-18添加按钮发布事件
private Button btn;
private List<HashMap<String,String>> date;
private SimpleAdapter adapter;
private EditText editText;
private Thread mythread;
private Handler handler=new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==0)
{
//msg.obj是获取handler发送信息传来的数据
@SuppressWarnings("unchecked")
List<HashMap<String,String>> date=( List<HashMap<String,String>>) msg.obj;
if(date!=null)
{
adapter=new SimpleAdapter(Fragment2.this.getActivity(), date, R.layout.item2,new String[]{
"userimage","content" }, new int[]{R.id.personImage,R.id.context});
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment2, null);
/*listView=(ListView)view.findViewById(R.id.Listview1);*/
btn=(Button)view.findViewById(R.id.publish2);
btn.setOnClickListener( this);
editText=(EditText)view.findViewById(R.id.content2);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//从网络获取信息
/*List<HashMap<String,String>> date=new ArrayList<HashMap<String,String>>();
HashMap<String, String> item=new HashMap<String, String>();
item.put("personImg", String.valueOf(R.drawable.s120521201));
item.put("context", "琥珀");
date.add(item);*/
//date=new ArrayList<HashMap<String,String>>();
/*try {
//前面的http必须加上
date=MsgService.getpiblishMsg("http://www.fenxer.com/getpublish.php");
} catch (Exception e) {
}
adapter=new SimpleAdapter(getActivity(), date, R.layout.item2,new String[]{
"userimage","content" }, new int[]{R.id.personImage,R.id.context});
setListAdapter(adapter); */
Runnable runnable=new Runnable()
{
public void run()
{
try
{
//xmlwebData解析网络中xml中的数据
date=MsgService.getpiblishMsg("http://www.fenxer.com/getpublish.php");
if(date!=null)
{
//发送消息,并把persons结合对象传递过去
handler.sendMessage(handler.obtainMessage(0, date));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
try
{
//开启线程
mythread= new Thread(runnable);
mythread.start();
//handler与线程之间的通信及数据处理
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
//向网络发送数据并从新绑定
@Override
public void onClick(View arg0) {
String contentString=editText.getText().toString();
if(contentString.equals(""))
{
Toast.makeText(getActivity(), "不能为空", 0).show();
return;
}
else {
String userID=UserInfo.getUserID();
HashMap<String, String> hmp=new HashMap<String, String>();
hmp.put("userID", userID);
hmp.put("content", contentString);
if( MsgService.sendmsg("http://www.fenxer.com/postpublish?", hmp,"utf-8"))
{
editText.setText("");
Toast.makeText(getActivity(), "成功", 0).show();
try {
date=MsgService.getpiblishMsg("http://www.fenxer.com/getpublish.php");
setListAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
else {
Toast.makeText(getActivity(), "法师宏失败", 0).show();
}
}
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
System.out.println("onDestroyView");
super.onDestroyView();
if(mythread!=null)
{
mythread.stop();
}
}
}
友情提示:这种事情需要看代码
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import com.example.info.UserInfo;
import com.example.service.MsgService;
import com.util.Tools.UrlServerTool;
import android.R.string;
import android.annotation.SuppressLint;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SimpleAdapter;
import android.widget.Toast;
@SuppressLint("HandlerLeak")
public class Fragment2 extends ListFragment implements OnClickListener {
//2014-3-18添加按钮发布事件
private Button btn;
private List<HashMap<String,String>> date;
private SimpleAdapter adapter;
private EditText editText;
private Thread mythread;
private Handler handler=new Handler()
{
public void handleMessage(Message msg)
{
if(msg.what==0)
{
//msg.obj是获取handler发送信息传来的数据
@SuppressWarnings("unchecked")
List<HashMap<String,String>> date=( List<HashMap<String,String>>) msg.obj;
if(date!=null)
{
adapter=new SimpleAdapter(Fragment2.this.getActivity(), date, R.layout.item2,new String[]{
"userimage","content" }, new int[]{R.id.personImage,R.id.context});
adapter.notifyDataSetChanged();
setListAdapter(adapter);
}
}
}
};
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.fragment2, null);
/*listView=(ListView)view.findViewById(R.id.Listview1);*/
btn=(Button)view.findViewById(R.id.publish2);
btn.setOnClickListener( this);
editText=(EditText)view.findViewById(R.id.content2);
return view;
}
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//从网络获取信息
/*List<HashMap<String,String>> date=new ArrayList<HashMap<String,String>>();
HashMap<String, String> item=new HashMap<String, String>();
item.put("personImg", String.valueOf(R.drawable.s120521201));
item.put("context", "琥珀");
date.add(item);*/
//date=new ArrayList<HashMap<String,String>>();
/*try {
//前面的http必须加上
date=MsgService.getpiblishMsg("http://www.fenxer.com/getpublish.php");
} catch (Exception e) {
}
adapter=new SimpleAdapter(getActivity(), date, R.layout.item2,new String[]{
"userimage","content" }, new int[]{R.id.personImage,R.id.context});
setListAdapter(adapter); */
Runnable runnable=new Runnable()
{
public void run()
{
try
{
//xmlwebData解析网络中xml中的数据
date=MsgService.getpiblishMsg("http://www.fenxer.com/getpublish.php");
if(date!=null)
{
//发送消息,并把persons结合对象传递过去
handler.sendMessage(handler.obtainMessage(0, date));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
};
try
{
//开启线程
mythread= new Thread(runnable);
mythread.start();
//handler与线程之间的通信及数据处理
}
catch (Exception e)
{
e.printStackTrace();
}
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
//向网络发送数据并从新绑定
@Override
public void onClick(View arg0) {
String contentString=editText.getText().toString();
if(contentString.equals(""))
{
Toast.makeText(getActivity(), "不能为空", 0).show();
return;
}
else {
String userID=UserInfo.getUserID();
HashMap<String, String> hmp=new HashMap<String, String>();
hmp.put("userID", userID);
hmp.put("content", contentString);
if( MsgService.sendmsg("http://www.fenxer.com/postpublish?", hmp,"utf-8"))
{
editText.setText("");
Toast.makeText(getActivity(), "成功", 0).show();
try {
date=MsgService.getpiblishMsg("http://www.fenxer.com/getpublish.php");
setListAdapter(adapter);
} catch (Exception e) {
e.printStackTrace();
}
}
else {
Toast.makeText(getActivity(), "法师宏失败", 0).show();
}
}
}
@Override
public void onDestroyView() {
// TODO Auto-generated method stub
System.out.println("onDestroyView");
super.onDestroyView();
if(mythread!=null)
{
mythread.stop();
}
}
}