2013-07-09 45 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 –

相关问题