关于此活动的清单中的错误没有默认构造函数

时间:2017-08-27 17:34:12

标签: java android

我在CourseInnerPage活动中创建了一个构造函数,并在OnBindViewHolder方法中调用它并传递getItemCount作为它的输入。但是我得到了一个异常,在我的manifest.xml文件中说这个活动没有默认的构造函数。我该怎么办?

适配器类

public class AdapterVRList extends RecyclerView.Adapter<AdapterVRList.CourseViewHolder> {

private Context context;
private List<DObjectVrList> DObjectVrListList;

public AdapterVRList(Context context, List<DObjectVrList> DObjectVrListList){
    this.context = context;
    this.DObjectVrListList = DObjectVrListList;
}
public AdapterVRList(Context context){

}


@Override
public CourseViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    View view = LayoutInflater.from(context).inflate(R.layout.card_vr,parent,false);
    return new CourseViewHolder(view);
}

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

    DObjectVrList DObjectVrList = DObjectVrListList.get(position);
    holder.CourseText.setText(DObjectVrList.getTitleCourse());

    holder.itemView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CourseInnerPage course = new CourseInnerPage(getItemCount());
            Intent intent = new Intent(context,CourseInnerPage.class);
            context.startActivity(intent);
        }
    });
}

@Override
public int getItemCount() {
    return 8;
}

public class CourseViewHolder extends RecyclerView.ViewHolder{

    private TextView CourseText;

    public CourseViewHolder(View itemView) {
        super(itemView);
        CourseText = (TextView) itemView.findViewById(R.id.course_text);
    }
}
}

CourseInnerPage活动:

public class CourseInnerPage extends AppCompatActivity {

private static final String TAG = "CourseInnerPage";
private FloatingActionButton ShowButton;
private NestedScrollView CourseShow;
private int id;

public CourseInnerPage(int Id_Card){
    id = Id_Card;
}


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

    CourseShow = (NestedScrollView) findViewById(R.id.course_descript);
    CourseShow.setVisibility(View.INVISIBLE);
    CoordinatorLayout layout = (CoordinatorLayout) findViewById(R.id.inner_course);
    for(int i=1;i<=8;i++){
        switch (id) {
            case 1:
                layout.setBackground(getResources().getDrawable(R.drawable.one_bg, null));
                ShowButton.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        TeacherNet teacherNet = new TeacherNet(CourseInnerPage.this);
                        JSONObject requestObject = new JSONObject();
                        try {
                            requestObject.put("vrcode","1");
                            teacherNet.SelectContent(requestObject, new TeacherNet.JSONsendListener() {
                                @Override
                                public void JSONsent(String success) {
                                    if (success=="OK"){
                                        Log.i(TAG, "JSONsent: Everything is Ok");
                                    }
                                }
                            });
                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                });
                break;
            case 2:
                layout.setBackground(getResources().getDrawable(R.drawable.two_bg, null));
                break;
            case 3:
                layout.setBackground(getResources().getDrawable(R.drawable.three_bg,null));
                break;
            case 4:
                layout.setBackground(getResources().getDrawable(R.drawable.four_bg,null));
                break;
            case 5:
                layout.setBackground(getResources().getDrawable(R.drawable.five_bg,null));
                break;
            case 6:
                layout.setBackground(getResources().getDrawable(R.drawable.six_bg,null));
                break;
            case 7:
                layout.setBackground(getResources().getDrawable(R.drawable.seven_bg,null));
                break;
            case 8:
                layout.setBackground(getResources().getDrawable(R.drawable.eight_bg,null));
                break;
        }
    }


}
}

AndroidManifest文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="realup.ir.teacher">

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Teacher"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".Activity.Login">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".Activity.Main"
        android:parentActivityName=".Activity.Student">
        <meta-data
            android:name="android.support.PARENT_ACTIVTY"
            android:value=".Student" />
    </activity>
    <activity android:name=".Activity.Student" />
    <activity android:name=".Activity.CourseInnerPage"/>
</application>

</manifest>

异常:java.lang.InstantiationException:java.lang.Class没有零参数构造函数

3 个答案:

答案 0 :(得分:2)

不要将构造函数保留在“活动”中。而是使用onCreate方法初始化变量。

如果您要为活动提供ID,请在开始活动前使用intent.putExtra("tag", id),并将活动的onCreate方法的值视为:

int id = getIntent().getIntExtra("tag", 0);

答案 1 :(得分:0)

问题是您的Activity没有零参数构造函数。系统需要它来自动创建活动实例。但正如一般方法@Nabin的回答是完全正确的。

答案 2 :(得分:0)

实现非默认构造函数时,它会删除no-arg

您可以将其添加回来,或者删除调用new CourseInnerPage(int)是制作该类的正确方法的想法,因为它不是

How do I pass data between Activities in Android application?