-1

目标:我正在尝试使用 RecyclerView 添加 EditText 字段和 PlaceAutocompleteFragment 字段

注意:PlaceAutoComplete 在添加 RecyclerView 之前工作。此外,EditTexts 在 RecyclerView 中工作正常,但是一旦我尝试将 PlaceAutoComplete 与它一起使用,就会出现 NullPointer 异常

是的,我在stackoverflow上搜索并搜索了这个错误以及AutoPlacecomplete,例如(谷歌位置片段上的Java空指针异常)和(没有找到类“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 是如何工作的(特别是因为它在使用回收器视图之前工作......)?

注册业务活动:

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();
    }


}

我的适配器:

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();
    }

}
4

1 回答 1

0

您从 Activity ( activity_register_businessprofile.xml) 中找到 Fragment 而不是适配器中的行列表。因此它是空的

就个人而言,我不建议将 Fragments 放入 Recyclerview Adapter。尝试只使用一个PlaceAutocomplete小部件,或者将 Fragment 移动到 Activity 中,而不是适配器布局

于 2017-11-22T13:33:15.597 回答