gpt4 book ai didi

java - 如何根据点击的微调器更改字体大小

转载 作者:行者123 更新时间:2023-11-29 23:30:58 25 4
gpt4 key购买 nike

在向您解释之前,我告诉过您,我一直在 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
} }

ma​​in.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>

但是,问题就出在这里: Error located here

这是错误的结果:

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 天了。

最佳答案

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

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

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

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

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

关于java - 如何根据点击的微调器更改字体大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52673438/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com