我正在一個學校的應用程序,我必須創建一個購物清單,當我選擇一個列表,我應該得到一個新的活動與列表上的產品。現在我的問題是,當我嘗試獲取listView中的單擊元素,然後將產品放在新活動的列表視圖中的列表中時,我在textview上獲得空指針異常。在android的textView空指針異常android
這是獲取異常
<?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>
這是我的新的活動列表視圖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">
<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;
}
=======編輯解決空指針後=======
現在它打開新的活動,但它是空的,它不填充列表中的EW。我想知道我究竟在做錯什麼。
這是我的片段:
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;
}
你可以上傳'ShoppingListAdapter'類嗎? –
我在文章末尾添加了 –
您應該可以執行類似'ShoppingList list = listAdapter .getItem(position)'的操作,以便在點擊的位置獲取列表。 –