无法 运行 应用程序在 Android 中使用 RecyclerView

Could not run application using RecyclerView in Android

我是 android 开发新手,我正在尝试使用 RecyclerView。我遵循了一些教程和 Google 文档,但是当我尝试在我的 activity

中使用适配器时,总是会发生同样的错误

mrec.setAdapter(madap); error in this line

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

public static boolean islogin;
private SharedPreferences sp;
private String username;
private RecyclerView mrec;
private MyAdapter madap;
public static ArrayList<Pair<String, Pair<Integer, String>>> Debtor;
public static ArrayList<Pair<String, Pair<Integer, String>>> Creditor;
public Update dataupdate;
Button btn;

@Override
protected void onCreate(Bundle savedInstanceState) {
    Debtor = new ArrayList<>();
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    islogin = false;
    ////
    mrec = (RecyclerView) findViewById(R.id.viewk);
    mrec.setHasFixedSize(false);
    mrec.setLayoutManager(new LinearLayoutManager(this));
    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            ///

            showProgress(true);
            dataupdate = new Update(username);
            dataupdate.execute((Void) null);
            madap.reset();
            showProgress(false);
            ///
            Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                    .setAction("Action", null).show();
        }
    });
    TextView tv = (TextView) findViewById(R.id.textview23);
    tv.setText("hjgjgjhh");

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.setDrawerListener(toggle);
    toggle.syncState();
    sp = getApplicationContext().getSharedPreferences("userP", 0);
    username = sp.getString("username", "");
    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
    //  navigationView.getMenu().getItem(2).setVisible(false);//to remove item from NavigationView :D


    // mrec.addItemDecoration(new RecyclerView.ItemDecoration(this, LinearLayoutManager.VERTICAL) {
    // });

    String tmp = "123";
    final String[] strarr = new String[2];
    strarr[1] = tmp;
    strarr[0] = "456";
    Pair<String, Pair<Integer, String>> tmmmp = new Pair<>("jhjkh", new Pair<>(22, "hjh"));
    Debtor.add(tmmmp);
    int gh = Debtor.size();
    madap = new MyAdapter(this, Debtor);
    mrec.setAdapter(madap);


    btn = (Button) findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            showProgress(true);
            //  madap.add(2, "741852963");
            username = sp.getString("username", "");
            Toast.makeText(getApplicationContext(), username, Toast.LENGTH_LONG).show();
            strarr[1] = "555555";
            mrec.setAdapter(madap);
        }
    });
}

@Override
public void onBackPressed() {
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    if (drawer.isDrawerOpen(GravityCompat.START)) {
        drawer.closeDrawer(GravityCompat.START);
    } else {
        super.onBackPressed();
    }
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
    } else if (id == R.id.nav_gallery) {

    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {
        Intent intent = new Intent(this, tabActivity.class);
        startActivity(intent);

    } else if (id == R.id.nav_share) {
        Intent intent = new Intent(this, RegisterActivity.class);
        startActivity(intent);

    } else if (id == R.id.nav_send) {
        Intent intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
    }

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}


////progressbat
private void showProgress(final boolean show) {
    // On Honeycomb MR2 we have the ViewPropertyAnimator APIs, which allow
    // for very easy animations. If available, use these APIs to fade-in
    // the progress spinner.
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
        int shortAnimTime = getResources().getInteger(android.R.integer.config_shortAnimTime);
        TextView tv = (TextView) findViewById(R.id.textview23);
        tv.setVisibility(show ? View.GONE : View.VISIBLE);
        tv.animate().setDuration(shortAnimTime).alpha(
                show ? 0 : 1).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                TextView tv = (TextView) findViewById(R.id.textview23);
                tv.setVisibility(show ? View.GONE : View.VISIBLE);
            }
        });
        ProgressBar mProgressView = (ProgressBar) findViewById(R.id.login_progress);
        mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
        mProgressView.animate().setDuration(shortAnimTime).alpha(
                show ? 1 : 0).setListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                ProgressBar mProgressView = (ProgressBar) findViewById(R.id.login_progress);
                mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
            }
        });
    } else {
        // The ViewPropertyAnimator APIs are not available, so simply show
        // and hide the relevant UI components.
        ProgressBar mProgressView = (ProgressBar) findViewById(R.id.login_progress);
        mProgressView.setVisibility(show ? View.VISIBLE : View.GONE);
        TextView tv = (TextView) findViewById(R.id.textview23);
        tv.setVisibility(show ? View.GONE : View.VISIBLE);
    }
}
}

适配器:

 public class MyAdapter extends RecyclerView.Adapter<MyAdapter.SimpleViewHolder> {

    private final Context mContext;
    private List<String> name;
    private List<String> status;
    private List<Integer> Money;
    private ArrayList<Pair<String, Pair<Integer, String>>> myadaptordata;


    public void remove(int position) {
        if (position < getItemCount()) {
            myadaptordata.remove(position);
            notifyItemRemoved(position);
        }
    }

    public void add(int position, Pair<String, Pair<Integer, String>> additem) {
        myadaptordata.add(additem);
        notifyItemInserted(myadaptordata.size());


    }

    public void reset() {
        notifyItemRangeChanged(0, getItemCount());
    }

    public static class SimpleViewHolder extends RecyclerView.ViewHolder {
        public final TextView name;
        public final TextView money;
        public final TextView status;

        public SimpleViewHolder(View view) {
            super(view);
            name = (TextView) view.findViewById(R.id.name);
            money = (TextView) view.findViewById(R.id.money);
            status = (TextView) view.findViewById(R.id.status);
        }
    }

    public MyAdapter(Context context, ArrayList<Pair<String, Pair<Integer, String>>> data) {
        mContext = context;
        if (data != null) {
            //name = new ArrayList<String>(Arrays.asList(data.));
            myadaptordata = data;
        }
    }

    public SimpleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        final View view = LayoutInflater.from(mContext).inflate(R.layout.listholder, parent, false);
        return new SimpleViewHolder(view);
    }

    @Override
    public void onBindViewHolder(SimpleViewHolder holder, final int position) {

        holder.name.setText(myadaptordata.get(position).first.toString());
        holder.money.setText(myadaptordata.get(position).second.first.intValue());
        holder.status.setText(myadaptordata.get(position).second.second.toString());
        holder.name.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(mContext, "Position =" + position, Toast.LENGTH_SHORT).show();
            }
        });
    }

    @Override
    public int getItemCount() {
        return myadaptordata.size();
    }
    }

日志:

01-28 20:01:51.061 25984-25984/com.example.mohsen158.test1 E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.mohsen158.test1, PID: 25984
android.content.res.Resources$NotFoundException: String resource ID #0x16
at android.content.res.Resources.getText(Resources.java:244)
at android.widget.TextView.setText(TextView.java:3888)
at com.example.mohsen158.test1.MyAdapter.onBindViewHolder(MyAdapter.java:75)
at com.example.mohsen158.test1.MyAdapter.onBindViewHolder(MyAdapter.java:20)
at android.support.v7.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:5217)
at android.support.v7.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:5250)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4487)
at android.support.v7.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:4363)
at android.support.v7.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:1961)
at android.support.v7.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1370)
at android.support.v7.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1333)
at android.support.v7.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:562)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:2900)
at android.support.v7.widget.RecyclerView.onLayout(RecyclerView.java:3071)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.support.design.widget.CoordinatorLayout.layoutChild(CoordinatorLayout.java:1037)
at android.support.design.widget.CoordinatorLayout.onLayoutChild(CoordinatorLayout.java:747)
at android.support.design.widget.ViewOffsetBehavior.onLayoutChild(ViewOffsetBehavior.java:42)
at android.support.design.widget.AppBarLayout$ScrollingViewBehavior.onLayoutChild(AppBarLayout.java:1156)
at android.support.design.widget.CoordinatorLayout.onLayout(CoordinatorLayout.java:760)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.support.v4.widget.DrawerLayout.onLayout(DrawerLayout.java:1043)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
at android.view.View.layout(View.java:14817)
at android.view.ViewGroup.layout(ViewGroup.java:4631)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1987)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1744)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRoo

问题出在这一行:

holder.money.setText(myadaptordata.get(position).second.first.intValue())

这是一个整数值。你应该使用

String.valueOf(myadaptordata.get(position).second.first.intValue())

holder.money.setText(myadaptordata.get(position).second.first.intValue());

抛出 Resources$NotFoundException 因为

myadaptordata.get(position).second.first.intValue() return 一个 intsetText() 方法需要一个 CharSequence 作为参数。


要解决此问题,请将其更改为:

holder.money.setText(String.valueOf(myadaptordata.get(position).second.first.intValue()));

holder.money.setText(""+myadaptordata.get(position).second.first.intValue());