Java 设置setOnItemSelectedListener时微调器错误
当我将微调器用于下拉列表或Java 设置setOnItemSelectedListener时微调器错误,java,android,xml,android-studio,Java,Android,Xml,Android Studio,当我将微调器用于下拉列表或setOnItemSelectedListener时发生错误: nav_login.xml: <Spinner android:layout_width="20dp" android:layout_height="12dp" android:id="@+id/spinner" android:layout_alignParentBottom="true" android:layout_alignParentRight="tr
setOnItemSelectedListener
时发生错误:
nav_login.xml:
<Spinner
android:layout_width="20dp"
android:layout_height="12dp"
android:id="@+id/spinner"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_alignWithParentIfMissing="false"
android:layout_marginRight="30dp"
android:layout_marginBottom="12dp"
android:entries="@array/items" />
问题就在这里。您将OnCreate中的contentView设置为R.layout.activity\u main,但微调器位于nav\u login.xml中,因此程序正在activity\u main中查找微调器,但找不到它 2种解决方案:
微调器=(微调器)findViewById(R.id.spinner); 它将在布局活动\u main中搜索微调器(因为您在onCreate中将活动\u main设置为内容视图),但它不是活动\u main的子级。因此它抛出NullPointerException 而是像这样使用, 微调器=(微调器)mUserLoggedIn.findViewById(R.id.spinner);
(如果微调器位于nav\u header\u login布局中)完整发布代码,是否设置了
ContentView
?是否有名为微调器的活动???最终微调器微调器;微调器=(微调器)findViewById(R.id.spinner);这些行如何有效?无法为此分配最终变量way@csenga,这是final的有效用法,编译器将计算出重建项目的结果,并检查您是否分配了正确的根视图(通过setContentView),但我希望我的微调器显示在导航抽屉标题中,该标题不在main_activity.xml中,它显示:找不到显式活动类{com.example.syedsaadali.myapplication/android.widget.Spinner};您是否在AndroidManifest.xml中声明了此活动?但我的第一个项目默认情况下被单击我希望它被用户@Kiran Rajamby默认方式单击?当您进入该屏幕时,onItemSelected方法是否会自动执行?当draw打开时,会自动注销,因为它是SpinnerOK中的第一个元素他的解决方法。当您向微调器添加项目时,将第一个项目添加为“选择项目”或类似的内容,然后添加其余项目。现在在onItemSelected方法中,插入此代码。如果(msupplier.equalsIgnoreCase(“选择项目”){return;}
<string-array name="items">
<item>Log Out</item>
<item>Setting</item>
</string-array>
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Firebase.setAndroidContext(this);
Firebase rootRef = new Firebase("https://intense-heat-4844.firebaseio.com/property");
navigationView = (NavigationView) findViewById(R.id.nav_view);
View mLoginButton = navigationView.inflateHeaderView(R.layout.nav_header);
mLoginButton.findViewById(R.id.Login);
if (!myBooleanVariable) {
mLoginButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Calling activity LoginRegister
Intent intent = new Intent(getApplication(), LoginRegister.class);
startActivity(intent);
}
});
}
Intent intent = getIntent();
if (null != intent) {
myBooleanVariable = intent.getBooleanExtra("my_boolean_key", myBooleanVariable);
if (myBooleanVariable) {
navigationView.getHeaderView(0).setVisibility(View.GONE);
navigationView = (NavigationView) findViewById(R.id.nav_view);
View mUserLoggedIn = navigationView.inflateHeaderView(R.layout.nav_header_login);
final Spinner spinner;
spinner = (Spinner) findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String msupplier = spinner.getSelectedItem().toString();
Log.e("Selected item : ", msupplier);
Intent intent = new Intent(getApplication(), Spinner.class);
startActivity(intent);
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
}
ePager = (ViewPager) findViewById(R.id.Pager);
ePager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
eTabs = (SlidingTabLayout) findViewById(R.id.TabLayout);
eTabs.setViewPager(ePager);
Toolbartext = (TextView) findViewById(R.id.toolbar_title);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
Toolbartext.setText("Residential Search");
getSupportActionBar().setDisplayShowHomeEnabled(true);
Navigation1 drawerfragment = (Navigation1)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation1);
drawerfragment.setup(R.id.fragment_navigation1, (DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
Toast.makeText(this, "hitted item" + item.getTitle(), Toast.LENGTH_SHORT).show();
return true;
}
return super.onOptionsItemSelected(item);
}
03-01 13:17:47.039 8587-8587/com.example.syedsaadali.myapplication I/System.out﹕ User ID: 4310e794-0b9b-4fad-afd0-e3f73c5f7412, Provider: password
03-01 13:17:47.074 8587-8587/com.example.syedsaadali.myapplication D/AndroidRuntime﹕ Shutting down VM
03-01 13:17:47.074 8587-8587/com.example.syedsaadali.myapplication E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.syedsaadali.myapplication, PID: 8587
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.syedsaadali.myapplication/com.example.syedsaadali.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
at android.app.ActivityThread.access$800(ActivityThread.java:151)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setOnItemSelectedListener(android.widget.AdapterView$OnItemSelectedListener)' on a null object reference
at com.example.syedsaadali.myapplication.MainActivity.onCreate(MainActivity.java:72)