0

:)打開在現有的片段(主詳細設計)的片段

我在使用中Android中一個主詳細視圖從現有的片段打開另一個片段的問題。我嘗試了各種解決方案,但沒有任何幫助。 我有一個listview作爲一個菜單,在那裏我可以點擊不同的項目。通過點擊它們,正確的片段將被打開。在一個特殊的片段中,我想實現一個應該用另一個替換現有片段的按鈕,但這不起作用。我已經添加了敬酒,看看onclick方法是否有效,它確實起作用,只是片段切換不會完成他的工作。任何人都可以幫助我,或給我任何提示,我即將很快就會發狂! :d

private Button btnLogin; 

btnLogin = new Button(getActivity()); 
btnLogin.setLayoutParams(params); 
btnLogin.setText("Login"); 

btnLogin.setOnClickListener(new OnClickListener() { 
     @Override 
     public void onClick(View rootView) { 
      Toast toast = Toast.makeText(getActivity(), "register", 
        Toast.LENGTH_SHORT); 
      toast.show(); 

      Fragment fragment = new FragmentApps(); 
      FragmentTransaction transaction = getFragmentManager() 
        .beginTransaction(); 

      // if(addToBack) 
      transaction.addToBackStack(null); 

      transaction.replace(R.id.fragment_item_detail_1, fragment).commit(); 
     } 
    }); 

這是錯誤消息我得到:

12-03 11:30:06.839: E/AndroidRuntime(9464): FATAL EXCEPTION: main 
12-03 11:30:06.839: E/AndroidRuntime(9464): Process: at.test.app, PID: 9464 
12-03 11:30:06.839: E/AndroidRuntime(9464): java.lang.NullPointerException: Attempt to invoke   virtual method 'boolean android.os.Bundle.containsKey(java.lang.String)' on a null object reference 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at  at.test.app.apps.FragmentApps.onCreate(FragmentApps.java:49) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.app.Fragment.performCreate(Fragment.java:2031) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1067) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.app.BackStackRecord.run(BackStackRecord.java:833) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1452) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:447) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.os.Handler.handleCallback(Handler.java:739) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.os.Handler.dispatchMessage(Handler.java:95) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.os.Looper.loop(Looper.java:135) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at android.app.ActivityThread.main(ActivityThread.java:5221) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at java.lang.reflect.Method.invoke(Native Method) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at java.lang.reflect.Method.invoke(Method.java:372) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
12-03 11:30:06.839: E/AndroidRuntime(9464):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

在此先感謝您的幫助!

編輯 - >整個代碼: package at.test.app.login;

import android.os.Bundle; 
import android.app.Activity; 
import android.app.Fragment; 
import android.app.FragmentTransaction; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.TextView; 
import android.widget.Toast; 


/** 
* A fragment representing a single Item detail screen. This fragment is either 
* contained in a {@link ItemListActivity} in two-pane mode (on tablets) or a 
* {@link ItemDetailActivity} on handsets. 
*/ 
public class FragmentTest extends Fragment { 

    private LinearLayout linearLayout; 
    private TextView sample; 
    private Button btnLogin; 

    /** 
    * The fragment argument representing the item ID that this fragment 
    * represents. 
    */ 
    public static final String ARG_ITEM_ID = "item_id"; 

    /** 
    * The dummy content this fragment is presenting. 
    */ 
    private ItemContent.Item mItem; 

    /** 
    * Mandatory empty constructor for the fragment manager to instantiate the 
    * fragment (e.g. upon screen orientation changes). 
    */ 
    public FragmentTest() { 
    } 

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

     getActivity().setTitle(getResources().getString(R.string.apps_titel)); 

     if (getArguments().containsKey(ARG_ITEM_ID)) { 
      // Load the dummy content specified by the fragment 
      // arguments. In a real-world scenario, use a Loader 
      // to load content from a content provider. 
      mItem = ItemContent.ITEM_MAP.get(getArguments().getString(
        ARG_ITEM_ID)); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 
     View rootView = inflater.inflate(R.layout.fragment_item_detail, 
       container, false); 

     linearLayout = (LinearLayout) rootView.findViewById(R.id.fragment_item_detail_1); 
     LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
       android.widget.LinearLayout.LayoutParams.FILL_PARENT, 
       android.widget.LinearLayout.LayoutParams.WRAP_CONTENT); 
     sample = new TextView(getActivity()); 
     sample.setText(getResources().getString(R.string.apps_titel)); 

     btnLogin = new Button(getActivity()); 
     btnLogin.setLayoutParams(params); 
     btnLogin.setText("Login"); 


     linearLayout.addView(sample); 
     linearLayout.addView(btnLogin); 

     btnLogin.setOnClickListener(new OnClickListener() { 
      @Override 
      public void onClick(View rootView) { 
       Toast toast = Toast.makeText(getActivity(), "register", 
         Toast.LENGTH_SHORT); 
       toast.show(); 

       Fragment fragment = new FragmentApps(); 
       FragmentTransaction transaction = getChildFragmentManager() 
         .beginTransaction(); 

       // if(addToBack) 
       transaction.addToBackStack(null); 

       transaction.replace(R.id.fragment_item_detail_1, fragment).commit(); 


       // Intent i = new Intent(getActivity(), 
       // FragmentRegister.class); 
       // startActivity(i); 
       // getActivity().finish(); 
      } 
     }); 

     return rootView; 
    } 
} 
+0

你用捆綁任何地方??? – Piyush 2014-12-03 11:15:34

+0

是公共無效的onCreate(捆綁savedInstanceState)和@覆蓋 \t公共查看onCreateView(LayoutInflater吹氣,ViewGroup中容器, \t \t \t捆綁savedInstanceState) – 2014-12-03 11:20:05

+0

所以問題是存在的......它 – Piyush 2014-12-03 11:20:56

回答

0

我發現我自己的答案:

在MainActivity

我在LoginFragment添加引用

private void setDetailFragment(String id, int fragmentToReplace, 
     boolean addToBack) { 
     Bundle arguments = new Bundle(); 
     arguments.putString(ItemDetailFragment.ARG_ITEM_ID, id); 

     Fragment fragment = new ItemDetailFragment(); 
     if (id.equals("register")){ 
      fragment = new FragmentRegister(); 
     } else if (id.equals("dashboard")){ 
      fragment = new FragmentDashboard(); 
     } 
     fragment.setArguments(arguments); 

     FragmentTransaction transaction = getFragmentManager() 
       .beginTransaction(); 
     transaction.addToBackStack(null); 
     transaction.replace(fragmentToReplace, fragment).commit(); 

    } 

我添加此功能:

 @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     if (!(activity instanceof Callbacks)) { 
      throw new IllegalStateException(
        "Activity must implement fragment's callbacks."); 
     } 

     mCallbacks = (Callbacks) activity; 
    } 

和按鈕看起來是這樣的:

btnLogin.setOnClickListener(new View.OnClickListener() { 

     public void onClick(View view) { 
      mCallbacks.onItemSelected("dashboard"); 
     } 
    }); 
0

使用getChildFragmentManager()代替getFragmentManager

+0

感謝您的回答,但錯誤是一樣的:( – 2014-12-03 11:17:58

+0

哦對不起,我沒有看到錯誤, ?哪一個是線49 – TheRedFox 2014-12-03 11:19:41

+0

我已經editetd問題文本,線49是:@覆蓋 \t公共無效的onCreate(捆綁savedInstanceState){ \t \t super.onCreate(savedInstanceState); – 2014-12-03 11:26:00