我正在尝试制作一个使用RecyclerView的应用程序,以显示从项目数组中获得的项目列表。一切似乎都很好,除非我尝试在手机(Lenovo K5 Note,Android 6.0)上运行该应用程序,但该应用程序立即崩溃。我得到的错误是:
两个错误都在Adapter类中,我已将其标记为。
08-15 21:11:37.771 3163-3163/com.example.mayankthakur.reminderapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mayankthakur.reminderapp, PID: 3163
android.content.res.Resources$NotFoundException: Resource ID #0x0
at android.content.res.Resources.getValue(Resources.java:1552)
at android.content.res.Resources.loadXmlResourceParser(Resources.java:3020)
at android.content.res.Resources.getLayout(Resources.java:1330)
at android.view.LayoutInflater.inflate(LayoutInflater.java:421)
at com.example.mayankthakur.reminderapp.MyAdapter.onCreateViewHolder(MyAdapter.java:36)
at com.example.mayankthakur.reminderapp.MyAdapter.onCreateViewHolder(MyAdapter.java:10)
at android.support.v7.widget.RecyclerView$Adapter.createViewHolder(RecyclerView.java:6685)
at android.support.v7.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:5869)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5752)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:5748)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2232)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1559)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1519)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:614)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:3812)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3529)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:4082)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1189)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:443)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:2001)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1844)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1753)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:396)
at android.widget.FrameLayout.onLayout(FrameLayout.java:333)
at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2769)
at android.view.View.layout(View.java:16967)
at android.view.ViewGroup.layout(ViewGroup.java:5584)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2547)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:2250)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1321)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6732)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:894)
at android.view.Choreographer.doCallbacks(Choreographer.java:696)
at android.view.Choreographer.doFrame(Choreographer.java:631)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:880)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:207)
at android.app.ActivityThread.main(ActivityThread.java:5740)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCall
商品类:
public class Item extends AppCompatActivity implements View.OnClickListener{
public Item[] data;
EditText tName;
EditText difficulty;
Button addResource;
private String topicName;
private int time;
private int adifficulty;
private int size;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add_resource);
size = data.length;
tName = findViewById(R.id.editText);
difficulty = findViewById(R.id.editText2);
addResource = findViewById(R.id.button);
addResource.setOnClickListener(this);
MainActivity.method(data);
}
public Item (String name, int difficulty){
this.topicName = name;
this.adifficulty = difficulty;
}
public String getTopicName() {
return topicName;
}
public int getTime(){
return time;
}
public int getDifficulty(){
return adifficulty;
}
@Override
public void onClick(View view) {
topicName = tName.getText().toString();
String i = difficulty.getText().toString();
adifficulty = Integer.parseInt(i);
Item thing = new Item (topicName, adifficulty);
data[size + 1 ] = thing;
}
}
MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
static Item thing = new Item("hello", 34);1 // assigning a random value to the dataset to make sure i dont get any nullpointerexceptions
static Item[] theDataset = {thing};
public static void method (Item[] data){
theDataset = data;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FloatingActionButton nBut = (FloatingActionButton) findViewById(R.id.fab);
nBut.setOnClickListener(this);
RecyclerView mRecyclerView = (RecyclerView) findViewById(R.id.mRecyclerView);
mRecyclerView.setHasFixedSize(true);
LinearLayoutManager mLayoutManager = new LinearLayoutManager(this);
mRecyclerView.setLayoutManager(mLayoutManager);
MyAdapter mAdapter = new MyAdapter(theDataset);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onClick(View view) {
switch (view.getId()) {
case com.example.mayankthakur.reminderapp.R.id.fab:
Intent i = new Intent(this, Item.class );
startActivity(i);
break;
default:
break;
}
}
}
AdapterClass:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> { //ERROR
private Item [] mDataset;
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView mTextView;
public ViewHolder(final View v) {
super(v);
mTextView = (TextView) v.findViewById(R.id.textView);
}
}
// Provide a reference to th e views for each data item
// Complex data items may need more than one view per item, and
// you provide access to all the views for a data item in a view holder
// Provide a suitable constructor (depends on the kind of dataset)
public MyAdapter(Item[] myDataset) {
mDataset = myDataset;
}
// Create new views (invoked by the layout manager)
@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
// create a new view
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false); //ERROR
return new ViewHolder(view);
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder holder, int position) {
// - get element from your dataset at this position
// - replace the contents of the view with that element
holder.mTextView.setText(mDataset[position].getTopicName());
}
// Return the size of your dataset (invoked by the layout manager)
@Override
public int getItemCount() {
return mDataset.length;
}
}
任何有关此问题的帮助将不胜感激!
答案 0 :(得分:2)
尝试一下:
LayoutInflater.from(parent.getContext()).inflate(R.layout.your_item.xml, parent, false);
答案 1 :(得分:1)
问题出在viewType
应该是您的布局xml
LayoutInflater.from(parent.getContext()).inflate(R.layout.your_item.xml, parent, false);
答案 2 :(得分:0)
您看到的Exception
是由此行引起的
final View view = LayoutInflater.from(parent.getContext()).inflate(viewType, parent, false);
在这里,您从onCreateViewHolder()
传入参数 viewType 。因为您没有在getItemViewType()
中覆盖RecyclerView.Adapter
,所以它将始终为0。
此方法的默认实现返回0,并假设适配器具有单一视图类型。
(引自documentation on RecyclerView.Adapter)
但是inflate()
此时需要一个有效的布局ID,类似于R.layout.list_row
,其中可以在res/layout
文件夹中找到list_row.xml。
0永远不是有效的资源标识符,因此您的应用程序崩溃了
注意:仅在需要根据当前位置的数据为不同的布局充气时(运行时将调用getItemViewType(int position)
以确定正确的值)时,才需要评估 viewType < / p>