1

I've created a constructor inside the CourseInnerPage activity and called it inside the OnBindViewHolder method and passed the getItemCount as it's input. But I got an exception and in my manifest.xml file says that this activity has no default constructor. What should I do?

Adapter class:

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 activity:

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 file:

<?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>

Exception:java.lang.InstantiationException: java.lang.Class has no zero argument constructor

Amin soley
  • 517
  • 2
  • 6
  • 15

3 Answers3

2

Do not keep constructors in the Activities. Instead use the onCreate method to initialize variables.

If you want to supply an id for the activity, use intent.putExtra("tag", id) before starting activity and get the value from the onCreate method of your activity as:

int id = getIntent().getIntExtra("tag", 0);
Nabin Bhandari
  • 15,949
  • 6
  • 45
  • 59
0

The problem is that your Activity has no zero argument constructor. The system needs it to automatically create that activity instances. But as general approach @Nabin' answer is perfectly correct.

algrid
  • 5,600
  • 3
  • 34
  • 37
0

When you implement a non-default constructor, it removes the no-arg one

You can either add it back, or remove the thought that calling new CourseInnerPage(int) is the correct way to make that class, because it is not

How do I pass data between Activities in Android application?

OneCricketeer
  • 179,855
  • 19
  • 132
  • 245