我是Android开发的新手。我创建了一个简单的应用程序,使用eclipse添加两个数字。它工作正常。但是当我在更改按钮和textViews的位置后运行应用程序时,使用图形布局而不直接编辑xml文件,"不幸的是应用程序停止了"模拟器中显示错误。这种情况几次发生在我身上。请有人知道为什么会这样吗?
logcat的
06-10 16:51:36.690: E/Trace(1343): error opening trace file: No such file or directory (2)
06-10 16:51:36.886: D/AndroidRuntime(1343): Shutting down VM
06-10 16:51:36.886: W/dalvikvm(1343): threadid=1: thread exiting with uncaught exception (group=0xa624b288)
06-10 16:51:36.890: E/AndroidRuntime(1343): FATAL EXCEPTION: main
06-10 16:51:36.890: E/AndroidRuntime(1343): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.a/com.example.a.MainActivity}: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.Button
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.os.Handler.dispatchMessage(Handler.java:99)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.os.Looper.loop(Looper.java:137)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-10 16:51:36.890: E/AndroidRuntime(1343): at java.lang.reflect.Method.invokeNative(Native Method)
06-10 16:51:36.890: E/AndroidRuntime(1343): at java.lang.reflect.Method.invoke(Method.java:511)
06-10 16:51:36.890: E/AndroidRuntime(1343): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-10 16:51:36.890: E/AndroidRuntime(1343): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-10 16:51:36.890: E/AndroidRuntime(1343): at dalvik.system.NativeStart.main(Native Method)
06-10 16:51:36.890: E/AndroidRuntime(1343): Caused by: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.Button
06-10 16:51:36.890: E/AndroidRuntime(1343): at com.example.a.MainActivity$PlaceholderFragment.onCreateView(MainActivity.java:75)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1163)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.Activity.performStart(Activity.java:5018)
06-10 16:51:36.890: E/AndroidRuntime(1343): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2032)
06-10 16:51:36.890: E/AndroidRuntime(1343): ... 11 more
fagment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="fill"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#EB7474"
tools:context="com.example.a.MainActivity$PlaceholderFragment" >
<EditText
android:id="@+id/n1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="23dp"
android:layout_marginTop="119dp"
android:ems="10"
android:background="#FFE9E9"
android:hint="num2" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/n2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/n1"
android:layout_alignBottom="@+id/n1"
android:layout_alignParentRight="true"
android:layout_marginRight="20dp"
android:ems="10"
android:background="#FFE9E9"
android:hint="num2" />
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup1"
android:layout_marginTop="99dp"
android:layout_toRightOf="@+id/n1"
android:text="Answer" />
<TextView
android:id="@+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/b1"
android:layout_centerHorizontal="true"
android:layout_marginTop="69dp"
android:background="#FFE9E9"
android:text="@string/hello_world" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/n1"
android:layout_centerHorizontal="true"
android:layout_marginTop="132dp"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/r0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="+" />
<RadioButton
android:id="@+id/r1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="-" />
<RadioButton
android:id="@+id/r2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="X" />
<RadioButton
android:id="@+id/r3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="/" />
</RadioGroup>
</RelativeLayout>
activity_main.xml中
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.a.MainActivity"
tools:ignore="MergeRootFrame" />
mainActivity.java
package com.example.a;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
Button A;
TextView B;
EditText n1;
EditText n2;
RadioGroup radioButtonGroup;
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
A = (Button) rootView.findViewById(R.id.b1);
B = (TextView) rootView.findViewById(R.id.tv1);
n1 = (EditText) rootView.findViewById(R.id.n1);
n2 = (EditText) rootView.findViewById(R.id.n2);
radioButtonGroup = (RadioGroup) rootView.findViewById(R.id.radioGroup1);
A.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
View radioButton = radioButtonGroup.findViewById(radioButtonID);
int idx = radioButtonGroup.indexOfChild(radioButton);
int num1 = Integer.parseInt(n1.getText().toString());
int num2 = Integer.parseInt(n2.getText().toString());
int total = 0;
switch (idx)
{
case 0:
total = num1+num2;
break;
case 1:
total = num1-num2;
break;
case 2:
total = num1*num2;
break;
case 3:
total = num1/num2;
break;
case -1:
break;
}
String ttl = String.valueOf(total);
B.setText(ttl);
}
});
return rootView;
}
}
}
答案 0 :(得分:1)
当我所做的一切都是在xml布局中交换视图时,我通常会遇到此错误。这是一个ecllipse错误(有时),因此您的资源不会与adt构建器同步。当我得到他的错误时,我做的第一件事就是关闭ecllipse,从任务管理器(windows)结束处理adb.exe,然后再次启动ecllipse。如果这对Android工具没有帮助 - &gt;修复项目属性和项目 - &gt;清洁。
答案 1 :(得分:0)
android.widget.RadioGroup cannot be cast to android.widget.Button
您的Fragment类正在使用findViewById获取该项,并尝试将其强制转换为Button。比如那样:
Button btn = (Button) getView().findViewById(r.id.myRadioButton);
您需要更改XML以使元素或更改您的片段以获取RadioGroup变量:
RadioGroup grp = (RadioGroup) getView().findViewById(r.id.myRadioButton);
答案 2 :(得分:0)
问题在于您尝试将RadioGroup
投射到Button
,而您无法做到。这发生在MainActivity
。
如果找不到问题,请发布MainActivity
的代码。
答案 3 :(得分:0)
我怀疑,我没有看到任何无效的施法。我的猜测是您收到此错误,因为已部署的内容与您在IDE中构建的内容之间存在脱节。 R.java可能陈旧。您需要清理(删除bin,gen,lib,obj目录)并重建项目。
这是一个类似的帖子,同样的事情发生了:Android ClassCastException for RadioGroup into RadioButton
通过关闭模拟器并重新启动IDE来解决他们的问题。
答案 4 :(得分:0)
这很简单! 我曾经遇到过这个。这看起来令人费解并且非常困难。起初令人沮丧,但你唯一需要做的就是帮助eclipse了解一些(或所有)组件的位置和布局已经被修改/改变的事实。 而这应该是通过从头开始构建工作空间来实现的。 为此,您需要做的就是关闭并重新启动eclipse。 那就是。