我有一个带有各种标签的应用程序(在tabhost上),每个标签都是一个扩展活动的活动,并且在其上有一些文本字段和内容。在我的第一个标签上,我用它来显示使用customadapter的列表视图这个列表是从服务器获取数据,数据在list中每3秒后更新.App工作正常,但有时会崩溃并在某些时候在CustomAdapter中显示java.lang.NullPointerException。
我的代码在下面提到我的课就像这样
public class BaseActivity extends Activity {
private ListView list;
private NotificationManager mNotificationManager;
private final int NOTIFICATION_ID = 1010;
private Timer timer = new Timer();
private String response;
protected Dialog m_ProgressDialog;
String[] operatorList,operatorDetail,operatordetail,tempoperatordata;
String[] noofvisitors,opt;
private static final String DEB_TAG = "Error Message";
public static ArrayList<String> SessionText,IPText,DurationText;
private ArrayList<String> StatusText,NoOfVisit,ButtonText;
private int sizeoflist;
private String IP;
Context context;
public static String from,sessionid,id,text,iptext,status;
private int position,noofchat;
private boolean IsSoundEnable,IsChatOnly;
private Button logout;
private CustomAdapter adapter;
NotificationManager notificationManager;
final HashMap<String, String> postParameters = new HashMap<String, String>();
private String url;
private Handler handler;
private Runnable updater;
private void TimerMethod()
{
//This method is called directly by the timer
//and runs in the same thread as the timer.
//We call the method that will work with the UI
//through the runOnUiThread method.
this.runOnUiThread(Timer_Tick);
}
private Runnable Timer_Tick = new Runnable() {
public void run() {
//This method runs in the same thread as the UI.
//Do something to the UI thread here
getVisitorDetailFromServer();
try {
Log.i("UPDATE", "Handler called");
// searchAdapter = getFeed(URL);
//handler.postDelayed(this, 3000);
} catch(Exception e) {
Log.e("UPDATE ERROR", e.getMessage());
}
}
};
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.visitor);
list = (ListView) findViewById(R.id.list01);
logout = (Button) findViewById(R.id.btnlogout);
// list.setTextFilterEnabled(true);
//list.clearChoices();
//-----------------Making the object of arrayList----------------
SessionText = new ArrayList<String>();
IPText = new ArrayList<String>();
DurationText = new ArrayList<String>();
StatusText = new ArrayList<String>();
NoOfVisit = new ArrayList<String>();
ButtonText = new ArrayList<String>();
Bundle extras = getIntent().getExtras();
if (extras != null) {
IsSoundEnable = Controls.IsSoundEnable;
IsChatOnly = Controls.IsChatOnly;
IsSoundEnable = extras.getBoolean("IsSoundOnly", Controls.IsSoundEnable);
IsChatOnly= extras.getBoolean("IsCOnlyhat", Controls.IsChatOnly);
extras.getString("From");
position=extras.getInt("Position");
}
}
@Override
protected void onStart() {
super.onStart();
//------------Getting the visitor detail-------------------------
//getVisitorDetailFromServer();
adapter = new CustomAdapter(BaseActivity.this);
timer.schedule(new TimerTask() {
public void run() {
TimerMethod();
}
}, 0, 6000);
/*handler = new Handler();
updater = new Runnable() {
public void run() {
/*
* Update the list
*/
/* getVisitorDetailFromServer();
try {
Log.i("UPDATE", "Handler called");
// searchAdapter = getFeed(URL);
handler.postDelayed(this, 3000);
} catch(Exception e) {
Log.e("UPDATE ERROR", e.getMessage());
}
}
};
updater.run();*/
//---------------When user click on logout button-----------------------------------
logout.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
logoutFromServer();
}
});
}
//----------------------------Getting the detail from server of monitoring window-------------------------
private void getVisitorDetailFromServer() {
// TODO Auto-generated method stub
url = "http://sa.live2support.com/cpn/wz1-allonlineu.php?";
postParameters.put("adminid",Main.loginId.getText().toString());
postParameters.put("sid",Main.siteId.getText().toString());
postParameters.put("nvar","Y");
postParameters.put("conly", "N");
Runnable searchThread = new Runnable(){
public void run() {
// TODO Auto-generated method stub
try {
response = CustomHttpClient.executeHttpPost1(url,postParameters);
Log.i(DEB_TAG, "Requesting to server"+response);
//CustomHttpClient.getResponseInString(response );
System.out.println("Output of httpResponse:"+response);
String result = response;
result = response;
String delimiter1 = "<ln>";
String delimiter = "<fld>";
operatorDetail = result.split(delimiter1);
for(int i=0;i<operatorDetail.length;i++){
operatorList = operatorDetail[i].split(delimiter);
SessionText.add(operatorList[0]);
IPText.add(operatorList[1]);
DurationText.add(operatorList[4]);
StatusText.add(operatorList[3]);
NoOfVisit.add(operatorList[2]);
ButtonText.add(operatorList[6]);
//System.out.println("value of iptext is"+IPText.get(i));
}
operatorDetail=null;
operatorList=null;
SessionText = null;
IPText =null;
DurationText=null;
StatusText=null;
NoOfVisit=null;
ButtonText=null;
operatorDetail = result.split(delimiter1);
SessionText = new ArrayList<String>();
IPText = new ArrayList<String>();
DurationText = new ArrayList<String>();
StatusText = new ArrayList<String>();
NoOfVisit = new ArrayList<String>();
ButtonText = new ArrayList<String>();
//--------Getting the size of the list---------------------
sizeoflist = operatorDetail.length;
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if(response!=null){
runOnUiThread(new Runnable() {
public void run() {
// TODO Auto-generated method stub
playsound3();
noofchat =0;
list.setAdapter(adapter);
list.getDrawingCache(false);
list.invalidateViews();
list.setCacheColorHint(Color.TRANSPARENT);
list.requestFocus(0);
list.setSelected(false);
// adapter.notifyDataSetChanged();
// list.getAdapter().notifyAll();
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getBaseContext(), item, Toast.LENGTH_LONG).show();
}
});
if(IsChatOnly==false){
TimerTask timerTask = new TimerTask()
{
@Override
public void run()
{
//triggerNotification();
//playsound();
}
};
timer.schedule(timerTask, 3000);
}
else{
playsound();
}
}
});
}
else {
ShowAlert();
}
}
};
Thread thread = new Thread(null, searchThread, "MagentoBackground");
thread.start();
}
我正在为类使用的适配器类就像这样
class CustomAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public CustomAdapter(Context context) {
mInflater = LayoutInflater.from(context);
this.notifyDataSetChanged();
}
public int getCount() {
return sizeoflist;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.row1, null);
holder = new ViewHolder();
holder.IP = (TextView) convertView.findViewById(R.id.ip);
holder.duration = (TextView) convertView.findViewById(R.id.duration);
holder.status =(TextView) convertView.findViewById(R.id.status);
holder.noOfVisit = (TextView) convertView.findViewById(R.id.NoOfvisit);
holder.invite =(Button)convertView.findViewById(R.id.btnjoin);
holder.deny = (Button) convertView.findViewById(R.id.btndeny);
//holder.accept = (Button) convertView.findViewById(R.id.btnaccept);
holder.deny.setVisibility(View.INVISIBLE);
holder.invite.setId(position);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
String result = response;
String delimiter = "<fld>";
String delimiter1 = "<ln>";
String delimiter2 = "<blk>";
String delimiter3 = "<fld><fld><blk>";
operatorDetail = result.split(delimiter1);
String[] operatorlist = result.split(delimiter3);
noofvisitors = operatorlist[1].split(delimiter2);
//System.out.println("no of visitor================"+noofvisitors[0]);
for(int i=0;i<operatorDetail.length;i++){
operatorList = operatorDetail[i].split(delimiter);
operatorlist = result.split(delimiter2);
SessionText.add(operatorList[0]);
IPText.add(operatorList[1]);
DurationText.add(operatorList[4]);
StatusText.add(operatorList[3]);
NoOfVisit.add(operatorList[2]);
ButtonText.add(operatorList[6]);
//if(StatusText.get(position).equalsIgnoreCase("chat request")){
//holder.deny.setVisibility(View.VISIBLE);
//holder.invite.setText("Accept");
//holder.accept.setText("Accept");
/*holder.accept.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
holder.invite.setText("Join");
holder.deny.setVisibility(View.INVISIBLE);
//chatRequest(IPText.get(position), SessionText.get(position));
}
});*/
//}
//else{
// holder.invite.setText("Invite");
holder.deny.setVisibility(View.INVISIBLE);
// //holder.accept.setVisibility(View.INVISIBLE);
//}
holder.invite.setText("Invite");
holder.deny.setVisibility(View.INVISIBLE);
if(holder.invite.getText().equals("Join")){
holder.invite.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
chatRequest(iptext,sessionid);
}
});
}
holder.IP.setText(IPText.get(position));
holder.duration.setText(DurationText.get(position));
holder.status.setText(StatusText.get(position));
holder.noOfVisit.setText(NoOfVisit.get(position));
//---------------------When user click on invite Button---------------------
holder.invite.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
callToServer(SessionText.get(position));
}
});
//-----------------------------When user click on deny button------------------------
holder.deny.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
refuseToServer(sessionid);
holder.deny.setVisibility(View.INVISIBLE);
}
});
}
System.out.println("no of visitor================"+noofvisitors[0]);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
Intent i=new Intent(BaseActivity.this,VisitorDetail.class);
i.putExtra("ID", id);
i.putExtra("Position",position);
i.putExtra("From", from);
i.putExtra("SessionText", SessionText.get(position));
i.putExtra("IPTEXT",IPText.get(position));
startActivity(i);
}});
//startService();
return convertView;
}
class ViewHolder {
TextView IP;
TextView duration;
Button deny;
TextView status;
TextView noOfVisit;
Button invite;
Button accept;
}
}
像这样的logcat
02-23 14:05:42.188: E/AndroidRuntime(2011): FATAL EXCEPTION: main
02-23 14:05:42.188: E/AndroidRuntime(2011): java.lang.IndexOutOfBoundsException: Invalid index 3, size is 0
02-23 14:05:42.188: E/AndroidRuntime(2011): at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
02-23 14:05:42.188: E/AndroidRuntime(2011): at java.util.ArrayList.get(ArrayList.java:311)
02-23 14:05:42.188: E/AndroidRuntime(2011): at com.live2support.BaseActivity$CustomAdapter.getView(BaseActivity.java:719)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.AbsListView.obtainView(AbsListView.java:1294)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.ListView.measureHeightOfChildren(ListView.java:1198)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.ListView.onMeasure(ListView.java:1109)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.RelativeLayout.measureChild(RelativeLayout.java:563)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.LinearLayout.measureVertical(LinearLayout.java:381)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.LinearLayout.measureVertical(LinearLayout.java:526)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.LinearLayout.onMeasure(LinearLayout.java:304)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.widget.FrameLayout.onMeasure(FrameLayout.java:245)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.View.measure(View.java:8171)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewRoot.performTraversals(ViewRoot.java:801)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.view.ViewRoot.handleMessage(ViewRoot.java:1727)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.os.Handler.dispatchMessage(Handler.java:99)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.os.Looper.loop(Looper.java:123)
02-23 14:05:42.188: E/AndroidRuntime(2011): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-23 14:05:42.188: E/AndroidRuntime(2011): at java.lang.reflect.Method.invokeNative(Native Method)
02-23 14:05:42.188: E/AndroidRuntime(2011): at java.lang.reflect.Method.invoke(Method.java:521)
02-23 14:05:42.188: E/AndroidRuntime(2011): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-23 14:05:42.188: E/AndroidRuntime(2011): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-23 14:05:42.188: E/AndroidRuntime(2011): at dalvik.system.NativeStart.main(Native Method)
我做错了什么?我无法理解................. 任何人都可以帮助我吗?
像这样或logcat
02-23 15:01:01.928:E / AndroidRuntime(2429):致命异常:主要 02-23 15:01:01.928:E / AndroidRuntime(2429):java.lang.NullPointerException 02-23 15:01:01.928:E / AndroidRuntime(2429):at com.live2support.BaseActivity $ CustomAdapter.getView(BaseActivity.java:670) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.AbsListView.obtainView(AbsListView.java:1315) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.ListView.measureHeightOfChildren(ListView.java:1198) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.ListView.onMeasure(ListView.java:1109) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.RelativeLayout.measureChild(RelativeLayout.java:563) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:378) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1012) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.LinearLayout.measureVertical(LinearLayout.java:381) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:578) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.RelativeLayout.onMeasure(RelativeLayout.java:362) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.LinearLayout.measureVertical(LinearLayout.java:526) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.LinearLayout.onMeasure(LinearLayout.java:304) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:3132) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.widget.FrameLayout.onMeasure(FrameLayout.java:245) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.View.measure(View.java:8171) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewRoot.performTraversals(ViewRoot.java:801) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.view.ViewRoot.handleMessage(ViewRoot.java:1727) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.os.Handler.dispatchMessage(Handler.java:99) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.os.Looper.loop(Looper.java:123) 02-23 15:01:01.928:E / AndroidRuntime(2429):在android.app.ActivityThread.main(ActivityThread.java:4627) 02-23 15:01:01.928:E / AndroidRuntime(2429):at java.lang.reflect.Method.invokeNative(Native Method) 02-23 15:01:01.928:E / AndroidRuntime(2429):at java.lang.reflect.Method.invoke(Method.java:521) 02-23 15:01:01.928:E / AndroidRuntime(2429):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:868) 02-23 15:01:01.928:E / AndroidRuntime(2429):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-23 15:01:01.928:E / AndroidRuntime(2429):at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
看起来你的一个arraylist的数据少于列表大小,请查看您尝试访问的行号码719上的哪个arraylist对象,并检查这个arraylist不等于列表大小的原因。