0

後,我成立了一個微調,我得到了folllowing錯誤: 了java.lang.RuntimeException:無法啓動活動ComponentInfo微調得到錯誤:了java.lang.RuntimeException:無法啓動活動ComponentInfo

我發現,當我註釋掉這一行:

spinner1.setOnItemSelectedListener(新Spinner1Listener());

在MainActivity類(線195),它工作一切正常,但後來我顯然不能處理OnItemClicked的微調活動

這裏是我的MainActivity類別

package school.joran.trainitknowit; 

import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.content.SharedPreferences; 
import android.os.Bundle; 
import android.support.v4.widget.DrawerLayout; 
import android.support.v7.app.ActionBarDrawerToggle; 
import android.support.v7.app.AppCompatActivity; 
import android.support.v7.widget.LinearLayoutManager; 
import android.support.v7.widget.RecyclerView; 
import android.support.v7.widget.Toolbar; 
import android.view.GestureDetector; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.MotionEvent; 
import android.view.View; 
import android.widget.Spinner; 
import android.widget.Toast; 

import school.joran.trainitknowit.fragments.FragmentDefault; 
import school.joran.trainitknowit.fragments.FragmentSettings; 
import school.joran.trainitknowit.menu.main.mainAdapter; 

public class MainActivity extends AppCompatActivity { 

    private Toolbar toolbar; 

    String navMenuTitles[]; 
    int navnMenuIcons[] = {R.drawable.ic_plus_grey, R.drawable.ic_library_grey, R.drawable.ic_import_export_grey, R.drawable.ic_settings_grey, R.drawable.ic_help_grey}; 

    SharedPreferences prefs; 
    SharedPreferences.Editor prefsEditor; 

    final String KEYUserLoggedIn = "keyUserLoggedIn"; 
    final String KEYUsername = "keyUsername"; 
    final String KEYUserProfilePic = "keyUserProfilePic"; 

    public Boolean userLoggedIn; 
    String username; 
    int userProfilPic; 

    RecyclerView mRecyclerView;       // Declaring RecyclerView 
    RecyclerView.Adapter mAdapter;      // Declaring Adapter For Recycler View 
    RecyclerView.LayoutManager mLayoutManager;   // Declaring Layout Manager as a linear layout manager 
    DrawerLayout Drawer;         // Declaring DrawerLayout 

    ActionBarDrawerToggle mDrawerToggle;     // Declaring Action Bar Drawer Toggle 

    public FragmentManager fragManager; 
    public FragmentTransaction fragTrans; 

    private Spinner spinner1; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     addListenerOnSpinner1(); 

     navMenuTitles = getResources().getStringArray(R.array.menu_main_item_names); 

     toolbar = (Toolbar) findViewById(R.id.toolbar); // Attaching the layout to the toolbar object 
     toolbar.setTitle(R.string.app_title); 
     setSupportActionBar(toolbar); // Setting toolbar as the ActionBar with setSupportActionBar() call 

     fragManager = getFragmentManager(); //Setting Fragment Manager 
     fragTrans = fragManager.beginTransaction(); //Beginning Transaction so the Fragment can be created 
     fragTrans.add(R.id.MainLayout, new FragmentDefault()); 
     fragTrans.commit(); 

     prefs = this.getSharedPreferences("mainPreferences", MODE_PRIVATE); 
     prefsEditor = prefs.edit(); 

     userLoggedIn = prefs.getBoolean(KEYUserLoggedIn, false); 
     username = prefs.getString(KEYUsername, getString(R.string.no_user)); 
     userProfilPic = prefs.getInt(KEYUserProfilePic, R.drawable.ic_user_template_white); 

     prefs.edit().apply(); 

     mRecyclerView = (RecyclerView) findViewById(R.id.RecyclerView); // Assigning the RecyclerView Object to the xml View 
     mRecyclerView.setHasFixedSize(true);       // Letting the system know that the list objects are of fixed size 

     mAdapter = new mainAdapter(navMenuTitles, navnMenuIcons, username, userProfilPic);  // Creating the Adapter of MyAdapter class(which we are going to see in a bit) 
     // And passing the titles,icons,header view name, header view email, 
     // and header view profile picture 

     mRecyclerView.setAdapter(mAdapter);        // Setting the adapter to RecyclerView 
     mLayoutManager = new LinearLayoutManager(this);     // Creating a layout Manager 
     mRecyclerView.setLayoutManager(mLayoutManager);     // Setting the layout Manager 

     final GestureDetector mGestureDetector = new GestureDetector(MainActivity.this, new GestureDetector.SimpleOnGestureListener() { 

      @Override 
      public boolean onSingleTapUp(MotionEvent e) { 
       return true; 
      } 

     }); 

     Drawer = (DrawerLayout) findViewById(R.id.DrawerLayout);  // Drawer object Assigned to the view 
     mDrawerToggle = new ActionBarDrawerToggle(this, Drawer, toolbar, R.string.openDrawer, R.string.closeDrawer) { 
      @Override 
      public void onDrawerOpened(View drawerView) { 
       super.onDrawerOpened(drawerView); 
       // code here will execute once the drawer is opened(As I dont want anything happened when drawer is 
       // open I am not going to put anything here) 
      } 

      @Override 
      public void onDrawerClosed(View drawerView) { 
       super.onDrawerClosed(drawerView); 
       // Code here will execute once drawer is closed 
      } 
     }; 

     Drawer.setDrawerListener(mDrawerToggle); // Drawer Listener set to the Drawer toggle 
     mDrawerToggle.syncState();    // Finally we set the drawer toggle sync State 

     mRecyclerView.addOnItemTouchListener(new RecyclerView.OnItemTouchListener() { 

      @Override 
      public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { 

       View child = recyclerView.findChildViewUnder(motionEvent.getX(), motionEvent.getY()); 

       if (child != null && mGestureDetector.onTouchEvent(motionEvent)) { 
        Drawer.closeDrawers(); 
        fragManager = getFragmentManager(); 

        switch (recyclerView.getChildAdapterPosition(child)) { 
         case 0: { 
          Toast.makeText(MainActivity.this, R.string.nicht_verfügbar, Toast.LENGTH_SHORT).show(); 
          break; 
         } 
         case 1: { 
          toolbar.setTitle(R.string.app_title); 
          fragTrans = fragManager.beginTransaction(); //Beginning Transaction so the Fragment can be created 
          fragTrans.replace(R.id.MainLayout, new FragmentDefault()); 
          fragTrans.commit(); 
          break; 
         } 
         case 2: { 
          toolbar.setTitle(navMenuTitles[1]); 
          fragTrans = fragManager.beginTransaction(); //Beginning Transaction so the Fragment can be created 
          fragTrans.replace(R.id.MainLayout, new FragmentDefault()); 
          fragTrans.commit(); 
          break; 
         } 
         case 3: { 
          toolbar.setTitle(navMenuTitles[2]); 
          fragTrans = fragManager.beginTransaction(); //Beginning Transaction so the Fragment can be created 
          fragTrans.replace(R.id.MainLayout, new FragmentDefault()); 
          fragTrans.commit(); 
          break; 
         } 
         case 4: { 
          toolbar.setTitle(navMenuTitles[3]); 
          fragTrans = fragManager.beginTransaction(); //Beginning Transaction so the Fragment can be created 
          fragTrans.replace(R.id.MainLayout, new FragmentSettings()); 
          fragTrans.commit(); 
          break; 
         } 
         case 5: { 
          toolbar.setTitle(navMenuTitles[4]); 
          fragTrans = fragManager.beginTransaction(); //Beginning Transaction so the Fragment can be created 
          fragTrans.replace(R.id.MainLayout, new FragmentDefault()); 
          fragTrans.commit(); 
          break; 
         } 
        } 

        return true; 

       } 

       return false; 
      } 

      @Override 
      public void onTouchEvent(RecyclerView recyclerView, MotionEvent motionEvent) { 

      } 

      @Override 
      public void onRequestDisallowInterceptTouchEvent(boolean disallowIntercept) { 

      } 
     }); 
    } 

    public void addListenerOnSpinner1() { 
     spinner1 = (Spinner) findViewById(R.id.spinnerBenachrichtigungenZeitintervalle); 
     spinner1.setOnItemSelectedListener(new Spinner1Listener()); 
    } 

    @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) { 
      return true; 
     }*/ 

     return super.onOptionsItemSelected(item); 
    } 
} 

這是我完整的錯誤日誌,當我嘗試運行我的應用程序:

10-10 00:51:54.727 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ Zygote::ForkAndSpecialize : 0 
10-10 00:51:54.727 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ zygote get new systemTid : 9565 
10-10 00:51:54.727 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ Late-enabling CheckJNI 
10-10 00:51:54.729 9565-9567/school.joran.trainitknowit D/dalvikvm﹕ threadid=2: interp stack at 0x5a64a000 
10-10 00:51:54.731 9565-9569/school.joran.trainitknowit D/dalvikvm﹕ threadid=3: interp stack at 0x5a752000 
10-10 00:51:54.732 9565-9569/school.joran.trainitknowit D/dalvikvm﹕ Elevating priority from 0 to -8 
10-10 00:51:54.732 9565-9565/school.joran.trainitknowit D/jdwp﹕ prepping for JDWP over ADB 
10-10 00:51:54.732 9565-9565/school.joran.trainitknowit D/jdwp﹕ ADB transport startup 
10-10 00:51:54.739 9565-9571/school.joran.trainitknowit D/dalvikvm﹕ threadid=4: interp stack at 0x5a85a000 
10-10 00:51:54.740 9565-9571/school.joran.trainitknowit D/jdwp﹕ JDWP: thread running 
10-10 00:51:54.740 9565-9571/school.joran.trainitknowit D/jdwp﹕ acceptConnection 
10-10 00:51:54.740 9565-9571/school.joran.trainitknowit D/jdwp﹕ trying to receive file descriptor from ADB 
10-10 00:51:54.740 9565-9573/school.joran.trainitknowit D/dalvikvm﹕ threadid=5: interp stack at 0x5d15b000 
10-10 00:51:54.740 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ zygote get thread init done 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ create interp thread : stack size=32KB 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ create new thread 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ new thread created 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ update thread list 
10-10 00:51:54.741 9565-9574/school.joran.trainitknowit D/dalvikvm﹕ threadid=6: interp stack at 0x5d163000 
10-10 00:51:54.741 9565-9574/school.joran.trainitknowit D/dalvikvm﹕ threadid=6: created from interp 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ start new thread 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ create interp thread : stack size=32KB 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ create new thread 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ new thread created 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ update thread list 
10-10 00:51:54.741 9565-9574/school.joran.trainitknowit D/dalvikvm﹕ threadid=6: notify debugger 
10-10 00:51:54.741 9565-9574/school.joran.trainitknowit D/dalvikvm﹕ threadid=6 (ReferenceQueueDaemon): calling run() 
10-10 00:51:54.741 9565-9575/school.joran.trainitknowit D/dalvikvm﹕ threadid=7: interp stack at 0x5d26b000 
10-10 00:51:54.741 9565-9575/school.joran.trainitknowit D/dalvikvm﹕ threadid=7: created from interp 
10-10 00:51:54.741 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ start new thread 
10-10 00:51:54.742 9565-9575/school.joran.trainitknowit D/dalvikvm﹕ threadid=7: notify debugger 
10-10 00:51:54.742 9565-9575/school.joran.trainitknowit D/dalvikvm﹕ threadid=7 (FinalizerDaemon): calling run() 
10-10 00:51:54.742 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ create interp thread : stack size=32KB 
10-10 00:51:54.742 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ create new thread 
10-10 00:51:54.742 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ new thread created 
10-10 00:51:54.742 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ update thread list 
10-10 00:51:54.744 9565-9576/school.joran.trainitknowit D/dalvikvm﹕ threadid=8: interp stack at 0x5d373000 
10-10 00:51:54.744 9565-9576/school.joran.trainitknowit D/dalvikvm﹕ threadid=8: created from interp 
10-10 00:51:54.744 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ start new thread 
10-10 00:51:54.746 9565-9576/school.joran.trainitknowit D/dalvikvm﹕ threadid=8: notify debugger 
10-10 00:51:54.746 9565-9576/school.joran.trainitknowit D/dalvikvm﹕ threadid=8 (FinalizerWatchdogDaemon): calling run() 
10-10 00:51:54.754 9565-9577/school.joran.trainitknowit D/dalvikvm﹕ threadid=9: interp stack at 0x5d779000 
10-10 00:51:54.754 9565-9578/school.joran.trainitknowit D/dalvikvm﹕ threadid=10: interp stack at 0x5d881000 
10-10 00:51:54.800 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ open_cached_dex_file : /data/app/school.joran.trainitknowit-2.apk /data/dalvik-cache/[email protected]@[email protected] 
10-10 00:51:54.813 9565-9565/school.joran.trainitknowit W/dalvikvm﹕ VFY: unable to find class referenced in signature (Landroid/view/SearchEvent;) 
10-10 00:51:54.814 9565-9565/school.joran.trainitknowit I/dalvikvm﹕ Could not find method android.view.Window$Callback.onSearchRequested, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onSearchRequested 
10-10 00:51:54.814 9565-9565/school.joran.trainitknowit W/dalvikvm﹕ VFY: unable to resolve interface method 17267: Landroid/view/Window$Callback;.onSearchRequested (Landroid/view/SearchEvent;)Z 
10-10 00:51:54.814 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 
10-10 00:51:54.814 9565-9565/school.joran.trainitknowit I/dalvikvm﹕ Could not find method android.view.Window$Callback.onWindowStartingActionMode, referenced from method android.support.v7.internal.view.WindowCallbackWrapper.onWindowStartingActionMode 
10-10 00:51:54.814 9565-9565/school.joran.trainitknowit W/dalvikvm﹕ VFY: unable to resolve interface method 17271: Landroid/view/Window$Callback;.onWindowStartingActionMode (Landroid/view/ActionMode$Callback;I)Landroid/view/ActionMode; 
10-10 00:51:54.814 9565-9565/school.joran.trainitknowit D/dalvikvm﹕ VFY: replacing opcode 0x72 at 0x0002 
10-10 00:51:54.824 9565-9571/school.joran.trainitknowit D/jdwp﹕ received file descriptor 48 from ADB 
10-10 00:51:54.827 9565-9571/school.joran.trainitknowit D/jdwp﹕ processIncoming 
10-10 00:51:54.830 9565-9571/school.joran.trainitknowit D/jdwp﹕ processIncoming 
10-10 00:51:54.830 9565-9571/school.joran.trainitknowit D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000B066, flags=0x0, dataLen=0x8 
10-10 00:51:54.839 9565-9571/school.joran.trainitknowit D/jdwp﹕ processIncoming 
10-10 00:51:54.839 9565-9571/school.joran.trainitknowit D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x17, id=0x4000B067, flags=0x0, dataLen=0xC 
10-10 00:51:54.839 9565-9571/school.joran.trainitknowit D/jdwp﹕ processIncoming 
10-10 00:51:54.839 9565-9571/school.joran.trainitknowit D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000B068, flags=0x0, dataLen=0x8 
10-10 00:51:54.840 9565-9571/school.joran.trainitknowit D/jdwp﹕ processIncoming 
10-10 00:51:54.840 9565-9571/school.joran.trainitknowit D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x13, id=0x4000B069, flags=0x0, dataLen=0x8 
10-10 00:51:54.840 9565-9571/school.joran.trainitknowit D/jdwp﹕ processIncoming 
10-10 00:51:54.842 9565-9571/school.joran.trainitknowit D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x4000B06A, flags=0x0, dataLen=0x9 
10-10 00:51:54.868 9565-9571/? D/jdwp﹕ processIncoming 
10-10 00:51:54.869 9565-9571/? D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x4000B06C, flags=0x0, dataLen=0x9 
10-10 00:51:54.871 9565-9565/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations 
10-10 00:51:54.871 9565-9565/? W/dalvikvm﹕ VFY: unable to resolve virtual method 426: Landroid/content/res/TypedArray;.getChangingConfigurations()I 
10-10 00:51:54.871 9565-9565/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
10-10 00:51:54.872 9565-9571/? D/jdwp﹕ processIncoming 
10-10 00:51:54.872 9565-9571/? D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x4000B06E, flags=0x0, dataLen=0x9 
10-10 00:51:54.873 9565-9565/? I/dalvikvm﹕ Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType 
10-10 00:51:54.873 9565-9565/? W/dalvikvm﹕ VFY: unable to resolve virtual method 448: Landroid/content/res/TypedArray;.getType (I)I 
10-10 00:51:54.873 9565-9565/? D/dalvikvm﹕ VFY: replacing opcode 0x6e at 0x0002 
10-10 00:51:54.877 9565-9565/? D/skia﹕ Flag is not 10 
10-10 00:51:54.899 9565-9565/? D/AndroidRuntime﹕ Shutting down VM 
10-10 00:51:54.899 9565-9565/? W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4109b9a8) 
10-10 00:51:54.901 9565-9565/? E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{school.joran.trainitknowit/school.joran.trainitknowit.MainActivity}: java.lang.NullPointerException 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2377) 
      at android.app.ActivityThread.access$600(ActivityThread.java:167) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
      at android.os.Handler.dispatchMessage(Handler.java:99) 
      at android.os.Looper.loop(Looper.java:153) 
      at android.app.ActivityThread.main(ActivityThread.java:5341) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:511) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.NullPointerException 
      at school.joran.trainitknowit.MainActivity.addListenerOnSpinner1(MainActivity.java:195) 
      at school.joran.trainitknowit.MainActivity.onCreate(MainActivity.java:60) 
      at android.app.Activity.performCreate(Activity.java:5125) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1081) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2289) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2377) 
            at android.app.ActivityThread.access$600(ActivityThread.java:167) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
            at android.os.Handler.dispatchMessage(Handler.java:99) 
            at android.os.Looper.loop(Looper.java:153) 
            at android.app.ActivityThread.main(ActivityThread.java:5341) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:511) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696) 
            at dalvik.system.NativeStart.main(Native Method) 
10-10 00:51:55.406 9565-9571/? D/jdwp﹕ processIncoming 
10-10 00:51:55.406 9565-9571/? D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x4000B06F, flags=0x0, dataLen=0x9 
10-10 00:51:55.406 9565-9571/? D/jdwp﹕ sendBufferedRequest : len=0x34 
10-10 00:51:55.903 9565-9571/? D/jdwp﹕ processIncoming 
10-10 00:51:55.904 9565-9571/? D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x4000B070, flags=0x0, dataLen=0x9 
10-10 00:51:55.904 9565-9571/? D/jdwp﹕ sendBufferedRequest : len=0x34 
10-10 00:51:56.403 9565-9571/? D/jdwp﹕ processIncoming 
10-10 00:51:56.403 9565-9571/? D/jdwp﹕ handlePacket : cmd=0x1, cmdSet=0xC7, len=0x14, id=0x4000B071, flags=0x0, dataLen=0x9 
10-10 00:51:56.403 9565-9571/? D/jdwp﹕ sendBufferedRequest : len=0x34 
10-10 00:51:56.550 9565-9565/? I/Process﹕ Sending signal. PID: 9565 SIG: 9 

而且其他重要代碼:

FragmentSetting.java

package school.joran.trainitknowit.fragments; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

import school.joran.trainitknowit.R; 

public class FragmentSettings extends Fragment { 

    View myView; 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    } 

    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 
     myView = inflater.inflate(R.layout.fragment_settings, container, false); 
     return myView; 
    } 

    @Override 
    public void onActivityCreated(Bundle savedInstanceState) { 
     super.onActivityCreated(savedInstanceState); 
    } 
} 

fragment_setting.xml

<?xml version="1.0" encoding="utf-8"?> 

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android" 
    android:id="@+id/scrollViewSettings" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:orientation="vertical"> 

     <TextView 
      android:id="@+id/textGlobaleEinstellungen" 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:layout_marginBottom="3dp" 
      android:layout_marginLeft="10dp" 
      android:layout_marginTop="20dp" 
      android:text="@string/GlobaleEinstellungen" 
      android:textAppearance="?android:attr/textAppearanceLarge" 
      android:textStyle="bold" /> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="2dp" 
      android:background="@color/divider" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_margin="10dp" 
      android:focusable="false" 
      android:orientation="horizontal"> 

      <Switch 
       android:id="@+id/switchBenachrichtigen" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_gravity="right" 
       android:switchMinWidth="80dp" 
       android:text="@string/regelmBenachrichtigen" 
       android:textOff="@string/aus" 
       android:textOn="@string/ein" 
       android:thumb="@drawable/switch_thumb" 
       android:track="@drawable/switch_track" /> 
     </LinearLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/divider" /> 

     <LinearLayout 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:layout_gravity="center_horizontal" 
      android:layout_margin="10dp" 
      android:orientation="horizontal"> 

      <Spinner 
       android:id="@+id/spinnerBenachrichtigungenZeitintervalle" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content" 
       android:entries="@array/benachrichtigungenZeitintervalle" /> 
     </LinearLayout> 

     <View 
      android:layout_width="match_parent" 
      android:layout_height="1dp" 
      android:background="@color/divider" /> 


    </LinearLayout> 
</ScrollView> 

AndroidManifest.xml中

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="school.joran.trainitknowit" > 

    <application 
     android:allowBackup="true" 
     android:icon="@mipmap/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="school.joran.trainitknowit.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
    </application> 

</manifest> 

請幫幫我!我搜索了互聯網的小時,並找不到解決方案。

回答

0

您的微調器是您的片段佈局的一部分,但您在活動內調用addListenerOnSpinner()。活動的佈局沒有這種微調,所以,當你執行

spinner1 = (Spinner) findViewById(R.id.spinnerBenachrichtigungenZeitintervalle); 

spinner1是空的,所以下一行拋出NullPointerException

如果微調是分段佈局,那麼你需要把你的微調代碼片段,而不是活動。

+0

哇感謝您的快速回復,其實一開始我試圖把整個代碼在我FragmentSettings.java微調,但隨後表示,它無法解析「findViewById」的方法,我把它放在MainActivity。我只是發現它的作品,當我說myView.findViewById() –

相關問題