我正在尝试修改我的布局,但每当我使用图形布局时,它最终会破坏并在下面看到错误。我知道这不是问题,因为它可以解除我的更改。只有在我使用图形布局时才会这样。如果我使用XML它就可以正常工作。我知道它说它在我的java代码中,但切换xml布局不应该导致此错误。第294行是mEnergy = (ImageButton) view.findViewById(R.id.btnEnergy);
07-08 09:39:22.527: E/AndroidRuntime(22345): FATAL EXCEPTION: main
07-08 09:39:22.527: E/AndroidRuntime(22345): java.lang.ClassCastException: android.widget.ToggleButton cannot be cast to android.widget.ImageButton
07-08 09:39:22.527: E/AndroidRuntime(22345): at com.projectcaruso.naturalfamilyplaning.ChartingFragment.onCreateView(ChartingFragment.java:294)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.os.Handler.handleCallback(Handler.java:615)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.os.Handler.dispatchMessage(Handler.java:92)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.os.Looper.loop(Looper.java:137)
07-08 09:39:22.527: E/AndroidRuntime(22345): at android.app.ActivityThread.main(ActivityThread.java:4950)
07-08 09:39:22.527: E/AndroidRuntime(22345): at java.lang.reflect.Method.invokeNative(Native Method)
07-08 09:39:22.527: E/AndroidRuntime(22345): at java.lang.reflect.Method.invoke(Method.java:511)
07-08 09:39:22.527: E/AndroidRuntime(22345): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
07-08 09:39:22.527: E/AndroidRuntime(22345): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
07-08 09:39:22.527: E/AndroidRuntime(22345): at dalvik.system.NativeStart.main(Native Method)
现在这里有一个在我使用图形布局移动时无效的方法:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="@+id/charting"
android:layout_width="wrap_content"
android:layout_height="550dp" >
<TextView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:clickable="false"
android:gravity="center"
android:text="@string/charting_header"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageButton
android:id="@+id/pickdate"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBottom="@+id/dateselected"
android:layout_alignParentRight="true"
android:text="@string/date"
android:src="@drawable/calendar"
android:scaleType="centerInside"
android:background="#00000000" />
<EditText
android:id="@+id/dateselected"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/header"
android:layout_toLeftOf="@+id/pickdate"
android:clickable="false"
android:cursorVisible="false"
android:ems="10"
android:gravity="center"
android:inputType="date" />
<TextView
android:id="@+id/stamp"
android:layout_width="192dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView1"
android:layout_marginTop="14dp"
android:clickable="false"
android:ems="10"
android:textAppearance="?android:attr/textAppearanceMedium"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/stamp" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/pickdate"
android:layout_marginTop="17dp"
android:gravity="center"
android:text="@string/Methods"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="@+id/temp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/stamp"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:textAppearance="?android:attr/textAppearanceMedium"
android:focusableInTouchMode="false"
android:text="@string/temp"
android:paddingTop="10sp" />
<ImageButton
android:id="@+id/btnstamp"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:contentDescription="@string/stampval"
android:src="@drawable/observation"
android:background="#00000000" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/EditText03"
android:layout_marginTop="14dp"
android:gravity="center"
android:text="@string/Body"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="@+id/chkFertile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tempvalue"
android:layout_below="@+id/tempvalue"
android:layout_alignBaseline="@+id/fertile"/>
<ToggleButton
android:id="@+id/btnIntercorse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnMood"
android:layout_alignParentLeft="true"
android:text="@string/Intercourse"
android:background="@drawable/intercorse_button"
android:paddingTop="25sp"
android:textOff=""
android:textOn="" />
<ToggleButton
android:id="@+id/btnHeadache"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/btnMood"
android:layout_alignRight="@+id/measurecervix"
android:text="@string/headache"
android:background="@drawable/headache_button"
android:paddingTop="25sp"
android:textOff=""
android:textOn="" />
<ImageButton
android:id="@+id/btnEnergy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnMood"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="@drawable/energy"
android:text="@string/energy"
android:paddingTop="25sp" />
<ImageButton
android:id="@+id/btnMood"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="27dp"
android:background="#00000000"
android:padding="7sp"
android:paddingTop="25sp"
android:src="@drawable/mood"
android:text="@string/mood" />
<ToggleButton
android:id="@+id/btnPeriod"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/btnEnergy"
android:layout_alignLeft="@+id/btnIntercorse"
android:background="@drawable/period_button"
android:text="@string/period"
android:textOff=""
android:textOn="" />
<ToggleButton
android:id="@+id/btnTest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btnPeriod"
android:layout_alignBottom="@+id/btnPeriod"
android:layout_alignRight="@+id/measurecervix"
android:background="@drawable/pregnancy_button"
android:text="@string/pregnancytest"
android:textOff=""
android:textOn=""
android:paddingRight="50dp" />
<TextView
android:id="@+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/btnPeriod"
android:layout_alignRight="@+id/btnPeriod"
android:layout_below="@+id/btnPeriod"
android:gravity="center_horizontal"
android:text="@string/period"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView7"
android:layout_alignBottom="@+id/textView7"
android:layout_alignLeft="@+id/btnEnergy"
android:layout_alignRight="@+id/btnEnergy"
android:gravity="center_horizontal"
android:text="@string/energy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/btnIntercorse"
android:layout_alignLeft="@+id/btnIntercorse"
android:gravity="center_horizontal"
android:text="@string/Intercourse"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView4"
android:layout_alignBottom="@+id/textView4"
android:layout_alignLeft="@+id/btnHeadache"
android:gravity="center_horizontal"
android:text="@string/headache"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/textView8"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/textView7"
android:layout_alignBottom="@+id/textView7"
android:layout_alignParentRight="true"
android:gravity="center_horizontal"
android:text="@string/pregnancytest"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/fertile"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/temp"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/fertile"
android:paddingTop="10sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/EditText03"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/fertile"
android:clickable="false"
android:ems="10"
android:focusable="false"
android:focusableInTouchMode="false"
android:text="@string/cervix"
android:paddingTop="10sp"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="@+id/measurecervix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/EditText03"
android:layout_toRightOf="@+id/EditText03"
android:layout_alignLeft="@+id/tempvalue"
android:ems="10"
android:hint="@string/tempval"
android:inputType="number" >
</EditText>
<EditText
android:id="@+id/tempvalue"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/temp"
android:layout_alignBottom="@+id/temp"
android:layout_alignRight="@+id/btnstamp"
android:ems="10"
android:hint="@string/tempval"
android:inputType="number" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/textView4"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:text="@string/mood"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
<!-- Notes Field -->
<EditText
android:id="@+id/chartingnote"
android:layout_width="match_parent"
android:layout_height="150dip"
android:background="#C4C6CF"
android:ems="20"
android:gravity="top|center_horizontal"
android:hint="@string/hintNote"
android:inputType="textMultiLine" />
<!-- Update Button -->
<Button
android:id="@+id/chartingupdate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/charting_update" />
</LinearLayout>
</ScrollView>
爪哇:
public class ChartingFragment extends Fragment implements OnDateSetListener, OnClickListener {
SharedPreferences mPreferences;
Boolean symptothermal, mucus_stamps, fertile_infertile;
EditText sqlNotes, sqlDate, sqlTemperature, sqlmeasurecervix;
CheckBox sqlFertile;
int mood_selected, stamp_selected, energy_selected;
Button update;
ToggleButton mIntercorse, mPeriod, mHeadache, mTest;
ImageButton mMood, stamp, date, mEnergy;
private static final int ID_HAPPY = 1;
private static final int ID_SAD = 2;
private static final int ID_MAD = 3;
private static final int ID_CRY = 4;
private static final int ID_ARROUSED = 5;
private static final int ID_SLEEPY = 6;
private static final int _ID1 = 1;
private static final int _ID2 = 2;
private static final int _ID3 = 3;
private static final int _ID4 = 4;
private static final int _ID5 = 5;
private static final int _ID6 = 6;
private static final int _ID7 = 7;
private static final int _ID8 = 8;
private static final int _ID9 = 9;
private static final int _ID10 = 10;
private static final int _ID11 = 11;
private static final int _ID12 = 12;
private static final int _ID13 = 13;
private static final int _ID14 = 14;
private static final int _ID15 = 15;
URL SERVICE_URL;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
init();
}
private void init() {
mPreferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
symptothermal = mPreferences.getBoolean("symptothermal", true);
mucus_stamps = mPreferences.getBoolean("mucus_stamps", true);
fertile_infertile = mPreferences.getBoolean("fertile_infertil", true);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view;
view = setcontrolvisability(inflater);
update = (Button) view.findViewById(R.id.chartingupdate);
update.setOnClickListener(this);
date = (ImageButton) view.findViewById(R.id.pickdate);
date.setOnClickListener(this);
date.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
android.support.v4.app.FragmentTransaction ft = getFragmentManager().beginTransaction();
DialogFragment newFragment = new DatePickerDialogFragment(ChartingFragment.this);
newFragment.show(ft, "date_picker_dialog");
}
});
final QuickAction mQuickAction = new QuickAction(getActivity());
ActionItem happyItem = new ActionItem(ID_HAPPY, "Happy", getResources().getDrawable(R.drawable.emo_happy));
ActionItem sadtItem = new ActionItem(ID_SAD, "Sad", getResources().getDrawable(R.drawable.emo_sad));
ActionItem madItem = new ActionItem(ID_MAD, "Mad", getResources().getDrawable(R.drawable.emo_mad));
ActionItem cryItem = new ActionItem(ID_CRY, "Cry", getResources().getDrawable(R.drawable.emo_cry));
ActionItem arrousedItem = new ActionItem(ID_ARROUSED, "Arroused", getResources().getDrawable(R.drawable.emo_arroused));
ActionItem sleepyItem = new ActionItem(ID_SLEEPY, "Sleepy", getResources().getDrawable(R.drawable.emo_sleepy));
mQuickAction.addActionItem(happyItem);
mQuickAction.addActionItem(sadtItem);
mQuickAction.addActionItem(madItem);
mQuickAction.addActionItem(cryItem);
mQuickAction.addActionItem(arrousedItem);
mQuickAction.addActionItem(sleepyItem);
mMood = (ImageButton) view.findViewById(R.id.btnMood);
mMood.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQuickAction.show(v);
}
});
//setup the action item click listener
mQuickAction.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction quickAction, int pos, int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos);
mood_selected = actionItem.getActionId();
switch(mood_selected) {
case 1:
mMood.setImageResource(R.drawable.emo_happy);
break;
case 2:
mMood.setImageResource(R.drawable.emo_sad);
break;
case 3:
mMood.setImageResource(R.drawable.emo_mad);
break;
case 4:
mMood.setImageResource(R.drawable.emo_cry);
break;
case 5:
mMood.setImageResource(R.drawable.emo_arroused);
break;
case 6:
mMood.setImageResource(R.drawable.emo_sleepy);
break;
}
}
});
final QuickAction mQuickAction2 = new QuickAction(getActivity());
ActionItem observation1 = new ActionItem(_ID1, "", getResources().getDrawable(R.drawable.observation_1));
ActionItem observation2 = new ActionItem(_ID2, "", getResources().getDrawable(R.drawable.observation_2));
ActionItem observation3 = new ActionItem(_ID3, "", getResources().getDrawable(R.drawable.observation_3));
ActionItem observation4 = new ActionItem(_ID4, "", getResources().getDrawable(R.drawable.observation_4));
ActionItem observation5 = new ActionItem(_ID5, "", getResources().getDrawable(R.drawable.observation_5));
ActionItem observation6 = new ActionItem(_ID6, "", getResources().getDrawable(R.drawable.observation_6));
ActionItem observation7 = new ActionItem(_ID7, "", getResources().getDrawable(R.drawable.observation_7));
ActionItem observation8 = new ActionItem(_ID8, "", getResources().getDrawable(R.drawable.observation_8));
ActionItem observation9 = new ActionItem(_ID9, "", getResources().getDrawable(R.drawable.observation_9));
ActionItem observation10 = new ActionItem(_ID10, "", getResources().getDrawable(R.drawable.observation_10));
ActionItem observation11 = new ActionItem(_ID11, "", getResources().getDrawable(R.drawable.observation_11));
ActionItem observation12 = new ActionItem(_ID12, "", getResources().getDrawable(R.drawable.observation_12));
ActionItem observation13 = new ActionItem(_ID13, "", getResources().getDrawable(R.drawable.observation_13));
ActionItem observation14 = new ActionItem(_ID14, "", getResources().getDrawable(R.drawable.observation_14));
ActionItem observation15 = new ActionItem(_ID15, "", getResources().getDrawable(R.drawable.observation_15));
mQuickAction2.addActionItem(observation1);
mQuickAction2.addActionItem(observation2);
mQuickAction2.addActionItem(observation3);
mQuickAction2.addActionItem(observation4);
mQuickAction2.addActionItem(observation5);
mQuickAction2.addActionItem(observation6);
mQuickAction2.addActionItem(observation7);
mQuickAction2.addActionItem(observation8);
mQuickAction2.addActionItem(observation9);
mQuickAction2.addActionItem(observation10);
mQuickAction2.addActionItem(observation11);
mQuickAction2.addActionItem(observation12);
mQuickAction2.addActionItem(observation13);
mQuickAction2.addActionItem(observation14);
mQuickAction2.addActionItem(observation15);
stamp = (ImageButton) view.findViewById(R.id.btnstamp);
stamp.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQuickAction2.show(v);
}
});
//setup the action item click listener
mQuickAction2.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction quickAction, int pos, int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos);
stamp_selected = actionItem.getActionId();
switch(stamp_selected) {
case 1:
stamp.setImageResource(R.drawable.observation_1);
break;
case 2:
stamp.setImageResource(R.drawable.observation_2);
break;
case 3:
stamp.setImageResource(R.drawable.observation_3);
break;
case 4:
stamp.setImageResource(R.drawable.observation_4);
break;
case 5:
stamp.setImageResource(R.drawable.observation_5);
break;
case 6:
stamp.setImageResource(R.drawable.observation_6);
break;
case 7:
stamp.setImageResource(R.drawable.observation_7);
break;
case 8:
stamp.setImageResource(R.drawable.observation_8);
break;
case 9:
stamp.setImageResource(R.drawable.observation_9);
break;
case 10:
stamp.setImageResource(R.drawable.observation_10);
break;
case 11:
stamp.setImageResource(R.drawable.observation_11);
break;
case 12:
stamp.setImageResource(R.drawable.observation_12);
break;
case 13:
stamp.setImageResource(R.drawable.observation_13);
break;
case 14:
stamp.setImageResource(R.drawable.observation_14);
break;
case 15:
stamp.setImageResource(R.drawable.observation_15);
break;
}
}
});
final QuickAction mQuickAction3 = new QuickAction(getActivity());
ActionItem energy1 = new ActionItem(_ID1, "", getResources().getDrawable(R.drawable.energy_1));
ActionItem energy2 = new ActionItem(_ID2, "", getResources().getDrawable(R.drawable.energy_2));
ActionItem energy3 = new ActionItem(_ID3, "", getResources().getDrawable(R.drawable.energy_3));
ActionItem energy4 = new ActionItem(_ID4, "", getResources().getDrawable(R.drawable.energy_4));
ActionItem energy5 = new ActionItem(_ID5, "", getResources().getDrawable(R.drawable.energy_5));
mQuickAction3.addActionItem(energy1);
mQuickAction3.addActionItem(energy2);
mQuickAction3.addActionItem(energy3);
mQuickAction3.addActionItem(energy4);
mQuickAction3.addActionItem(energy5);
mEnergy = (ImageButton) view.findViewById(R.id.btnEnergy);
mEnergy.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mQuickAction3.show(v);
}
});
//setup the action item click listener
mQuickAction3.setOnActionItemClickListener(new QuickAction.OnActionItemClickListener() {
@Override
public void onItemClick(QuickAction quickAction, int pos, int actionId) {
ActionItem actionItem = quickAction.getActionItem(pos);
energy_selected = actionItem.getActionId();
switch(energy_selected) {
case 1:
mEnergy.setImageResource(R.drawable.energy_1);
break;
case 2:
mEnergy.setImageResource(R.drawable.energy_2);
break;
case 3:
mEnergy.setImageResource(R.drawable.energy_3);
break;
case 4:
mEnergy.setImageResource(R.drawable.energy_4);
break;
case 5:
mEnergy.setImageResource(R.drawable.energy_5);
break;
}
}
});
mIntercorse = (ToggleButton) view.findViewById(R.id.btnIntercorse);
mPeriod = (ToggleButton) view.findViewById(R.id.btnPeriod);
mHeadache = (ToggleButton) view.findViewById(R.id.btnHeadache);
mTest = (ToggleButton) view.findViewById(R.id.btnTest);
return view;
}
答案 0 :(得分:10)
清理您的项目。在Eclipse中,
项目 - &gt;清理......选择项目
有时Eclipse不会立即获取xml更改,因此它会抛出这些类转换异常。简单地清理和重建项目通常会处理这些错误。这是一个快速简单的事情,在拉出你所有的头发之前试图弄清楚为什么你会得到这样的例外。
另外,我建议尽可能使用xml选项卡。恕我直言,它更灵活,更容易得到你想要的结果。在手动编写xml代码时仍然会遇到此问题,但我认为在大多数情况下以这种方式生成所需的布局会更容易。
答案 1 :(得分:0)
您的布局xml文件错误。
不是android:layout_alignBottom="@+id/btnEnergy"
,android:layout_alignBottom="@id/btnEnergy"
不包括“+”
@ + id表示添加新ID,这会导致错误。