我想根据点击的微调器更改字体大小。每次单击下拉列表微调器时,它都会将我带到 java.lang.NullPointerException。干得好:我的Android应用活动public class MyAndroidAppActivity extends AppCompatActivity {private Spinner spinner1, spinnerLatin;@Overridepublic 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());}@Overridepublic boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case android.R.id.home: this.finish(); return true; } return super.onOptionsItemSelected(item);} }
1 回答
鸿蒙传说
TA贡献1865条经验 获得超7个赞
在您的听众中,这些台词几乎肯定是错误的:
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);
添加回答
举报
0/150
提交
取消