目标:我正在尝试使用RecyclerView添加EditText字段和PlaceAutocompleteFragment字段
注意:在添加RecyclerView之前,PlaceAutoComplete工作正常。此外,EditTexts在RecyclerView中运行良好,但是一旦我尝试使用PlaceAutoComplete,那就是当我得到NullPointer异常时
是的我在stackoverflow上搜索和搜索此错误以及AutoPlacecomplete,例如(Java null pointer exception on google places fragment)和(Didn't find class "android.view.fragment")
完全错误:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.location.places.ui.PlaceAutocompleteFragment.setOnPlaceSelectedListener(com.google.android.gms.location.places.ui.PlaceSelectionListener)' on a null object reference
at com.netgalaxystudios.timeclock.RegisterBusinessActivity.onCreate(RegisterBusinessActivity.java:119)
第119行引用:autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener(){
来自row_list.xml的片段:
<fragment
android:id="@+id/place_autocomplete_fragment"
android:layout_width="0dp"
android:layout_weight="2.55"
android:layout_height="wrap_content"
android:name="com.google.android.gms.location.places.ui.PlaceAutocompleteFragment"
/>
似乎从我的代码中设置了PlaceAutocomplete,所以不确定它的NullPointer(特别是因为它在使用recycleler视图之前工作了......)??
RegisterBusinessActivity:
public class RegisterBusinessActivity extends Activity {
//EditText businessLocationET;
//FIREBASE ITEMS
private static FirebaseUser currentUser;
private static final String TAG = "RealtimeDB";
private FirebaseDatabase database;
private DatabaseReference dbRef;
private EditText businessName;
private EditText businessLocation;
//RECYCLERVIEW ITEMS
private Context mContext;
LinearLayout mLinearLayout;
LinearLayout mLinearLayout2;
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
int position;
ArrayList<LinearLayout> linearLayoutList = new ArrayList<LinearLayout>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register_businessprofile);
Button addLocationBtn = (Button) findViewById(R.id.addLocationBtn);
// Get the widgets reference from XML layout
mLinearLayout = (LinearLayout) findViewById(R.id.rl);
mLinearLayout2 = (LinearLayout) findViewById(R.id.rl2);
mRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
mContext = getApplicationContext();
linearLayoutList.add(mLinearLayout);
linearLayoutList.add(mLinearLayout2);
// Define a layout for RecyclerView
mLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
mRecyclerView.setLayoutManager(mLayoutManager);
// Initialize a new instance of RecyclerView Adapter instance
mAdapter = new MyAdapter(mContext);
// Set the adapter for RecyclerView
mRecyclerView.setAdapter(mAdapter);
mAdapter.updateDataSet(linearLayoutList);
position = 0;
//FIREBASE FIELDS
businessName = (EditText) findViewById(R.id.nameOfBusinessET);
database = FirebaseDatabase.getInstance();
dbRef = database.getReference("/data");
currentUser =
FirebaseAuth.getInstance().getCurrentUser();
//CODE FOR LOCATION AUTOCOMPLETE
PlaceAutocompleteFragment autocompleteFragment = (PlaceAutocompleteFragment)
getFragmentManager().findFragmentById(R.id.place_autocomplete_fragment);
autocompleteFragment.setOnPlaceSelectedListener(new PlaceSelectionListener() {
@Override
public void onPlaceSelected(Place place) {
// TODO: Get info about the selected place.
Log.i("TAG", "Place: " + place.getName());
//businessLocationET.setText(place.getName());
}
@Override
public void onError(Status status) {
// TODO: Handle the error.
Log.i("TAG", "An error occurred: " + status);
}
});
//ADD EXTRA LOCATIONS
addLocationBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
linearLayoutList.add(mLinearLayout);
linearLayoutList.add(mLinearLayout2);
mAdapter.updateDataSet(linearLayoutList);
}
});
} //END OF ONCREATE
DatabaseReference.CompletionListener completionListener =
new DatabaseReference.CompletionListener() {
@Override
public void onComplete(DatabaseError databaseError,
DatabaseReference databaseReference) {
if (databaseError != null) {
notifyUser(databaseError.getMessage());
}
}
};
private void notifyUser(String message) {
Toast.makeText(RegisterBusinessActivity.this, message,
Toast.LENGTH_SHORT).show();
}
}
MyAdapter:
class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder>{
private ArrayList<LinearLayout> mDataSet;
private Context mContext;
private Random mRandom = new Random();
public MyAdapter(Context context){
mContext = context;
}
public static class ViewHolder extends RecyclerView.ViewHolder{
public EditText mTextEditName, mTextEditLocation;
public LinearLayout mRelativeLayout; //from the (Main)Activity XML
public ViewHolder(View v){
super(v);
mTextEditName = (EditText) v.findViewById(R.id.nameOfBusinessET);
//mTextEditLocation = (EditText) v.findViewById(R.id.bizLocation);
mRelativeLayout = (LinearLayout) v.findViewById(R.id.rl);
}
}
void updateDataSet(ArrayList<LinearLayout> myArrayList) {
mDataSet = myArrayList;
notifyDataSetChanged();
}
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
// Create a new View
View v = LayoutInflater.from(mContext).inflate(R.layout.row_layout,parent,false);
ViewHolder vh = new ViewHolder(v);
return vh;
}
//A ViewHolder object stores each of the component views inside the tag field of the Layout, so you can immediately access them without the need to look them up repeatedly.
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
//holder.mTextEditName.setText("");
}
@Override
public int getItemCount(){
return mDataSet.size();
}
}
答案 0 :(得分:0)
您正在从活动(formControlName
)中找到片段而不是适配器中的行列表。因此它是空的
就个人而言,我不会建议将Fragments放入Recyclerview适配器。请尝试仅使用activity_register_businessprofile.xml
小部件,或将片段移动到Activity中,而不是适配器布局