2013-07-09 42 views
-1

用於學習目的我正在製作應用程序..當我按下屏幕上的任何按鈕之前更改我的方向時,它成功更改方向並顯示我添加到「layout-land 「文件夾...應用程序崩潰作爲方向更改

但是,當我按下按鈕,顯示下fragement,然後切換方向。我期待的活動重新啓動,但我的應用程序崩潰......在這裏,我需要幫助..

調試劑量不顯示任何例外拋出線

MainActivity類

package com.example.prototype; 

import android.os.Bundle; 
import android.support.v4.app.FragmentActivity; 
import android.support.v4.app.FragmentTransaction; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.widget.Button; 

import com.example.prototype.fragments.FragmentOne; 



public class MainActivity extends FragmentActivity { 

    int fragShowButtonPressed =0; 
    boolean FragOneDisplayed=false; 
    boolean FragTwoDisplayed=false; 


public void fragment_switch(View v){ 

    Button showfrag1 = (Button)findViewById(R.id.buttonShowFrag1); 
    Button showfrag2 = (Button)findViewById(R.id.buttonShowFrag2); 
     showfrag1.setVisibility(showfrag1.INVISIBLE); 
     showfrag2.setVisibility(showfrag2.INVISIBLE); 

    FragmentTransaction ft = getSupportFragmentManager().beginTransaction(); 


    switch(v.getId()){ 

    case(R.id.buttonShowFrag1): ft.replace(R.id.frameLayout1, new FragmentOne("frag_1"), null); fragShowButtonPressed=1; FragOneDisplayed=true; FragTwoDisplayed=false; break; 

    case(R.id.buttonShowFrag2): ft.replace(R.id.frameLayout1, new FragmentOne("frag_2"), null); fragShowButtonPressed=1; FragOneDisplayed=false; FragTwoDisplayed=true; break; 


    } 

    ft.addToBackStack(null); 
    ft.commit(); 


} 


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

     Log.i("create", "Entered Oncreate"); 
     setContentView(R.layout.activity_main); 


     if(savedInstanceState != null){ 

      if(savedInstanceState.getInt("frag_button_pressed_stat")==1) 
      { 
       Button showfrag1 = (Button)findViewById(R.id.buttonShowFrag1); 
       Button showfrag2 = (Button)findViewById(R.id.buttonShowFrag2); 
       showfrag1.setVisibility(showfrag1.INVISIBLE); 
       showfrag2.setVisibility(showfrag2.INVISIBLE); 

       FragOneDisplayed=savedInstanceState.getBoolean("frag_one_display_stat"); 
       FragTwoDisplayed=savedInstanceState.getBoolean("frag_two_display_stat"); 



      } 


      else{} 





     } 



    } 


@Override 
protected void onSaveInstanceState(Bundle outState) { 

     if(fragShowButtonPressed ==1) 
     { 
      outState.putInt("frag_button_pressed_stat", fragShowButtonPressed); 
      outState.putBoolean("frag_one_display_stat", FragOneDisplayed); 
      outState.putBoolean("frag_two_display_stat", FragTwoDisplayed); 
     } 
    super.onSaveInstanceState(outState); 
} 

    @Override 
    public boolean onCreateOptionsMenu(Menu menu) { 

     getMenuInflater().inflate(R.menu.main, menu); 
     return true; 
    } 

} 

FragmentOne類

package com.example.prototype.fragments; 


import com.example.prototype.R; 

import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 

public class FragmentOne extends Fragment { 

    int id; 
    Context context; 

    public FragmentOne(String frag_name){ 

     if(frag_name=="frag_1") 
     { 
      id=R.layout.frag_1; 
     } 

     else if(frag_name == "frag_2"){ 

      id=R.layout.frag_2; 

     } 

     else{} 
    } 





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

     context = inflater.getContext(); 
     View v=inflater.inflate(id, container, false); 
     return v; 

    } 
} 

請注意,當我做出不同的片段班每個空的構造,並直接提供INT XML資源膨脹()函數問題解決

Taimoor Ali,Thankyou

LOG CAT

07-09 23:35:45.387: E/AndroidRuntime(948): FATAL EXCEPTION: main 
07-09 23:35:45.387: E/AndroidRuntime(948): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.prototype/com.example.prototype.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.prototype.fragments.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2205) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2240) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:3809) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.access$700(ActivityThread.java:139) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1266) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.os.Handler.dispatchMessage(Handler.java:99) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.os.Looper.loop(Looper.java:156) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.main(ActivityThread.java:4987) 
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.reflect.Method.invokeNative(Native Method) 
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.reflect.Method.invoke(Method.java:511) 
07-09 23:35:45.387: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
07-09 23:35:45.387: E/AndroidRuntime(948): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
07-09 23:35:45.387: E/AndroidRuntime(948): at dalvik.system.NativeStart.main(Native Method) 
07-09 23:35:45.387: E/AndroidRuntime(948): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.prototype.fragments.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.Fragment.instantiate(Fragment.java:405) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.FragmentState.instantiate(Fragment.java:97) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.FragmentManagerImpl.restoreAllState(FragmentManager.java:1767) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.FragmentActivity.onCreate(FragmentActivity.java:208) 
07-09 23:35:45.387: E/AndroidRuntime(948): at com.example.prototype.MainActivity.onCreate(MainActivity.java:44) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.Activity.performCreate(Activity.java:4538) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2161) 
07-09 23:35:45.387: E/AndroidRuntime(948): ... 12 more 
07-09 23:35:45.387: E/AndroidRuntime(948): Caused by: java.lang.InstantiationException: can't instantiate class com.example.prototype.fragments.FragmentOne; no empty constructor 
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.Class.newInstanceImpl(Native Method) 
07-09 23:35:45.387: E/AndroidRuntime(948): at java.lang.Class.newInstance(Class.java:1319) 
07-09 23:35:45.387: E/AndroidRuntime(948): at android.support.v4.app.Fragment.instantiate(Fragment.java:394) 
07-09 23:35:45.387: E/AndroidRuntime(948): ... 19 more 

回答

1

你的錯誤提示,你不必爲你的片段一個空的構造:

07-09 23:35:45.387:E/AndroidRuntime(948) :引發:java.lang.InstantiationException:無法實例化類com.example.prototype.fragments.FragmentOne;沒有空的構造函數

而且您將需要一個配置更改,因爲您的片段正在重建。

您需要實例以不同的方式在片段中的數據,例如:

public static final GridFragment newInstance(String tabId) 
{ 
    GridFragment f = new GridFragment(); 
    Bundle bdl = new Bundle(2); 
    bdl.putString(TAB_ID, tabId); 
    f.setArguments(bdl); 
    return f; 
} 


@Override 
public void onCreate(Bundle savedInstanceState) 
{ 
    String tabId = getArguments().getString(TAB_ID); 
    if (application.currentReport != null) 
    { 
     this.odTab = application.currentReport.getODTabByTabId(tabId); 
    } 
    else 
    { 
     startActivity(new Intent(getActivity(), LoginScrActivity.class)); 
    } 
    super.onCreate(savedInstanceState); 
} 

這樣的構造仍然是空的,但你通過你需要的字符串,也看到了這個問題:

Do fragments really need an empty constructor?

+0

任何替代方案因爲5個XML佈局,我不能做5個不同的片段類,必須有一種方法將它們組合在一個類中@Emil Adz –

+0

查看更新的答案。 –

+0

@TaimoorAli,我之前只發布了相同的確切代碼。它沒有幫助你? –

1

你必須有一個空構造函數(沒有參數)或沒有(默認)的片段。如果您想傳遞參數,請使用參數創建一個Bundle並使用setArguments

這通常是靜態創建方法來完成:

class MyFragment extends Fragment { 

public static MyFragment newInstance(String name) { 
    MyFragment f = new MyFragment(); 
    Bundle args = new Bundle(); 
    args.putString("name", name); 
    f.setArguments(bundle); 
    return f; 
} 
... 
} 

但在你的情況只是發了佈局的ID?

+0

任何鏈接從哪裏開始,你能告訴我如何這樣做是因爲iI有5種不同的XML佈局,我想使用類似的方法來改變片段....你能告訴我代碼..並感謝您顯示替代 –

+0

官方的Android開發者文檔是相當不錯的。以下是片段部分中的一個示例:http://developer.android.com/guide/components/fragments.html#Example 請注意,他們在DetailsFragment中如何使用該項目的id作爲參數來創建它。一般來說,你可能應該爲不同的佈局使用不同的片段,但很難說。取決於他們如何不同(或他們的目的)。祝你好運! –

+0

Ive added this View v = inflater.inflate(getArguments()。getInt(「id」),container,false); ...在我的activityclass方法中寫成FragmentOne frt = new FragmentOne(); frt.newinstance(R.layout.frag_1);現在我應該寫什麼作爲替換函數的第二個參數。這就是我寫的ft.replace(R.id.frameLayout1,frt,null); @Mattias –

相關問題