我正在尝试制作此错误。
当我点击一个按钮并执行“showData()”函数时; 我想做这个功能的原因是因为我想在什么时候更改片段 我单击此按钮并将“传递参数”传递给新片段。 所以它将继续执行函数“passData()”;
但是在“editTextNAME.setText(restaurant.name)”之后它会得到NullPointerException;这条线。
showData()
public void showData(View view){
RestaurantRepo repo = new RestaurantRepo(this);
ArrayList<HashMap<String, String>> restaurantList = repo.getRestaurantList();
if ( restaurantList.size()!=0 ) {
ListView lv = (ListView) findViewById(R.id.list);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
restaurantID = (TextView) view.findViewById(R.id.restaurantID);
String idRestaurant = restaurantID.getText().toString();
Bundle data = new Bundle();
data.putString("restaurantID", idRestaurant); // argument
Fragment fraUorD = null;
fraUorD = new EditFragment_Fix_UorD();
if ( null != fraUorD ) {
android.app.FragmentManager fragementManager = getFragmentManager();
android.app.FragmentTransaction transaction = fragementManager.beginTransaction();
transaction.replace(R.id.content_frame, fraUorD);
transaction.addToBackStack(null);
transaction.commit();
passData(data); // when transaction done , execute passData() -> i want to pass argument when the new fragment come out
}
}
});
ListAdapter adapter = new SimpleAdapter( MainActivity.this,restaurantList, R.layout.edit_fragment_fix_view,
new String[] { "id","name","type","price","phone","addr"},
new int[] {R.id.restaurantID, R.id.restaurantNAME,R.id.restaurantTYPE,
R.id.restaurantPRICE,R.id.restaurantPHONE,R.id.restaurantADDR});
lv.setAdapter(adapter);
}
else
Toast.makeText(this,"No restaurant!",Toast.LENGTH_SHORT).show(); // no data
passData()
public void passData(Bundle data){
int a = Integer.parseInt( data.getString("restaurantID", "")) ; // get it
Toast.makeText(this, ""+a,Toast.LENGTH_SHORT).show();
editTextNAME = (EditText) findViewById(R.id.editTextUpdateName);
editTextTYPE = (EditText) findViewById(R.id.editTextUpdateType);
editTextPRICE = (EditText) findViewById(R.id.editTextUpdatePrice);
editTextPHONE = (EditText) findViewById(R.id.editTextUpdatePhone);
editTextADDR = (EditText) findViewById(R.id.editTextUpdateAddr);
RestaurantRepo repo = new RestaurantRepo(this);
Restaurant restaurant = new Restaurant();
restaurant = repo.getRestaurantById(a);
// Everything is ok, but when executing codes below here will get errors
// ↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
editTextNAME.setText(restaurant.name);
editTextTYPE.setText(String.valueOf(restaurant.type));
editTextPRICE.setText(String.valueOf(restaurant.price));
editTextPHONE.setText(restaurant.phone);
editTextADDR.setText(restaurant.addr);
}
Logcat:
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.EditText.setText(java.lang.CharSequence)' at com.example.user.foody.MainActivity $ override.passData(MainActivity.java:284) at com.example.user.foody.MainActivity $ override.access $ dispatch(MainActivity.java) 在com.example.user.foody.MainActivity.passData(MainActivity.java:0) 在com.example.user.foody.MainActivity $ 2 $ override.onItemClick(MainActivity.java:244) at com.example.user.foody.MainActivity $ 2 $ override.access $ dispatch(MainActivity.java) 在com.example.user.foody.MainActivity $ 2.onItemClick(MainActivity.java:0)
答案 0 :(得分:0)
问题是在onViewCreated之前你无法在片段上找到view byId。
我建议阅读这篇关于通过片段传递数据的帖子。 http://gunhansancar.com/best-practice-to-instantiate-fragments-with-arguments-in-android/
重要的一块:
public static MyFragment newInstance(...)