2014-04-01 182 views
3

我不知道如何打開按鈕點擊一個新的片段,我只知道如何打開新的活動使用意圖,但當我試圖打開新的片段我的項目應用程序崩潰希望有人能幫助我與我的問題按鈕點擊裏面片段打開新的片段

這是我有:

import android.app.Fragment; 
import android.os.Bundle; 
import android.app.FragmentManager; 
import android.app.FragmentTransaction; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.Spinner; 

public class FragmentTwo extends Fragment { 


public FragmentTwo() { 

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
     Bundle savedInstanceState) { 

    View view = inflater.inflate(R.layout.fragment_layout_two, container, 
      false); 

    // SPINNER1 
    Spinner spinner1 = (Spinner) view.findViewById(R.id.spinnerSpecialty); 
    ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
      getActivity(), R.array.specialty_arrays, 
      android.R.layout.simple_spinner_item); 
    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    spinner1.setAdapter(adapter); 

    // SPINNER2 
    Spinner spinner2 = (Spinner) view.findViewById(R.id.spinnerLocation); 
    ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(
      getActivity(), R.array.city_array, 
      android.R.layout.simple_spinner_item); 
    adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    spinner2.setAdapter(adapter); 

    // BUTTON 
    Button btnSearch = (Button) view.findViewById(R.id.btnSearch); 
    btnSearch.setOnClickListener(new View.OnClickListener() { 

     @Override 
      public void onClick(View v) { 
       switch (v.getId()) { 
       case R.id.btnSearch: 
        //what to put here 
        FragmentManager fm = getFragmentManager(); 
        FragmentTransaction ft = fm.beginTransaction(); 
        ft.replace(R.id.content_frame, new FragmentThree()); 
        ft.commit(); 
       break; 
       } 
      } 
    }); 

    return view; 
} 

}

的logcat:

04-01 03:45:25.508: E/AndroidRuntime(1618): FATAL EXCEPTION: main 
04-01 03:45:25.508: E/AndroidRuntime(1618): java.lang.NullPointerException 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at   com.droid.FragmentThree.onCreateView(FragmentThree.java:30) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.app.Fragment.performCreateView(Fragment.java:1695) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:885) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1057) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.app.BackStackRecord.run(BackStackRecord.java:682) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1435) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.app.FragmentManagerImpl$1.run(FragmentManager.java:441) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.os.Handler.handleCallback(Handler.java:730) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.os.Handler.dispatchMessage(Handler.java:92) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.os.Looper.loop(Looper.java:137) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at android.app.ActivityThread.main(ActivityThread.java:5103) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at java.lang.reflect.Method.invokeNative(Native Method) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at java.lang.reflect.Method.invoke(Method.java:525) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
04-01 03:45:25.508: E/AndroidRuntime(1618):  at dalvik.system.NativeStart.main(Native Method) 
+0

請確保您使用android.app.FragmentManager和android.app.FragmentTransaction而不是他們android.app.support.v4。*同行,我看你在Vamshi的編輯之前使用,或者你會得到意想不到的結果 –

+0

是啊我在'ExampleFragment'中改變了進口 –

回答

6

嘗試這個:

從下面的代碼更改id show_fra gment以配合您的佈局XML ID ..

@Override 
      public void onClick(View v) { 
       switch (v.getId()) { 
       case R.id.btnSearch: 
        //what to put here 
        FragmentManager fm = getFragmentManager(); 
        FragmentTransaction ft = fm.beginTransaction(); 
        ft.replace(R.id.show_fragment, new TestFragment(), "fragment_screen"); 
        ft.commit(); 
       break; 
       } 
      } 


<FrameLayout 
     android:id="@+id/show_fragment" 
     android:layout_width="match_parent" 
     android:layout_height="0dp"> 

    </FrameLayout> 
1

我希望這有助於你

// Create new fragment and transaction 
Fragment newFragment = new ExampleFragment(); 
FragmentTransaction transaction = getFragmentManager().beginTransaction(); 

// Replace whatever is in the fragment_container view with this fragment, 
// and add the transaction to the back stack 
transaction.replace(R.id.fragment_container, newFragment); 
transaction.addToBackStack(null); 

// Commit the transaction 
transaction.commit(); 
+0

我會把我想要打開的片段按下後點擊右鍵? –

+0

是的,你必須添加新的片段 – Ranjith

+0

以及R.id.fragment_container呢?對不起im newbee –

0

下面的代碼幫你呼籲按鈕點擊新片段。

Button btnSearch = (Button) view.findViewById(R.id.btnSearch); 
    btnSearch.setOnClickListener(new View.OnClickListener() { 

     @Override 
     public void onClick(View v) { 
      switch (v.getId()) { 
      case R.id.btnSearch: 
       FragmentHome home = new FragmentHome(); //this is your new fragment. 
       FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 
       ft.replace(R.id.activity_main_content_fragment, home); 
       ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);// it will anim while calling fragment. 
       ft.addToBackStack(null); // it will manage back stack of fragments. 
       ft.commit(); 
       break; 
      } 
     } 
    }); 
0

你打算從片段中打開片段,所以不要忘記getActivity()。

getActivity().getSupportFragmentManager() 
         .beginTransaction() 
         .replace(R.id.content_frame, new fragmentClass()) 
         .addToBackStack(null).commitAllowingStateLoss(); 

R.id.content_frame - >的幀佈局

fragmentClass的ID - >你第二片段

最後

commit()或commitAllowingStateLoss()

+0

我沒有得到那個框架佈局 –

+0

所以,要取代? –

+0

這是框架佈局,即對於FragmentActivity或sherlockFragmentActivity,您可能會給一些佈局文件。在該文件中,一個框架佈局應該在那裏以替換該框架佈局上的片段。你必須在這裏給這個ID –