0

在我的应用程序中,我有一个EditText,一个Button和一个Spinner。单击按钮时,它必须显示微调器。显示Spinner各种字体以及选择字体时,必须将字体应用于文本中EditText。我已经编写了代码,但我得到了但我NullPointerException无法解决它。请尽快帮助我。

这是代码:

public class FlipCardActivity extends Activity implements OnItemSelectedListener {

ImageButton btn_from,btn_msg_tap,btn_font;
EditText edt_aroundMsg;
String edt_aroundMsgText = null;
Spinner spnr_font;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_flip_card);
    edt_aroundMsg = (EditText) findViewById(R.id.edt_aroundMsg);        
    spnr_font = (Spinner) findViewById(R.id.spnr_font);
    ArrayAdapter<CharSequence> adapter = new ArrayAdapter<CharSequence>(FlipCardActivity.this,android.R.layout.simple_spinner_item,R.array.fonts);
        adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spnr_font.setAdapter(adapter);
    spnr_font.setOnItemSelectedListener(FlipCardActivity.this);
    addListenerOnButton();
    loadSavedPreferences();
}

private void addListenerOnButton() {
    btn_font = (ImageButton) findViewById(R.id.btn_font);
    btn_font.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            spnr_font.performClick();
        }
    }); 
}


@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    spnr_font.setSelection(position);
    String str_font = (String) spnr_font.getSelectedItem();
    if ("Fondamento".equals(str_font)){
    Typeface tf = Typeface.createFromAsset(getAssets(),"fonts/Fondamento-        Regular.ttf");
        EditText edt_msg = (EditText) findViewById(R.id.edt_aroundMsg);
        edt_msg.setTypeface(tf);
    }


}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

在我的 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"    
tools:context=".FlipCardActivity" >

<RelativeLayout
    android:id="@+id/RLayout_flip_card"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/top_bar_view"
    android:background="@drawable/bg"
    android:orientation="horizontal" >

<EditText
        android:id="@+id/edt_aroundMsg"
        android:layout_width="300dp"
        android:layout_height="400dp"
        android:layout_alignLeft="@+id/edt_aroundRec"
        android:layout_alignRight="@+id/logo_view"
        android:layout_below="@+id/btn_rec"
        android:background="@drawable/border_message"
        android:gravity="top"
        android:ems="10"
        android:enabled="false"
        android:inputType="textMultiLine"
        android:textColor="#FFFFFF" />

</RelativeLayout>

  <Spinner
        android:id="@+id/spnr_font"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:prompt="@string/spinner_hint"
        android:visibility="invisible" />

</RelativeLayout>

这是我的 Logcat 错误文件:

11-12 18:09:52.531: E/AndroidRuntime(8367): FATAL EXCEPTION: main
11-12 18:09:52.531: E/AndroidRuntime(8367): java.lang.RuntimeException: Unable to start      activity ComponentInfo{com.sample.postcare2/com.sample.postcare2.FlipCardActivity}:    java.lang.ClassCastException: android.widget.TextView cannot be cast to   android.widget.Spinner
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at   android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.app.ActivityThread.access$600(ActivityThread.java:139)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.os.Handler.dispatchMessage(Handler.java:99)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.os.Looper.loop(Looper.java:154)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.app.ActivityThread.main(ActivityThread.java:4944)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at java.lang.reflect.Method.invokeNative(Native Method)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at java.lang.reflect.Method.invoke(Method.java:511)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at   dalvik.system.NativeStart.main(Native Method)
11-12 18:09:52.531: E/AndroidRuntime(8367): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.Spinner
11-12 18:09:52.531: E/AndroidRuntime(8367):     at com.sample.postcare2.FlipCardActivity.onCreate(FlipCardActivity.java:38)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.app.Activity.performCreate(Activity.java:4531)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
11-12 18:09:52.531: E/AndroidRuntime(8367):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
11-12 18:09:52.531: E/AndroidRuntime(8367):     ... 11 more

11-12 18:10:08.086: E/AndroidRuntime(8433): 致命异常: main 11-12 18:10:08.086: E/AndroidRuntime(8433): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com. sample.postcare2/com.sample.postcare2.FlipCardActivity}:java.lang.ClassCastException:android.widget.TextView 不能转换为 android.widget.Spinner

4

2 回答 2

0

你确定你在看activity_flip_card?崩溃表明spnr_font是 TextView 而不是 Spinner。我的猜测是这activity_flip_card不是正确的 xml 文件

于 2013-11-12T10:21:42.093 回答
0

为微调器项制作单独的 xml

<?xml version="1.0" encoding="utf-8"?>
 <TextView 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/tv_vehilcles_child_item_for_item_name"
    android:layout_width="fill_parent"
    android:text="Item Name"
    android:layout_marginLeft="@dimen/dim_50"
    android:drawableLeft="@drawable/smalldot"
    android:drawablePadding="@dimen/dim_10"
    android:layout_marginTop="@dimen/dim_10"
    android:textSize="23sp"

    android:layout_height="wrap_content"/>
于 2015-07-15T10:32:19.823 回答