2017-08-27 50 views
1

我在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> 

抗辯n:java.lang.InstantiationException:java.lang.Class沒有零參數構造函數

回答

2

不要在構造函數中保留構造函數。請使用onCreate方法來初始化變量。

如果你想從你的活動onCreate方法提供一個ID爲活動,開始活動之前使用intent.putExtra("tag", id)和獲得的價值:

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

再次感謝你;) –

0

的問題是,你的活動有沒有零參數構造函數。系統需要它自動創建活動實例。但是,作爲@Nabin的一般方法,答案是完全正確的。

相關問題