我在对话框中有一个简单的编辑文本,输入一些文本后我单击确定我有一个列表在一个片段中查看我需要将该文本添加到我的listview中,这是一个片段。
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
这是我的Homefragment:
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.app.Fragment;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
public class HomeFragment extends Fragment {
public HomeFragment(){}
ListView lv;
List<String> li;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
lv = (ListView) getView().findViewById(R.id.itemslistView);
li=new ArrayList<String>();
li.add("Apple");
li.add("Mango");
li.add("Orange");
ArrayAdapter<String> adp=new ArrayAdapter<String>
(getActivity().getApplicationContext(),R.layout.list,li);
lv.setAdapter(adp);
return rootView;
}
}
当我运行此应用程序时,应用程序崩溃了,我在logcat中没有收到任何错误。
这是我的input_dialog:
protected void showInputDialog() {
// get prompts.xml view
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View promptView = layoutInflater.inflate(R.layout.input_dialog, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
MainActivity.this);
alertDialogBuilder.setView(promptView);
// setup a dialog window
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create an alert dialog
AlertDialog alert = alertDialogBuilder.create();
alert.show();
}
这是我的家庭片段:
public class HomeFragment extends Fragment {
public HomeFragment() {
}
ListView lv;
List<String> li;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
lv = (ListView) rootView.findViewById(R.id.itemslistView);
View editView = inflater.inflate(R.layout.input_dialog, container,
false);
EditText edit = (EditText) editView.findViewById(R.id.txtlist);
String ItemName = edit.getText().toString();
lv = (ListView) getView().findViewById(R.id.itemslistView);
li = new ArrayList<String>();
li.add(ItemName);
ArrayAdapter<String> adp = new ArrayAdapter<String>(getActivity()
.getApplicationContext(), R.layout.list, li);
lv.setAdapter(adp);
return rootView;
}
}
答案 0 :(得分:3)
更改
lv = (ListView) getView().findViewById(R.id.itemslistView);
到
lv = (ListView)rootView.findViewById(R.id.itemslistView);
onCreateView(...)
中的
答案 1 :(得分:0)
这样做
lv = (ListView) getView().findViewById(R.id.itemslistView);
您没有从
中这个虚增的视图中获取listview的实例View rootView = inflater.inflate(R.layout.fragment_home, container, false);
因此,在创建实例时,请从rootview中获取它....即
lv = (ListView) rootView.findViewById(R.id.itemslistView);