我试图在我的可折叠导航抽屉上添加一个单选按钮选项,我的动机是为我的课程创建一个测验。但是,在我的情况下 陈述,不接受活动
xml文件
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="20dp">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Question Here"
android:textAppearance="?android:attr/textAppearanceLarge" />
<RadioGroup
android:id="@+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RadioButton
android:id="@+id/radioButton1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 1" />
<RadioButton
android:id="@+id/radioButton2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 2" />
<RadioButton
android:id="@+id/radioButton3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Option 3" />
</RadioGroup>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="@+id/clearBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onClear"
android:text="Clear " />
<Button
android:id="@+id/submitBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onSubmit"
android:text="Submit" />
</LinearLayout>
</LinearLayout>
的java
package com.android.pet.view;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import com.doepiccoding.navigationdrawer.R;
public class Lesson111 extends Activity {
private RadioGroup radioGroup;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.onepointthree);
/* Initialize Radio Group and attach click handler */
radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
radioGroup.clearCheck();
/* Attach CheckedChangeListener to radio group */
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
RadioButton rb = (RadioButton) group.findViewById(checkedId);
if(null!=rb && checkedId > -1){
Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
});
}
public void onClear(View v) {
/* Clears all selected radio buttons to default */
radioGroup.clearCheck();
}
public void onSubmit(View v) {
RadioButton rb = (RadioButton) radioGroup.findViewById(radioGroup.getCheckedRadioButtonId());
Toast.makeText(Lesson111.this, rb.getText(), Toast.LENGTH_SHORT).show();
}
}
主要活动java
package com.android.pet.view;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.content.Context;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.widget.DrawerLayout.DrawerListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.ImageView;
import android.widget.TextView;
import com.doepiccoding.navigationdrawer.R;
public class NavigationActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
ImageView home;
Fragment fragment = null;
TextView appname;
ExpandableListView expListView;
HashMap<String, List<String>> listDataChild;
ExpandableListAdapter listAdapter;
List<String> listDataHeader;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String fontPath = "fonts/Shadow Boxing.ttf";
setContentView(R.layout.activity_navigation);
home = (ImageView)findViewById(R.id.home);
home.setOnClickListener(homeOnclickListener);
appname = (TextView)findViewById(R.id.appname);
Typeface tf = Typeface.createFromAsset(this.getAssets(), fontPath);
appname.setTypeface(tf);
setUpDrawer();
}
/**
*
* Get the names and icons references to build the drawer menu...
*/
private void setUpDrawer() {
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setScrimColor(getResources().getColor(android.R.color.transparent));
mDrawerLayout.setDrawerListener(mDrawerListener);
expListView = (ExpandableListView) findViewById(R.id.lvExp);
prepareListData();
listAdapter = new ExpandableListAdapter(this, listDataHeader, listDataChild);
// setting list adapter
expListView.setAdapter(listAdapter);
fragment = new Lesson1();
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
expListView.setOnChildClickListener(new OnChildClickListener() {
@Override
public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
switch (groupPosition) {
case 0:
switch (childPosition) {
case 0:
fragment = new Lesson1();
break;
case 1:
fragment = new Lesson11();
break;
现在我在这里遇到问题,因为它不接受某项活动。如果我改变了fragment = new Lesson111(); to activity = new Lesson111(); 。活动将变为红色,表示无法解析符号活动。有什么方法可以解决这个问题吗?
case 2:
fragment = new Lesson111();
break;
default:
break;
}
break;
继续主要活动java
case 1:
switch (childPosition) {
case 0:
fragment = new Lesson2();
break;
case 1:
fragment = new Lesson22();
break;
case 2:
fragment = new Lesson222();
break;
default:
break;
}
break;
case 2:
switch (childPosition) {
case 0:
fragment = new Lesson3();
break;
case 1:
fragment = new Lesson33();
break;
case 2:
fragment = new Lesson333();
break;
default:
break;
}
break;
case 3:
switch (childPosition) {
case 0:
fragment = new Lesson4();
break;
case 1:
fragment = new Lesson44();
break;
case 2:
fragment = new Lesson444();
break;
default:
break;
}
break;
case 4:
switch (childPosition) {
case 0:
fragment = new Lesson5();
break;
case 1:
fragment = new Lesson55();
break;
case 2:
fragment = new Lesson555();
break;
default:
break;
}
break;
default:
break;
}
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
return false;
}
});
}
View.OnClickListener homeOnclickListener = new OnClickListener() {
@Override
public void onClick(View v) {
if(mDrawerLayout.isDrawerOpen(expListView)){
mDrawerLayout.closeDrawer(expListView);
}else{
mDrawerLayout.openDrawer(expListView);
}
}
};
private OnItemClickListener mDrawerItemClickedListener = new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
switch(position){
case 0:
fragment = new Lesson1();
break;
case 1:
fragment = new Lesson2();
break;
case 2:
fragment = new Lesson3();
break;
case 3:
fragment = new Lesson4();
break;
case 4:
fragment = new Lesson5();
break;
default:
return;
}
getSupportFragmentManager().beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerLayout.closeDrawer(expListView);
}
};
// Catch the events related to the drawer to arrange views according to this
// action if necessary...
private DrawerListener mDrawerListener = new DrawerListener() {
@Override
public void onDrawerStateChanged(int status) {
}
@Override
public void onDrawerSlide(View view, float slideArg) {
}
@Override
public void onDrawerOpened(View view) {
}
@Override
public void onDrawerClosed(View view) {
}
};
private void prepareListData() {
listDataHeader = new ArrayList<String>();
listDataChild = new HashMap<String, List<String>>();
// Adding child data
listDataHeader.add("Lesson 1");
listDataHeader.add("Lesson 2");
listDataHeader.add("Lesson 3");
listDataHeader.add("Lesson 4");
listDataHeader.add("Lesson 5");
// Adding child data
List<String> Lesson1 = new ArrayList<String>();
Lesson1.add("Objectives");
Lesson1.add("Contents");
Lesson1.add("Quiz");
List<String> Lesson2 = new ArrayList<String>();
Lesson2.add("Objectives");
Lesson2.add("Contents");
Lesson2.add("Quiz");
List<String> Lesson3 = new ArrayList<String>();
Lesson3.add("Objectives");
Lesson3.add("Contents");
Lesson3.add("Quiz");
List<String> Lesson4 = new ArrayList<String>();
Lesson4.add("Objectives");
Lesson4.add("Contents");
Lesson4.add("Quiz");
List<String> Lesson5 = new ArrayList<String>();
Lesson5.add("Objectives");
Lesson5.add("Contents");
Lesson5.add("Quiz");
listDataChild.put(listDataHeader.get(0), Lesson1); // Header, Child data
listDataChild.put(listDataHeader.get(1), Lesson2);
listDataChild.put(listDataHeader.get(2), Lesson3);
listDataChild.put(listDataHeader.get(3), Lesson4);
listDataChild.put(listDataHeader.get(4), Lesson5);
}
public class ExpandableListAdapter extends BaseExpandableListAdapter {
private Context _context;
private List<String> _listDataHeader; // header titles
// child data in format of header title, child title
private HashMap<String, List<String>> _listDataChild;
public ExpandableListAdapter(Context context, List<String> listDataHeader,
HashMap<String, List<String>> listChildData) {
this._context = context;
this._listDataHeader = listDataHeader;
this._listDataChild = listChildData;
}
@Override
public Object getChild(int groupPosition, int childPosititon) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.get(childPosititon);
}
@Override
public long getChildId(int groupPosition, int childPosition) {
return childPosition;
}
@Override
public View getChildView(int groupPosition, final int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
final String childText = (String) getChild(groupPosition, childPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_item, null);
}
TextView txtListChild = (TextView) convertView
.findViewById(R.id.lblListItem);
txtListChild.setText(childText);
return convertView;
}
@Override
public int getChildrenCount(int groupPosition) {
return this._listDataChild.get(this._listDataHeader.get(groupPosition))
.size();
}
@Override
public Object getGroup(int groupPosition) {
return this._listDataHeader.get(groupPosition);
}
@Override
public int getGroupCount() {
return this._listDataHeader.size();
}
@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}
@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
String headerTitle = (String) getGroup(groupPosition);
if (convertView == null) {
LayoutInflater infalInflater = (LayoutInflater) this._context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = infalInflater.inflate(R.layout.list_group, null);
}
TextView lblListHeader = (TextView) convertView
.findViewById(R.id.lblListHeader);
lblListHeader.setTypeface(null, Typeface.BOLD);
lblListHeader.setText(headerTitle);
return convertView;
}
@Override
public boolean hasStableIds() {
return false;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition) {
return true;
}
}
}