我正在尝试开发聊天应用程序。我创建了一个片段活动,tabhost
有两个tabwidget
。
首先tabwidget
显示来自手机的联系人。这很好。
在显示上次聊天的第二个tabwidget
上。此选项卡在选择时抛出错误。仅当最后一个聊天列表为空时,第二个tabwidget
才会引发错误。最后一个聊天列表是否至少有一个项目,第二个选项卡可以正常工作。
我附加了我的错误和代码。请看看。
08-01 01:29:27.870: W/dalvikvm(2672): threadid=1: thread exiting with uncaught exception (group=0xa4be9648)
08-01 01:29:27.870: E/AndroidRuntime(2672): FATAL EXCEPTION: main
08-01 01:29:27.870: E/AndroidRuntime(2672): java.lang.NullPointerException
08-01 01:29:27.870: E/AndroidRuntime(2672): at com.happwall.chat.ChatSecondTab.onCreateView(ChatSecondTab.java:94)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1789)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:955)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1138)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:740)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1501)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:458)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.os.Handler.handleCallback(Handler.java:730)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.os.Handler.dispatchMessage(Handler.java:92)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.os.Looper.loop(Looper.java:137)
08-01 01:29:27.870: E/AndroidRuntime(2672): at android.app.ActivityThread.main(ActivityThread.java:5103)
08-01 01:29:27.870: E/AndroidRuntime(2672): at java.lang.reflect.Method.invokeNative(Native Method)
08-01 01:29:27.870: E/AndroidRuntime(2672): at java.lang.reflect.Method.invoke(Method.java:525)
08-01 01:29:27.870: E/AndroidRuntime(2672): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
08-01 01:29:27.870: E/AndroidRuntime(2672): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
08-01 01:29:27.870: E/AndroidRuntime(2672): at dalvik.system.NativeStart.main(Native Method)
// ChatSecondTab.java
public class ChatSecondTab extends Fragment{
ListView lv;
JSONObject chat_list, nameasid;
String KEY_USER_NAME = "LoginStatus", username, name, KEY_REG_ID = "regId_me", reg_id;
SharedPreferences preferences;
List<InboxMessage> message_list = null;
InboxDataBaseHandler inbox_dbHandler = null;
private InboxMessageAdapter adapter;
Context context;
EditText search;
@Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.chat_second, container, false);
context = getActivity().getApplicationContext();
preferences = getActivity().getSharedPreferences("PREF_NAME", Context.MODE_PRIVATE);
username = preferences.getString(KEY_USER_NAME, "");
lv = (ListView) rootView.findViewById(R.id.listView1);
search = (EditText) rootView.findViewById(R.id.editText1);
inbox_dbHandler = new InboxDataBaseHandler(getActivity(), null, null, 1);
message_list = new ArrayList<InboxMessage>();
message_list = inbox_dbHandler.getMessages();
if(message_list.size() == 0)
{
Toast.makeText(getActivity(), "No chat available!", Toast.LENGTH_LONG).show();
}
else
{
adapter = new InboxMessageAdapter(getActivity(), message_list);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
InboxMessage m = message_list.get(position);
Intent i =new Intent(getActivity(), ChatActivity.class);
i.putExtra("name", m.name_chatter);
i.putExtra("reg_id_chatter", m.reg_id_chatter);
i.putExtra("name_mine", name);
getActivity().startActivity(i);
}
});
}
adapter.notifyDataSetChanged();
search.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
String search_text = arg0.toString();
if(search_text.equalsIgnoreCase("")) {
message_list = new ArrayList<InboxMessage>();
message_list = inbox_dbHandler.getMessages();
adapter = new InboxMessageAdapter(getActivity(), message_list);
lv.setAdapter(adapter);
}
else {
message_list = searchChatlist(message_list, arg0.toString());
adapter = new InboxMessageAdapter(getActivity(), message_list);
lv.setAdapter(adapter);
}
}
});
return rootView;
}
private List<InboxMessage> searchChatlist(
List<InboxMessage> message_list, String string) {
// TODO Auto-generated method stub
List<InboxMessage> message = new ArrayList<InboxMessage>();
for (InboxMessage list : message_list) {
if (list.name_chatter.contains(string)==true) {
message.add(list);
}
}
return message;
}
}