在我向您解释之前,我告诉过您,我一直在搜索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);
} }
SelectedListener
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
} }
main.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天了。
答案 0 :(得分:1)
在您的听众中,这些行几乎可以肯定是错误的:
TextView dgs = (TextView) view.findViewById(R.id.sizedoa);
TextView dgf = (TextView) view.findViewById(R.id.fontLatin);
传递给此方法的view
参数是被单击的微调器 inside 的视图,这意味着调用view.findViewById()
只会在对象的内部(一部分)微调器本身。大概这些视图在您的Fragment或Activity的布局中,而不是在微调器中。
如何解决此问题将完全取决于您的应用程序如何连接在一起,但是一种潜在的可能性是将AdapterView
的上下文转换为Activity
,然后在其中查找视图:>
Activity activity = (Activity) parent.getContext();
TextView dgs = activity.findViewById(R.id.sizedoa);