0

When I try Generate Signed APK Wizard in Android Studio 1.0 , I have a Error.

Error: This class should provide a default constructor (a public constructor with no arguments) (com.ex.Tab1_2_1) [Instantiatable]

There is my code.

It works in Eclipse and I wonder why it doesn't work in Android Studio.

public class Tab1_2_1 extends Fragment{

Context mContext;
WebView mWebView;
View mView;

public Tab1_2_1(Context context)
{
    mContext = context;
}

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

@Override
public View onCreateView(LayoutInflater inflater, 
        ViewGroup container, Bundle savedInstanceState) 
{
    mView = inflater.inflate(R.layout.webview, null);

    return mView;
}

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

   mWebView = (WebView)mView.findViewById(R.id.webview);
   mWebView.loadUrl("file:///android_asset/tab1_2/tab1_2_1.html"); 
   mWebView.getSettings().setJavaScriptEnabled(true);
}
}

Have a nice day.

gio
  • 4,950
  • 3
  • 32
  • 46
kohgsu
  • 15
  • 1
  • 3
  • solution work for me [check this](http://stackoverflow.com/questions/17420637/error-non-default-constructors-in-fragments/39608360#39608360) – Naeem Ibrahim Sep 21 '16 at 06:00

2 Answers2

1

From official doc Fragment

All subclasses of Fragment must include a public no-argument constructor. The framework will often re-instantiate a fragment class when needed, in particular during state restore, and needs to be able to find this constructor to instantiate it. If the no-argument constructor is not available, a runtime exception will occur in some cases during state restore.

You do not need to pass context into it. You can always get context by use getActivity() method

gio
  • 4,950
  • 3
  • 32
  • 46
0

yes, Fragments subclass are required to have an empty constructor. You should delete

public Tab1_2_1(Context context)
{
    mContext = context;
}

also, having a constructor just to provide a context object is not useful at all, since Fragment has the getActivity method which returns the activity the hosts your Fragment

Blackbelt
  • 156,034
  • 29
  • 297
  • 305