添加Spinner给出classcastexception的错误

时间:2013-09-15 12:28:59

标签: java android spinner

将微调器控件添加到布局时出现错误。

我无法弄清楚为什么会这样。下面是代码和stacktrace可以帮助我吗?

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:id="@+id/amritsarimageid"
    android:layout_width="fill_parent"
    android:layout_height="match_parent"
    android:background="@drawable/layout_background"
    android:orientation="vertical" >

    <TableLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/radio_border" >

        <TableRow>

            <ImageView
                android:id="@+id/amritsarimageid"
                android:layout_width="100dp"
                android:layout_height="40dp"
                android:scaleType="fitXY"
                android:src="@drawable/amritsar" />

            <TableLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <TableRow>

                    <TextView
                        android:id="@+id/heading"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center"
                        android:gravity="center"
                        android:tag="@string/background_splash"
                        android:text="HukumNama Darbar Sahib, Amritsar"
                        android:textAppearance="?android:attr/textAppearanceLarge"
                        android:textColor="@android:color/white"
                        android:textSize="12sp"
                        android:textStyle="bold" />
                </TableRow>

                <TableRow>

                    <TextView
                        android:id="@+id/lastupdated"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content"
                        android:text="Last updated on:"
                        android:textAppearance="?android:attr/textAppearanceSmall"
                        android:textColor="@android:color/white"
                        android:textStyle="italic" />
                </TableRow>
            </TableLayout>
        </TableRow>
    </TableLayout>

    <TextView
        android:id="@+id/link"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <RadioGroup
        android:id="@+id/radioSelectOption"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:dividerPadding="0dp"
        android:orientation="vertical" >

        <RadioButton
            android:id="@+id/amrRadioReadPunjabi"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:checked="true"
            android:onClick="onRadioButtonClicked"
            android:text="@string/lang1"
            android:textColor="@android:color/black"
            android:textSize="12sp" />

        <RadioButton
            android:id="@+id/amrRadioTransliteration"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:onClick="onRadioButtonClicked"
            android:text="Punjabi Transliteration"
            android:textColor="@android:color/black"
            android:textSize="12sp" />

        <com.google.ads.AdView
            android:id="@+id/adView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            ads:adSize="BANNER"
            ads:adUnitId="a15131e8f06efd9"
            ads:loadAdOnCreate="true" />

        <RadioButton
            android:id="@+id/amrRadioPunjabiMeaning"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:onClick="onRadioButtonClicked"
            android:text="Punjabi Vyakhya(Meaning)"
            android:textColor="@android:color/black"
            android:textSize="12sp" />

        <!--
                     <RadioButton
                    android:id="@+id/radioHindi"
                    android:layout_width="wrap_content"
                    android:layout_height="20dp"
                    android:onClick="onRadioButtonClicked"
                    android:text="Hindi Mein Padhiye"
                    android:textColor="@android:color/black"
                    android:textSize="12sp" />
        -->

        <RadioButton
            android:id="@+id/amrRadioEnglishTranslation"
            android:layout_width="wrap_content"
            android:layout_height="20dp"
            android:onClick="onRadioButtonClicked"
            android:text="@string/eng_translate"
            android:textColor="@android:color/black"
            android:textSize="12sp" />
    </RadioGroup>

    <Spinner
        android:id="@+id/amrSpinnerFontSize"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:entries="@array/font_sizes"
        android:prompt="@string/font_size_prompt" />

    <ScrollView
        android:id="@+id/scroll1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="20dp"
        android:background="@drawable/white_background" >

        <TextView
            android:id="@+id/amrTxtContent"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_marginTop="10dp"
            android:text="Loading Hukumnama Details"
            android:textAppearance="?android:attr/textAppearanceMedium"
            android:textColor="@android:color/black"
            android:textIsSelectable="true" />
    </ScrollView>

</LinearLayout>

活动代码:

public class AmritsarActivity extends Activity {

    private static final String PLAY_URL = "<a href='https://play.google.com/store/apps/details?id=com.gurbani.gurbanidaily'>Rate us on Google Play</a>";
    static DataLoaderAmritsar urlreaderinstance = null;
    static TextView textView = null;

    String PATH = null;
    String currdate = "";

    HukumNamaDetails hdetails = new HukumNamaDetails();
    AmritsarActivity amractivity = this;

    /*
     * (non-Javadoc)
     * 
     * @see android.app.Activity#onCreate(android.os.Bundle)
     */
    public void onCreate(Bundle savedInstanceState) {

        System.out.println("In Amritsar activity");

        super.onCreate(savedInstanceState);
        setContentView(R.layout.amritsar);

        String htmllinktext = PLAY_URL;
        TextView linktxt = (TextView) findViewById(R.id.link);
        linktxt.setText(Html.fromHtml(htmllinktext));
        linktxt.setMovementMethod(LinkMovementMethod.getInstance());

        textView = (TextView) findViewById(R.id.amrTxtContent);

        TextView lastupdatedtxt = (TextView) findViewById(R.id.lastupdated);

        Intent i = getIntent();
        hdetails = (HukumNamaDetails) i.getParcelableExtra("hdetails");
        System.out.println("hdetails" + hdetails);

        // Look up the AdView as a resource and load a request.
        AdView adView = (AdView) this.findViewById(R.id.adView);
        AdRequest adrequest = new AdRequest();
        adView.loadAd(adrequest);

        try {

            currdate = Utils.getCurrentDate();
            System.out.println("Loading it in UI");
            textView.setText(hdetails.getHukumnama_punjabi().replace("�", ":"));
            textView.setTypeface(Typeface.createFromAsset(getAssets(),
                    "fonts/punjabi.ttf"));
            lastupdatedtxt.setText(lastupdatedtxt.getText() + " "
                    + hdetails.getHdate());
            System.out.println("UI loaded--Gen pdf");
        } catch (Exception e) {

            System.out.println("Exception" + e);
            e.printStackTrace();
        }
    }

    /**
     * @param view
     */
    public void onRadioButtonClicked(View view) {

        // Check which radio button was clicked
        try {
            switch (view.getId()) {

            case R.id.amrRadioReadPunjabi:
                if ((hdetails.getHukumnama_punjabi() != null)
                        || (!hdetails.getHukumnama_punjabi().equals(""))) {

                    System.out.println(hdetails.getHukumnama_punjabi() + "text"
                            + textView.getId());

                    textView.setText(hdetails.getHukumnama_punjabi().replace(
                            "�", ":"));
                    textView.setVisibility(View.VISIBLE);
                    textView.setTypeface(Typeface.createFromAsset(getAssets(),
                            "fonts/punjabi.ttf"));
                    textView.setTextSize(15);
                } else {
                    textView.setText("Unable to fetch Hukamnama at this time, Please try again later");
                    textView.setVisibility(View.VISIBLE);
                }
                break;

            case R.id.amrRadioPunjabiMeaning:
                if ((hdetails.getPunjabi_vyakhya() != null)
                        || (!hdetails.getBan_punjabi_vyakhya().equals(""))) {

                    textView.setText(hdetails.getPunjabi_vyakhya().replace("�",
                            ":"));
                    textView.setVisibility(View.VISIBLE);
                    textView.setTypeface(Typeface.createFromAsset(getAssets(),
                            "fonts/punjabi.ttf"));
                } else {
                    textView.setText("Unable to fetch Hukamnama at this time, Please try again later");
                    textView.setVisibility(View.VISIBLE);
                }
                break;

            case R.id.amrRadioEnglishTranslation:

                if ((hdetails.getEnglish_translation() != null)
                        || (!hdetails.getEnglish_translation().equals(""))) {

                    System.out.println(hdetails.getEnglish_translation()
                            + "text" + textView.getId());

                    textView.setText(hdetails.getEnglish_translation().replace(
                            "�", ":"));

                    textView.setTypeface(Typeface.DEFAULT);
                    textView.setVisibility(View.VISIBLE);
                } else {
                    textView.setText("Unable to fetch Hukamnama at this time, Please try again later");
                    textView.setVisibility(View.VISIBLE);
                }
                break;

            /*
             * case R.id.radioHindi :
             * System.out.println("getHindi_translation--" +
             * hdetails.getHindi_translation());
             * textView.setText(hdetails.getHindi_translation
             * ().replace("�",":")); textView.setVisibility(View.VISIBLE);
             * textView.setTypeface(Typeface.createFromAsset(getAssets(),
             * "fonts/hindi.ttf")); break;
             */

            case R.id.amrRadioTransliteration:

                if ((hdetails.getAmrEnglish_transliteration() != null)
                        || (!hdetails.getAmrEnglish_transliteration()
                                .equals(""))) {

                    System.out.println("Transliteration--"
                            + hdetails.getAmrEnglish_transliteration());

                    textView.setText(hdetails.getAmrEnglish_transliteration()
                            .replace("�", ":"));

                    textView.setTypeface(Typeface.DEFAULT);
                    textView.setVisibility(View.VISIBLE);
                } else {
                    textView.setText("Unable to fetch Hukamnama at this time, Please try again later");
                    textView.setVisibility(View.VISIBLE);
                }
                break;
            }
        } catch (Exception e) {
            e.printStackTrace();
            System.out.println("Exception" + e);
        }
    }}

错误堆栈跟踪:

09-15 17:51:16.278: E/AndroidRuntime(31898): FATAL EXCEPTION: main
09-15 17:51:16.278: E/AndroidRuntime(31898): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gurbani.gurbanidaily/com.gurbani.gurbanidaily.MainActivity}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gurbani.gurbanidaily/com.gurbani.gurbanidaily.AmritsarActivity}: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.TextView
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2090)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.access$600(ActivityThread.java:136)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.os.Handler.dispatchMessage(Handler.java:99)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.os.Looper.loop(Looper.java:137)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.main(ActivityThread.java:4802)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at java.lang.reflect.Method.invokeNative(Native Method)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at java.lang.reflect.Method.invoke(Method.java:511)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:813)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:580)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at dalvik.system.NativeStart.main(Native Method)
09-15 17:51:16.278: E/AndroidRuntime(31898): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gurbani.gurbanidaily/com.gurbani.gurbanidaily.AmritsarActivity}: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.TextView
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2065)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.startActivityNow(ActivityThread.java:1906)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:135)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:347)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:694)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.widget.TabHost.setCurrentTab(TabHost.java:358)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.widget.TabHost.addTab(TabHost.java:236)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at com.gurbani.gurbanidaily.MainActivity.onCreate(MainActivity.java:60)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.Activity.performCreate(Activity.java:5013)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
09-15 17:51:16.278: E/AndroidRuntime(31898):    ... 11 more
09-15 17:51:16.278: E/AndroidRuntime(31898): Caused by: java.lang.ClassCastException: android.widget.RadioGroup cannot be cast to android.widget.TextView
09-15 17:51:16.278: E/AndroidRuntime(31898):    at com.gurbani.gurbanidaily.AmritsarActivity.onCreate(AmritsarActivity.java:47)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.Activity.performCreate(Activity.java:5013)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
09-15 17:51:16.278: E/AndroidRuntime(31898):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2029)
09-15 17:51:16.278: E/AndroidRuntime(31898):    ... 21 more

1 个答案:

答案 0 :(得分:0)

请不要将TextView textView用作静态变量。通过使其成为静态变量,它没有被正确初始化,CompilertextView和它的父视图(即RadioGroup)之间变得混乱。因此,它提供了一个运行时ClassCastException 我希望这会有所帮助。