Thilina Dinith Fonseka
Thilina Dinith Fonseka

Reputation: 644

Used Proteus in an Android Fragment, but getting an error while casting inflater - Proteus Android Layout Engine

Please check my code for this:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    JSONObject jsonlayout = null ;
    try {
        jsonlayout = new JSONObject(loadJSONFromAsset(getContext()));
    } catch (JSONException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    ProteusLayoutInflater layoutInflater = (ProteusLayoutInflater)inflater;
    ProteusView view = layoutInflater.inflate(jsonlayout.toString(), null, container, 0);
    return view.getAsView();
}

I want to use the Proteus library to create a view and render it as a fragment.

Can you please tell me what I did wrong? With this code I'm getting the following error:

com.android.internal.policy.PhoneLayoutInflater cannot be cast to com.flipkart.android.proteus.ProteusLayoutInflater

Upvotes: 0

Views: 761

Answers (1)

vader
vader

Reputation: 909

Here is some sample code for the Fragment that should get you started:

remember to add gson-adapter to the build.gradle file of your app.

dependencies {
    compile 'com.github.flipkart-incubator.proteus:gson-adapter:5.0.0-rc12'
}

In your Fragment add and modify according to the following:

  // some private fields in the fragment
  private Proteus proteus;
  private ProteusContext context;
  private ProteusLayoutInflater layoutInflater;
  private Gson gson;

  public void onAttach (Activity activity) {
    // create a new instance of the proteus type adapter 
    // for gson and register it with gson. 
    ProteusTypeAdapterFactory adapter = new ProteusTypeAdapterFactory(activity);
    gson = new GsonBuilder().registerTypeAdapterFactory(adapter).create();

    // create a new instance of proteus from the builder
    proteus = new ProteusBuilder().build();
    // get a new context object from proteus
    context = proteus.createContextBuilder(activity).build();

    // this context object has the proteus layout inflater
    layoutInflater = context.getInflater();

    // set the instance of proteus 
    ProteusTypeAdapterFactory.PROTEUS_INSTANCE_HOLDER.setProteus(proteus);
  }

  public ProteusContext getContext() {
    return context;
  }

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    String layoutString = loadJSONFromAsset(getContext());

    // use gson to deserialize the string into a Layout object.
    Layout layout = gson.fromJson(layoutString, Layout.class);

    // the use the proteus layout inflater to inflate a new proteus view  
    ProteusView view = layoutInflater.inflate(layout, new ObjectValue());
    return view.getAsView();
  }

This is essentially a working example with a lot of space for enhancement and improvement. You should head to the git repo, clone it, play around with the demo app and check out the code for ProteusActivity for a reference implementation.

Upvotes: 1

Related Questions