我正在为学校开发一个应用程序,我必须创建一个购物清单,当我选择一个列表时,我应该在该列表中获得一个新的活动。现在我的问题是,当我尝试获取listView中的clicked元素,然后将产品放在新活动的listview中的列表中时,我在textview上得到一个空指针异常。
这是获取异常的文本视图
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textSize="20dp"
android:textColor="#236B8E"
android:layout_marginTop="20dp"
android:id="@+id/products_on_list"/>
</LinearLayout>
这是我新活动中的listView:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/shopping_list_details">
</ListView>
</LinearLayout>
以下是新活动的java代码:
public class DisplayShoppingListDetailsActivity extends AppCompatActivity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//defines the activity layout
setContentView(R.layout.shopping_list_details);
ListView listView = (ListView) findViewById(R.id.listView_shopping_lists);
Intent intent = getIntent();
String name = intent.getStringExtra("list");
ProductsOnListAdapter ad = new ProductsOnListAdapter(this, -1, Service.getService().getProductsOnList(name));
listView.setAdapter(ad);
}
}
这是我有购物清单列表的片段。这是我为新活动创建意图的地方:
public class Fragment1 extends Fragment {
public Fragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment1, container, false);
final ListView listView = (ListView) rootView.findViewById(R.id.listView_shopping_lists);
// get data from the table by the ListAdapter
ShoppingListAdapter listAdapter = new ShoppingListAdapter(getActivity(), -1, Service.getService().getShoppingLists());
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// selected item
// HERE IS THE PROBLEM
// I have tried to use rootView instead of view
TextView textview = (TextView) view.findViewById(R.id.products_on_list);
String list = textview.getText().toString();
// Launching new Activity on selecting single List Item
Intent intent = new Intent(getActivity(), DisplayShoppingListDetailsActivity.class);
// sending data to new activity
intent.putExtra("list", list);
startActivity(intent);
}
});
//System.out.println("Test: " + Storage.getStorage().getShopingLists());
return rootView;
}
}
这是例外:
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.CharSequence android.widget.TextView.getText()' on a null object reference
at com.gnirt69.slidingmenuexample.fragment.Fragment1$1.onItemClick(Fragment1.java:44)
ShoppingListAdapter:
@Override
public View getView(int position, View convertView, final ViewGroup parent){
// Get the data item for this position
//final ShoppingList list = getItem(position);
ViewHolder holder;
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = View.inflate(getContext(), R.layout.items_lists_row, null);
//set up the viewHolder
holder = new ViewHolder();
holder.shoppingListTextView = (TextView) convertView.findViewById(R.id.textView1);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
// get the reference to the specific item
final ShoppingList list = getItem(position);
//TextView tv = (TextView) convertView.findViewById(R.id.textView1);
//manipulate the view
holder.shoppingListTextView.setText(list.getName());
return convertView;
}
public static class ViewHolder {
private TextView shoppingListTextView;
}
=======解决空指针后编辑=======
现在它会打开新活动但它是空的,它不会填充列表视图。我正在努力弄清楚我到底在做错什么。
这是我的片段:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.fragment1, container, false);
final ListView listView = (ListView) rootView.findViewById(R.id.listView_shopping_lists);
// get data from the table by the ListAdapter
final ShoppingListAdapter listAdapter = new ShoppingListAdapter(getActivity(), -1, Service.getService().getShoppingLists());
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ShoppingList shoppingList = listAdapter.getItem(position);
// selected item
TextView textview = (TextView) rootView.findViewById(R.id.products_on_list);
String list = textview.getText().toString();
// Launching new Activity on selecting single List Item
Intent intent = new Intent(getActivity(), DisplayShoppingListDetailsActivity.class);
// sending data to new activity
intent.putExtra("list", list);
//System.out.println(shoppingList.getProductsOnList());
startActivity(intent);
}
});
//System.out.println("Test: " + Storage.getStorage().getShopingLists());
return rootView;
}
应该在点击列表中显示产品的新活动,但不会:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//defines the activity layout
setContentView(R.layout.shopping_list_details);
ListView listView = (ListView) findViewById(R.id.shopping_list_details);
Intent intent = getIntent();
String name = intent.getStringExtra("list");
ProductsOnListAdapter ad = new ProductsOnListAdapter(this, -1, Service.getService().getProductsOnList(name));
listView.setAdapter(ad);
}
适配器:
@Override
public View getView(int position, View convertView, final ViewGroup parent){
// Get the data item for this position
//final ShoppingList list = getItem(position);
ViewHolder holder;
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = View.inflate(getContext(), R.layout.shopping_list_details, null);
//set up the viewHolder
holder = new ViewHolder();
holder.productsTextView = (TextView) convertView.findViewById(R.id.products_on_list);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
// get the reference to the specific item
final ProductOnList product = getItem(position);
//TextView tv = (TextView) convertView.findViewById(R.id.textView1);
//manipulate the view
holder.productsTextView.setText(product.toString());
return convertView;
}
public static class ViewHolder {
private TextView productsTextView;
}
答案 0 :(得分:0)
我认为问题是你想从布局
调用TextviewsetContentView(R.layout.shopping_list_details);
但你的片段使用了布局
final View rootView = inflater.inflate(R.layout.fragment1, container, false);
您可以通过将TextView从Main传递到Fragment或更改xml布局来解决此问题
您可以通过向fragment1布局添加具有相同ID的textview来测试是否为真。
答案 1 :(得分:0)
您在新Activity和Fragment中引用相同的ListView R.id.listView_shopping_lists
,您从不在Activity布局文件中定位shopping_list_details
,其中包含每个项目,因此TextView为null。