我正在为我的应用程序使用片段。我还使用了一个导航抽屉,该抽屉通过抽屉中的按钮打开片段。
我正在尝试为我的recyclerview显示一些基本图像和文本及其适配器和arraylist。
我遇到的问题是我不知道如何修改代码以对片段使用recyclerview,因为我看到的所有教程都没有使用片段。
下面是我的活动代码:
public class secondActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
private RecyclerView mRecyclerView;
private RecyclerView.Adapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ArrayList<exampleItemBooks> exampleList = new ArrayList<>();
exampleList.add(new exampleItemBooks(R.drawable.artofwar, "Line 1", "Line
2"));
exampleList.add(new exampleItemBooks(R.drawable.aristotle, "Line 3",
"Line 4"));
exampleList.add(new exampleItemBooks(R.drawable.caesarbook, "Line 5",
"Line 6"));
exampleList.add(new exampleItemBooks(R.drawable.platorepublic, "Line 7",
"Line 8"));
exampleList.add(new exampleItemBooks(R.drawable.senecaletters, "Line 9",
"Line 10"));
exampleList.add(new exampleItemBooks(R.drawable.thehistoryofmypeople,
"Line 11", "Line 12"));
exampleList.add(new exampleItemBooks(R.drawable.theprince, "Line 13",
"Line 14"));
exampleList.add(new exampleItemBooks(R.drawable.thritysixstrategems,
"Line 15", "Line 16"));
exampleList.add(new exampleItemBooks(R.drawable.medidations, "Line 17",
"Line 18"));
mRecyclerView = findViewById(R.id.recyclerViewBooks);
mRecyclerView.setHasFixedSize(false);
mLayoutManager = new LinearLayoutManager(this);
mAdapter = new exampleBooksAdapter(exampleList);
mRecyclerView.setLayoutManager(mLayoutManager);
mRecyclerView.setAdapter(mAdapter);
Toolbar toolbar = findViewById(R.id.toolbarMain);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer,
toolbar, R.string.navigation_open_drawer,
R.string.navigation_close_drawer);
drawer.addDrawerListener(toggle);
toggle.syncState();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new HomeFragment()).commit();
//Set the button as checked.
navigationView.setCheckedItem(R.id.nav_home);
}
}
下面是我的片段类代码,该片段代码负责我使用recyclerview的片段:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BooksFragment extends Fragment {
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_books, container, false);
View view = inflater.inflate(R.layout.fragment_books, container, false);
RecyclerView recyclerView = view.findViewById(R.id.recyclerViewBooks);
//mRecyclerView = findViewById(R.id.recyclerViewBooks);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
return view;
}
}
以下是我的recyclerView适配器:
public class exampleBooksAdapter extends RecyclerView.Adapter<exampleBooksAdapter.ExampleViewHolder> {
private ArrayList<exampleItemBooks> mExampleList;
public static class ExampleViewHolder extends RecyclerView.ViewHolder {
public ImageView mImageView;
public TextView mTextView1;
public TextView mTextView2;
public ExampleViewHolder(View itemView) {
super(itemView);
mImageView = itemView.findViewById(R.id.imageViewCards);
mTextView1 = itemView.findViewById(R.id.textViewCard1);
mTextView1 = itemView.findViewById(R.id.textViewCard2);
}
}
public exampleBooksAdapter(ArrayList<exampleItemBooks> exampleItemBooks) {
mExampleList = exampleItemBooks;
}
@Override
public ExampleViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.example_item_books, parent, false);
ExampleViewHolder evh = new ExampleViewHolder(v);
return evh;
}
@Override
public void onBindViewHolder(@NonNull ExampleViewHolder holder, int position) {
exampleItemBooks currentItem = mExampleList.get(position);
holder.mImageView.setImageResource(currentItem.getImageResource());
holder.mTextView1.setText(currentItem.getText1());
holder.mTextView2.setText(currentItem.getText2());
}
@Override
public int getItemCount() {
return mExampleList.size();
}
}
下面是我的arraylist类:
public class exampleItemBooks {
private int mImageResource;
private String mText1;
private String mText2;
public exampleItemBooks(int imageResource, String text1, String text2) {
mImageResource = imageResource;
mText1 = text1;
mText2 = text2;
}
public int getImageResource() {
return mImageResource;
}
public String getText1() {
return mText1;
}
public String getText2() {
return mText2;
}
}
我不断得到的错误是:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.warstories, PID: 5753
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.warstories/com.example.warstories.secondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2817)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setHasFixedSize(boolean)' on a null object reference
at com.example.warstories.secondActivity.onCreate(secondActivity.java:42)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
关键错误说明:
Unable to start activity ComponentInfo{com.example.warstories/com.example.warstories.secondActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView$LayoutManager)' on a null object reference
原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“ void android.support.v7.widget.RecyclerView.setLayoutManager(android.support.v7.widget.RecyclerView $ LayoutManager)”。
有帮助吗?
答案 0 :(得分:1)
您的代码失败,因为您的活动正在尝试访问作为Fragment布局一部分的RecyclerView
,并且您调用setContentView()
之后(甚至在{{ 1}}操作,由于您使用replace()
而不是commit()
),因此只能异步使用。
片段应该(在尽可能多的程度上)是独立的。这意味着,如果您的片段拥有commitNow()
,则它应该负责将数据加载到其中:而不是您的活动。
您应该将活动中与RecyclerView
接触的所有代码移到片段上-理想情况下,应移至RecyclerView
之类的方法中,该方法可让您访问已拥有的视图夸大了onViewCreated()
,是致电onCreateView()
并获取findViewById()
的合适位置。