错误android:FATAL EXCEPTION:main

时间:2018-04-02 03:30:20

标签: android nullpointerexception spinner

我有这个错误,这发生在我试图放一个Spinner:

  

致命的例外:主要                     java.lang.RuntimeException:无法启动活动ComponentInfo {mx.ipn.myprefect / mx.ipn.myprefect.Home}:   显示java.lang.NullPointerException                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)                         在android.app.ActivityThread.access $ 600(ActivityThread.java:150)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1298)                         在android.os.Handler.dispatchMessage(Handler.java:99)                         在android.os.Looper.loop(Looper.java:213)                         在android.app.ActivityThread.main(ActivityThread.java:5225)                         at java.lang.reflect.Method.invokeNative(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:525)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:741)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)                         at dalvik.system.NativeStart.main(Native Method)                      引起:java.lang.NullPointerException                         在mx.ipn.myprefect.Home.onCreate(Home.java:44)                         在android.app.Activity.performCreate(Activity.java:5133)                         在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)                         在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)                         在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)                         在android.app.ActivityThread.access $ 600(ActivityThread.java:150)                         在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1298)                         在android.os.Handler.dispatchMessage(Handler.java:99)                         在android.os.Looper.loop(Looper.java:213)                         在android.app.ActivityThread.main(ActivityThread.java:5225)                         at java.lang.reflect.Method.invokeNative(Native Method)                         在java.lang.reflect.Method.invoke(Method.java:525)                         在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:741)                         在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)                         在dalvik.system.NativeStart.main(本地方法)

这是我的所有Home活动,但错误在onCreate方法中:

public class Home extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener{

    private Toolbar toolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);

        Spinner select = findViewById(R.id.select_group);
        ArrayAdapter<String> aaGroup = new ArrayAdapter<String>(Home.this, android.R.layout.simple_list_item_1,
                getResources().getStringArray(R.array.groups));
        aaGroup.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        select.setAdapter(aaGroup);

        FragmentManager fm = getSupportFragmentManager();
        fm.beginTransaction().replace(R.id.contenedor, new BackHome()).commit();

    }

    public void mandar(View view){
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        FragmentManager fm = getSupportFragmentManager();
        MenuRedirect mr = new MenuRedirect();
        Intent it;
        switch (view.getId()){
            case R.id.optionSchedule:
                mr.setOption(1);
                it = new Intent(Home.this, MenuRedirect.class);
                startActivity(it);
                break;
            case R.id.optionReport:
                mr.setOption(2);
                it = new Intent(Home.this, MenuRedirect.class);
                startActivity(it);
                break;
            case R.id.optionStudent:
                mr.setOption(3);
                it = new Intent(Home.this, MenuRedirect.class);
                startActivity(it);
                break;
            default:
                toolbar.setTitle(R.string.title_activity_home);
                fm.beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right)
                        .replace(R.id.contenedor, new BackHome()).commit();
                break;
        }
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        toolbar = (Toolbar)findViewById(R.id.toolbar);
        int id = item.getItemId();
        FragmentManager fm = getSupportFragmentManager();
        Intent it;

        if (id == R.id.nav_home) {
            toolbar.setTitle(R.string.title_activity_home);
            fm.beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right)
                    .replace(R.id.contenedor, new BackHome()).commit();
        } else if (id == R.id.nav_camera) {

        } else if (id == R.id.nav_schedule) {
            it = new Intent(Home.this, MenuRedirect.class);
            it.putExtra("option", 1);
            startActivity(it);
        } else if (id == R.id.nav_reports) {
            it = new Intent(Home.this, MenuRedirect.class);
            it.putExtra("option", 2);
            startActivity(it);
        } else if (id == R.id.nav_students) {
            it = new Intent(Home.this, MenuRedirect.class);
            it.putExtra("option", 3);
            startActivity(it);
        } else if (id == R.id.nav_logout) {
            it = new Intent(Home.this, Login.class);
            startActivity(it);
        }else if (id == R.id.nav_settings) {
            toolbar.setTitle(R.string.action_settings);

        }else if (id == R.id.nav_info) {
            toolbar.setTitle(R.string.menu_start_inf_two);
            fm.beginTransaction().setCustomAnimations(R.anim.enter_from_right, R.anim.exit_to_right)
                    .replace(R.id.contenedor, new Info()).commit();
        }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

这是我的片段:

LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="mx.ipn.myprefect.Schedule"
    android:orientation="vertical">

    <TextView
        android:layout_marginTop="15dp"
        android:id="@+id/tv_schedule_present"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="@string/schedule_TextView"
        android:textStyle="italic"
        android:fontFamily="serif"
        android:textSize="18sp"/>

    <Spinner
        android:id="@+id/select_group"
        android:layout_marginTop="15dp"
        android:layout_marginStart="50dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</LinearLayout>

这有什么问题? ): 如果我删除了select.setAdapter(),则不再显示任何错误

0 个答案:

没有答案