首页 > 解决方案 > 如何根据点击的微调器更改字体大小

问题描述

在向您解释之前,我告诉您,我一直在搜索 stackoverflow 和许多网站中的所有问题,所以不要将其标记为重复或任何负面行为。我已经被困在这里超过4天了。

我想根据点击的微调器更改字体大小。每次我单击下拉列表微调器时,都会出现 java.lang.NullPointerException。干得好:

MyAndroidAppActivity

public class MyAndroidAppActivity extends AppCompatActivity {

private Spinner spinner1, spinnerLatin;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    // toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    //this line shows back button
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    //Display data
    Spinner spinnerBackgroundChange = (Spinner)findViewById(R.id.spinner1);
    ArrayAdapter<CharSequence> spinnerArrayAdapter = ArrayAdapter.createFromResource(this, R.array.country_arrays, android.R.layout.simple_spinner_item);
    spinnerArrayAdapter.setDropDownViewResource(R.layout.textview_with_background);
    spinnerBackgroundChange.setAdapter(spinnerArrayAdapter);

    addListenerOnSpinnerItemSelection();
    addListenerOnSpinner2ItemSelection();
}

public void addListenerOnSpinnerItemSelection() {
    spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new SelectedListener());
}

public void addListenerOnSpinner2ItemSelection() {
    spinnerLatin = (Spinner) findViewById(R.id.spinnerLatin);
    spinnerLatin.setOnItemSelectedListener(new SelectedLatin());
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
        case android.R.id.home:
            this.finish();
            return true;
    }

    return super.onOptionsItemSelected(item);
} }

选定的侦听器

public class SelectedListener implements OnItemSelectedListener {

public void onItemSelected (AdapterView <?> parent, View view, int pos, long id){

    ((TextView) view).setTextColor(Color.RED);
    switch (pos) {
        case 0:
            TextView dgs = (TextView) view.findViewById(R.id.sizedoa);
            dgs.setTextSize(30);
            break;
        case 1:
            TextView dgf = (TextView) view.findViewById(R.id.fontLatin);
            dgf.setTextSize(30);
            break;
        default:
            //Default image
            //image.setImageResource(R.drawable.item2);
            break;

    }

}

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

主要的.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reldoa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:background="@android:color/white"
    android:orientation="vertical">

            <TextView
                android:id="@+id/sizedoa"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="10dp"
                android:paddingLeft="15dp"
                android:paddingTop="5dp"
                android:text="Ukuran Font"
                android:textColor="#222222"
                android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true" />

</RelativeLayout>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/reldoa"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="3dp"
    android:background="@android:color/white"
    android:orientation="vertical">

    <TextView
        android:id="@+id/sizelatin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:paddingBottom="10dp"
        android:paddingLeft="15dp"
        android:paddingTop="5dp"
        android:text="Jenis Font"
        android:textColor="#222222"
        android:textSize="18sp" />

    <Spinner
        android:id="@+id/spinner2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:entries="@array/type_arrays"
        android:prompt="@string/type_font"/>

</RelativeLayout>

        <!-- Font latin -->

        <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/relLatin"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="35dp"
            android:background="@android:color/white"
            android:orientation="vertical">

            <TextView
                android:id="@+id/fontLatin"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:paddingBottom="15dp"
                android:paddingLeft="15dp"
                android:paddingTop="15dp"
                android:text="Font Latin"
                android:textColor="#226169"
                android:textSize="20sp" />

        </RelativeLayout>

但是,问题出在此处: 错误位于此处

这是错误的结果:

10-05 00:20:08.848 5035-5035/? E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
    at id.or.tauhid.doadandzikir.SelectedListener.onItemSelected(SelectedListener.java:32)
    at android.widget.AdapterView.fireOnSelected(AdapterView.java:892)
    at android.widget.AdapterView.access$200(AdapterView.java:49)
    at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:860)
    at android.os.Handler.handleCallback(Handler.java:730)
    at android.os.Handler.dispatchMessage(Handler.java:92)
    at android.os.Looper.loop(Looper.java:137)
    at android.app.ActivityThread.main(ActivityThread.java:5103)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:525)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
    at dalvik.system.NativeStart.main(Native Method)

你能帮我解决这个问题吗?我被困在这里4天了。

标签: javaandroidspinner

解决方案


在您的听众中,这些行几乎肯定是错误的:

TextView dgs = (TextView) view.findViewById(R.id.sizedoa);
TextView dgf = (TextView) view.findViewById(R.id.fontLatin);

view传递给此方法的参数是被单击的微调器内部的视图,这意味着调用view.findViewById()只会查看微调器本身的内部(一部分)。大概这些视图在您的 Fragment 或 Activity 的布局中,而不是在微调器中。

如何解决这个问题将取决于您的应用程序是如何连接在一起的,但一种潜在的可能性是将AdapterView' 的上下文转换为Activity,然后在其中找到视图:

Activity activity = (Activity) parent.getContext();
TextView dgs = activity.findViewById(R.id.sizedoa);

推荐阅读