RecyclerView的onClick方法崩溃了应用程序

时间:2016-03-17 12:25:59

标签: java android android-recyclerview recycler-adapter

下面是我的MainActivity.java (我删除了一些不必要的代码。我的主要活动是导航查看器活动)

public class MainActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

RecyclerView recyclerView;
RecyclerView.Adapter adapter;
RecyclerView.LayoutManager layoutManager;
ArrayList<MainStoryTile> list = new ArrayList<MainStoryTile>();
int[] image_id = { R.drawable.sample, R.drawable.sample, R.drawable.sample};

String[] name, email, mobile;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    name = getResources().getStringArray(R.array.person_name);
    email = getResources().getStringArray(R.array.person_email);
    mobile = getResources().getStringArray(R.array.person_mobile);

    int count = 0;

    for(String NAME: name){
        MainStoryTile contact = new MainStoryTile(image_id[count], NAME, email[count], mobile[count]);
        count++;
        list.add(contact);
    }


    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    layoutManager = new LinearLayoutManager(this);
    recyclerView.setLayoutManager(layoutManager);
    recyclerView.setHasFixedSize(true);

    adapter = new MainStoryAdapter(list);

    recyclerView.setAdapter(adapter);
}

@Override
public void onBackPressed() {
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
}

@SuppressWarnings("StatementWithEmptyBody")
@Override
public boolean onNavigationItemSelected(MenuItem item) {
    return true;
}
}

以下是我的Adapter类。 (注意:有一个内部类)

public class MainStoryAdapter extends RecyclerView.Adapter<MainStoryAdapter.StoryViewHolder>{

    ArrayList<MainStoryTile> contacts = new ArrayList<MainStoryTile>();

    public MainStoryAdapter(ArrayList<MainStoryTile> contacts){
        this.contacts = contacts;
    }


    @Override
    public StoryViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.content_main,parent,false );

        StoryViewHolder storyViewHolder = new StoryViewHolder(view);
        return storyViewHolder;
    }

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

        MainStoryTile CON = contacts.get(position);
        holder.person_img.setImageResource(CON.getImage_id());
        holder.person_name.setText(CON.getName());
        holder.person_email.setText(CON.getEmail());
        holder.person_mobile.setText(CON.getMobile());
    }


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

    public static class StoryViewHolder extends RecyclerView.ViewHolder  implements View.OnClickListener{


        ImageView person_img;
        TextView person_name, person_email, person_mobile;

        public StoryViewHolder(View view){
            super(view);
            person_img = (ImageView) view.findViewById(R.id.person_image);
            person_name = (TextView) view.findViewById(R.id.person_name);
            person_email = (TextView) view.findViewById(R.id.person_email);
            person_mobile = (TextView) view.findViewById(R.id.person_mobile);
            view.setOnClickListener(this);
        }

        @Override
        public void onClick(View v) {
            Log.d("MY LOG", "sdurfhiusrhdidysdysdysdysdys r");
        }
    }

}

当我点击某个项目时,我的应用程序崩溃了。下面是错误日志:

03-17 15:20:38.638 28744-28744/com.storyteller.pro.storyteller E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.storyteller.pro.storyteller, PID: 28744
    java.lang.IllegalStateException: Could not execute method of the activity
    at android.view.View$1.onClick(View.java:4286)
    at android.view.View.performClick(View.java:5242)
    at android.view.View$PerformClick.run(View.java:21196)
    at android.os.Handler.handleCallback(Handler.java:739)
    at android.os.Handler.dispatchMessage(Handler.java:95)
    at android.os.Looper.loop(Looper.java:145)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
    Caused by: java.lang.reflect.InvocationTargetException
    at java.lang.reflect.Method.invoke(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:372)
    at android.view.View$1.onClick(View.java:4281)
    at android.view.View.performClick(View.java:5242) 
    at android.view.View$PerformClick.run(View.java:21196) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:6938) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
    Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
    at android.view.ViewGroup.addViewInner(ViewGroup.java:4277)
    at android.view.ViewGroup.addView(ViewGroup.java:4127)
    at android.view.ViewGroup.addView(ViewGroup.java:4068)
    at android.view.ViewGroup.addView(ViewGroup.java:4041)
    at android.support.v7.app.AppCompatDelegateImplV7.setContentView(AppCompatDelegateImplV7.java:247)
    at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:114)
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at android.view.View$1.onClick(View.java:4281) 
    at android.view.View.performClick(View.java:5242) 
    at android.view.View$PerformClick.run(View.java:21196) 
    at android.os.Handler.handleCallback(Handler.java:739) 
    at android.os.Handler.dispatchMessage(Handler.java:95) 
    at android.os.Looper.loop(Looper.java:145) 
    at android.app.ActivityThread.main(ActivityThread.java:6938) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199) 
        03-17 15:20:43.048 28744-28744/com.storyteller.pro.storyteller I/Process: Sending signal. PID: 28744 SIG: 9

更新即使我在view.setOnClickListener(this);的{​​{1}}中评论了以下内容,我点击某个项目时我的应用仍会崩溃。 以下是那些想要了解更多信息的人的项目&gt; project zip version

1 个答案:

答案 0 :(得分:1)

我检查了您的项目,您为什么要在android:onClick="setContentView"的{​​{1}}版面中定义content_main.xml

删除该属性,它可以正常工作。