-1

出于学习目的,我正在制作一个应用程序..当我在按下屏幕上的任何按钮之前更改我的方向时..它成功更改了方向并显示了我添加到“layout-land”文件夹中的相应布局......

但是当我按下按钮显示下一个片段,然后切换方向..我希望活动重新启动但我的应用程序崩溃......在这里我需要帮助..

调试器不显示抛出任何异常的行

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;

    }
}

请注意,当我使用空构造函数创建不同的片段类并直接向 inflate() 函数提供 int XML 资源时,问题已解决

Taimoor Ali , 谢谢

日志猫

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
4

2 回答 2

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);
}

这样构造函数仍然是空的,但你正在传递你需要的字符串,另见这个问题:

片段真的需要一个空的构造函数吗?

于 2013-07-09T19:01:31.830 回答
1

对于片段,您必须有一个空的构造函数(没有参数)或没有(默认)。如果要传递参数,请使用参数创建 aBundle并使用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?

于 2013-07-09T19:03:05.653 回答