-1

私はアンドロイド開発が初めてです。eclipse を使用して 2 つの数値を加算する単純なアプリを作成しました。うまくいきました。しかし、xml ファイルを直接編集せずにグラフィカル レイアウトを使用して、ボタンと textViews の位置を変更した後にアプリを実行すると、エミュレーターに「残念ながらアプリが停止しました」というエラーが表示されます。これは私に数回起こります。誰かがそれがなぜなのか理解できますか?

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;
    }
}

}
4

5 に答える 5

0

これはシンプルなものです!私は一度これに出くわしたことがあります。最初は非常に困惑してイライラするように見えますが、必要な唯一のことは、いくつかの (またはすべての) コンポーネントの位置とレイアウトが変更/変更されたという事実を Eclipse が認識できるようにすることです。これは、ワークスペースをゼロから構築することで達成する必要があります。そのために必要なことは、Eclipse を閉じて再起動することだけです。そうするだろう。

于 2015-03-30T12:09:47.563 に答える
0
android.widget.RadioGroup cannot be cast to android.widget.Button
  • XML に RadioGroup 項目が含まれています
  • あなたの Fragment クラスは findViewById でその項目を取得しており、それを Button としてキャストしようとしています。たとえば、次のようにします。

    Button btn = (Button) getView().findViewById(r.id.myRadioButton);

XML を変更して要素にする、フラグメントを変更して RadioGroup 変数を取得する必要があります。

RadioGroup grp = (RadioGroup) getView().findViewById(r.id.myRadioButton);
于 2014-06-10T16:58:42.160 に答える