我尝试使用Fragment创建聊天。为此,我在谷歌上找到了一个例子。该项目使用Activity,我尝试将其用于Fragment,因为我的项目都是使用Fragments开发的。当我从Activity更改为Fragment时,这一行:adapter = new DiscussArrayAdapter(getView().getContext(), R.layout.listitem_discuss);
抛出异常
我在这里尝试
XML
///activity_discuss
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="80dp">
</ListView>
<RelativeLayout
android:id="@+id/form"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="@+id/chatText"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="@+id/buttonSend" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send"
android:id="@+id/buttonSend"
android:layout_alignBottom="@+id/chatText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
</RelativeLayout>
///listitem_discuss
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="@+id/wrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dip"
android:background="@drawable/bubble_yellow"
android:paddingLeft="10dip"
android:text="Hello bubbles!"
android:textColor="@android:color/primary_text_light" />
</LinearLayout>
</LinearLayout>
片段
public class HelloBubblesActivity extends Fragment {
private DiscussArrayAdapter adapter;
private ListView lv;
private LoremIpsum ipsum;
private EditText editText1;
private Button btnSend;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_discuss, container, false);
lv = (ListView)getView().findViewById(R.id.listView1);
adapter = new DiscussArrayAdapter(getView().getContext(), R.layout.listitem_discuss);
lv.setAdapter(adapter);
editText1 = (EditText)view.findViewById(R.id.chatText);
editText1.setOnKeyListener(new OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) {
// Perform action on key press
//adapter.add(new OneComment(false, editText1.getText().toString()));
receiveMessage();
editText1.setText("");
return true;
}
return false;
}
});
btnSend = (Button)view.findViewById(R.id.buttonSend);
btnSend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//adapter.add(new OneComment(false, editText1.getText().toString()));
receiveMessage();
editText1.setText("");
}
});
return view;
}
private void receiveMessage(){
String msg = editText1.getText().toString();
new ChatDAO().receiveMessage("");
}
/** recebe msg */
private void addItems() {
adapter.add(new OneComment(true, "Hello bubbles!"));
}
}
适配器
public class DiscussArrayAdapter extends ArrayAdapter<OneComment> {
private TextView countryName;
private List<OneComment> countries = new ArrayList<OneComment>();
private LinearLayout wrapper;
public DiscussArrayAdapter(Context context, int resource) {
super(context, resource);
}
@Override
public void add(OneComment object) {
countries.add(object);
super.add(object);
}
public int getCount() {
return this.countries.size();
}
public OneComment getItem(int index) {
return this.countries.get(index);
}
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
if (row == null) {
LayoutInflater inflater = (LayoutInflater) this.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
row = inflater.inflate(R.layout.listitem_discuss, parent, false);
}
wrapper = (LinearLayout) row.findViewById(R.id.wrapper);
OneComment coment = getItem(position);
countryName = (TextView) row.findViewById(R.id.comment);
countryName.setText(coment.comment);
countryName.setBackgroundResource(coment.left ? R.drawable.bubble_yellow : R.drawable.bubble_green);
wrapper.setGravity(coment.left ? Gravity.LEFT : Gravity.RIGHT);
return row;
}
public Bitmap decodeToBitmap(byte[] decodedByte) {
return BitmapFactory.decodeByteArray(decodedByte, 0, decodedByte.length);
}
}
答案 0 :(得分:2)
在代码中的那一点上,getView()
将返回null,因为从onCreateView()
方法返回的视图被设置为片段的视图。使用getActivity()
代替getView().getContext()
来获取必要的上下文。
答案 1 :(得分:2)
尝试使用getActivity()
代替getView().getContext()