出于某种原因,当我尝试在片段中创建此listView时,我遇到了应用程序崩溃的问题。我已经看到了几个需要使用的东西而不是视图,因为尚未创建视图。有没有办法重组这个代码或改变的东西,所以它的工作原理?这是代码。对不起,我是Android新手。 当我尝试单击“发送”按钮时代码崩溃,所以我假设在onClick方法中的某处。
package com.quickCollab.project;
import java.util.ArrayList;
import com.quickCollab.project.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
public class Chat extends Fragment {
private String message;
private EditText messageField;
private ListView mainListView;
private ArrayAdapter<String> listAdapter;
private ArrayList<String> chatList = new ArrayList<String>();
private View chat;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
chat = inflater.inflate(R.layout.chat_frag, container, false);
chatList.add("Welcome");
Button send = (Button) chat.findViewById(R.id.send);
messageField = (EditText) chat.findViewById(R.id.username);
mainListView = (ListView) chat.findViewById(R.id.mainListView);
listAdapter = new ArrayAdapter<String>(getActivity(),
R.layout.simplerow, chatList);
mainListView.setAdapter(listAdapter);
send.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
System.out.println("Got this far");
message = messageField.getText().toString();
chatList.add(message);
send();
System.out.println(chatList.toString());
/*
if (message.length() > 0) {
listAdapter.add(message);
listAdapter.notifyDataSetChanged();
} else {
message = "";
listAdapter.add(message);
listAdapter.notifyDataSetChanged();
}*/
};
});
return chat;
}
public void send() {
listAdapter.notifyDataSetChanged();
}
}
有一个logcat错误的请求。谢谢你的帮助!
12-08 22:45:11.456: I/Adreno-EGL(29631): <qeglDrvAPI_eglInitialize:410>: EGL 1.4 QUALCOMM build: ()
12-08 22:45:11.456: I/Adreno-EGL(29631): OpenGL ES Shader Compiler Version: E031.24.00.08+13
12-08 22:45:11.456: I/Adreno-EGL(29631): Build Date: 03/28/14 Fri
12-08 22:45:11.456: I/Adreno-EGL(29631): Local Branch: 0328_AU200_patches
12-08 22:45:11.456: I/Adreno-EGL(29631): Remote Branch:
12-08 22:45:11.456: I/Adreno-EGL(29631): Local Patches:
12-08 22:45:11.456: I/Adreno-EGL(29631): Reconstruct Branch:
12-08 22:45:11.486: D/OpenGLRenderer(29631): Enabling debug mode 0
12-08 22:45:11.506: D/(29631): DFactory start
12-08 22:45:11.506: D/(29631): This is decoding
12-08 22:45:11.506: D/(29631): onDecode : stream read 54 bytes, ptr = 0x72d8bef0
12-08 22:45:11.506: D/(29631): onDecode : QmageDecParseHeader call : QM
12-08 22:45:11.506: D/(29631): Qmage parsing for decoding ok
12-08 22:45:11.506: D/(29631): onDecode : QmageHeader Height() 96 Width() : 96 sampleSize : 1
12-08 22:45:11.506: D/(29631): Qmage Make Color table[2]
12-08 22:45:11.506: D/(29631): Qmage Mae Color table end
12-08 22:45:11.506: D/(29631): 1 to 1 Index mode decoding
12-08 22:45:11.506: D/(29631): 1 to 1 Index mode decoding end
12-08 22:45:11.506: D/(29631): onDecode : return true
12-08 22:45:12.016: W/IInputConnectionWrapper(29631): showStatusIcon on inactive InputConnection
答案 0 :(得分:0)
private ArrayList<String> chatList;
您使用chatList
填充了列表视图,但未使用任何值初始化chatList
。
listAdapter = new ArrayAdapter<String>(getActivity(), R.layout.simplerow, chatList);
因此,在创建listAdapter
时,它会返回NullPointerException
,请实例化您的chatList
并使用任何值进行迭代,然后重试。