2017-03-16 62 views
-3

所以我試圖通過點擊默認模板導航欄上的按鈕來啓動一個片段,我使用片段事務和提交,但應用程序崩潰,無法找到隱藏在android監視器錯誤部分中的活動下降。我得到這個崩潰使用替換和添加。片段:無法找到活動?

這裏是片段我想補充:

public class FoodScroll extends ListFragment implements AdapterView.OnItemClickListener{ 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){ 
     return inflater.inflate(R.layout.fragment_foodscroll, container, false); 
    } 
    @Override 
    public void onActivityCreated(Bundle savedInstanceState){ 
     super.onActivityCreated(savedInstanceState); 
     ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.foods, android.R.layout.simple_list_item_1); 
     setListAdapter(adapter); 
     getListView().setOnItemClickListener(this); 
    } 
    @Override 
    public void onItemClick(AdapterView<?> parent, View view, int position, long id){ 
     Toast.makeText(getActivity(), "Item" + position, Toast.LENGTH_SHORT).show(); 
    } 
} 

這裏是我想將它放在活動:

public class MainActivity extends AppCompatActivity{ 

    private FragmentManager manager = getFragmentManager(); 
    private FragmentTransaction transaction = manager.beginTransaction(); 
    private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener 
      = new BottomNavigationView.OnNavigationItemSelectedListener() { 

     @Override 
     public boolean onNavigationItemSelected(@NonNull MenuItem item) { 

      switch (item.getItemId()) { 
       case R.id.navigation_home: 
        FoodScroll foods = new FoodScroll(); 
        transaction.replace(R.id.container, foods); 
        transaction.commit(); 
        return true; 
       case R.id.navigation_dashboard: 

        return true; 
       case R.id.navigation_notifications: 

        return true; 
      } 
      return false; 
     } 

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

     BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation); 
     navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener); 

    } 
} 

package語句是不缺的,我把它在片段之外。 道歉,如果我不正確地放置代碼或東西。

該logcat工作不正常,我不得不重新啓動android工作室,讓它再次工作。這是顯示的內容

03-15 22:57:17.090 3238-3238/? I/art: Not late-enabling -Xcheck:jni (already on) 
 
03-15 22:57:17.090 3238-3238/? W/art: Unexpected CPU variant for X86 using defaults: x86 
 
03-15 22:57:17.110 3238-3245/? E/art: Failed sending reply to debugger: Broken pipe 
 
03-15 22:57:17.110 3238-3245/? I/art: Debugger is no longer active 
 
03-15 22:57:17.110 3238-3245/? I/art: Starting a blocking GC Instrumentation 
 
03-15 22:57:17.199 3238-3238/? W/System: ClassLoader referenced unknown path: /data/app/com.namename.www.name-2/lib/x86 
 
03-15 22:57:17.253 3238-3238/? W/art: Before Android 4.1, method android.graphics.PorterDuffColorFilter android.support.graphics.drawable.VectorDrawableCompat.updateTintFilter(android.graphics.PorterDuffColorFilter, android.content.res.ColorStateList, android.graphics.PorterDuff$Mode) would have incorrectly overridden the package-private method in android.graphics.drawable.Drawable 
 
03-15 22:57:17.393 3238-3238/? I/InstantRun: Starting Instant Run Server for com.namename.www.name 
 
03-15 22:57:17.454 3238-3262/? I/OpenGLRenderer: Initialized EGL, version 1.4 
 
03-15 22:57:17.454 3238-3262/? D/OpenGLRenderer: Swap behavior 1 
 
03-15 22:57:17.454 3238-3262/? W/OpenGLRenderer: Failed to choose config with EGL_SWAP_BEHAVIOR_PRESERVED, retrying without... 
 
03-15 22:57:17.454 3238-3262/? D/OpenGLRenderer: Swap behavior 0 
 
03-15 22:57:17.488 3238-3238/? W/art: Before Android 4.1, method int android.support.v7.widget.ListViewCompat.lookForSelectablePosition(int, boolean) would have incorrectly overridden the package-private method in android.widget.ListView 
 
03-15 22:57:25.564 3238-3238/com.chilosomexicanbistro.www.chiloso D/AndroidRuntime: Shutting down VM 
 
03-15 22:57:25.564 3238-3238/com.chilosomexicanbistro.www.chiloso E/AndroidRuntime: FATAL EXCEPTION: main 
 
                        Process: com.namename.www.name, PID: 3238 
 
                        java.lang.RuntimeException: Content has view with id attribute 'android.R.id.list' that is not a ListView class 
 
                         at android.app.ListFragment.ensureList(ListFragment.java:402) 
 
                         at android.app.ListFragment.onViewCreated(ListFragment.java:203) 
 
                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1010) 
 
                         at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1171) 
 
                         at android.app.BackStackRecord.run(BackStackRecord.java:816) 
 
                         at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1578) 
 
                         at android.app.FragmentManagerImpl$1.run(FragmentManager.java:483) 
 
                         at android.os.Handler.handleCallback(Handler.java:751) 
 
                         at android.os.Handler.dispatchMessage(Handler.java:95) 
 
                         at android.os.Looper.loop(Looper.java:154) 
 
                         at android.app.ActivityThread.main(ActivityThread.java:6119) 
 
                         at java.lang.reflect.Method.invoke(Native Method) 
 
                         at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
 
                         at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

+1

請發佈堆棧跟蹤。 http://stackoverflow.com/questions/23353173/uncomfort-myapp-has-stopped-how-can-i-solve-this –

+1

堆棧跟蹤與您提供的代碼不匹配。該代碼不會在'onNavigationItemSelected()'中調用'startActivity()'。如果您的應用程序與您顯示的代碼崩潰,則需要提供該堆棧跟蹤。如果它沒有從代碼中崩潰,你需要更好地解釋問題是什麼。 –

+0

我不知道,那是什麼在錯誤的Android監視器選項卡。當我點擊應該添加或替換片段的按鈕時,該應用崩潰,說它已停止。 –

回答

1

有幾個問題在這裏:

問題1 - 使用靜態初始化錯誤

private FragmentManager manager = getFragmentManager(); //no! don't do this 
private FragmentTransaction transaction = manager.beginTransaction(); //no! 

使用靜態初始化呢與Android Activity lifecycles不一致。請仔細研究這些內容並密切關注Android文檔中的示例 - 您幾乎不會在示例Activity和Fragments中找到任何靜態初始化。

問題2 - 不檢查FragmentManager,看它是否已經包含了片段

 switch (item.getItemId()) { 
      case R.id.navigation_home: 
       FoodScroll foods = new FoodScroll(); //no! check first if the fragment is already in the FragmentManager 
       transaction.replace(R.id.container, foods); 
       transaction.commit(); 
       return true; 

這段代碼是錯的 - 同樣,在示例中,你幾乎總是看到慣用寫爲getFragmentManager().beginTransaction()...呼叫。請仔細閱讀文檔中的示例碎片和活動。相信我,這將節省您的時間:)

,而不是上面的代碼,我想你想是這樣的:

case R.id.navigation_home: 
    FoodScroll foods = getFragmentManager().findFragmentByTag("FOOD"); 
    if (foods == null) { 
     foods = new FoodScroll(); 
    } 
    getFragmentManager().beginTransaction().replace(R.id.container, foods, "FOOD").commit(); 
    return true; 

問題3 - 用正確的堆棧跟蹤OP編輯之後更新:

根據Mike M的評論,在ListFragment中,如果使用自己的佈局,ListView的ID必須是@android:id/list

+0

您發佈的堆棧跟蹤在我回答後深刻地改變了這個問題。麥克M的評論是正確的。 –

+0

我對此表示歉意 –

+0

@Fail Pail無需道歉,我認爲答案仍然有用信息 –