سؤال

I am trying to set up a fragmented application with the IOIO (sort of like arduino) framework; The problem is when I changed the extension of the IOIOActivity.java class from extending activity to extending FragmentActivity and then I build my classes around that IOIOFragmentActivity I receive errors.

Here is my error: * fragmentTransaction.add(R.layout.digitalioio, fragment); **The method add(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, DigIOIOFrag)

Here is some of the code:

Original IOIOActivity Class:

package ioio.lib.util.android;

import ioio.lib.impl.SocketIOIOConnection;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.IOIOLooperProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;

public abstract class IOIOActivity extends Activity implements
    IOIOLooperProvider {
private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(
        this, this);

/**
 * Subclasses should call this method from their own onCreate() if
 * overloaded. It takes care of connecting with the IOIO.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    helper_.create();
}

/**
 * Subclasses should call this method from their own onDestroy() if
 * overloaded. It takes care of connecting with the IOIO.
 */
@Override
protected void onDestroy() {
    helper_.destroy();
    super.onDestroy();
}

/**
 * Subclasses should call this method from their own onStart() if
 * overloaded. It takes care of connecting with the IOIO.
 */
@Override
protected void onStart() {
    super.onStart();
    helper_.start();
}

/**
 * Subclasses should call this method from their own onStop() if overloaded.
 * It takes care of disconnecting from the IOIO.
 */
@Override
protected void onStop() {
    helper_.stop();
    super.onStop();
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
        helper_.restart();
    }
}

/**
 * Subclasses must either implement this method or its other overload by
 * returning an implementation of {@link IOIOLooper}. A dedicated thread
 * will be created for each available IOIO, from which the
 * {@link IOIOLooper}'s methods will be invoked. <code>null</code> may be
 * returned if the client is not interested to create a thread for this
 * IOIO. In multi-IOIO scenarios, where you want to identify which IOIO the
 * thread is for, consider overriding
 * {@link #createIOIOLooper(String, Object)} instead.
 * 
 * @return An implementation of {@link IOIOLooper}, or <code>null</code> to
 *         skip.
 */
protected IOIOLooper createIOIOLooper() {
    throw new RuntimeException(
            "Client must override one of the createIOIOLooper overloads!");
}

@Override
public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
    return createIOIOLooper();
}

}

My IOIOFragmentActivity class:

package ioio.lib.util.android;

import ioio.lib.impl.SocketIOIOConnection;
import ioio.lib.util.IOIOLooper;
import ioio.lib.util.IOIOLooperProvider;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

public abstract class IOIOFragmentActivity extends FragmentActivity implements
    IOIOLooperProvider {
private final IOIOAndroidApplicationHelper helper_ = new IOIOAndroidApplicationHelper(
        this, this);

/**
 * Subclasses should call this method from their own onCreate() if
 * overloaded. It takes care of connecting with the IOIO.
 */
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    helper_.create();
}

/**
 * Subclasses should call this method from their own onDestroy() if
 * overloaded. It takes care of connecting with the IOIO.
 */
@Override
protected void onDestroy() {
    helper_.destroy();
    super.onDestroy();
}

/**
 * Subclasses should call this method from their own onStart() if
 * overloaded. It takes care of connecting with the IOIO.
 */
@Override
protected void onStart() {
    super.onStart();
    helper_.start();
}

/**
 * Subclasses should call this method from their own onStop() if overloaded.
 * It takes care of disconnecting from the IOIO.
 */
@Override
protected void onStop() {
    helper_.stop();
    super.onStop();
}

@Override
protected void onNewIntent(Intent intent) {
    super.onNewIntent(intent);
    if ((intent.getFlags() & Intent.FLAG_ACTIVITY_NEW_TASK) != 0) {
        helper_.restart();
    }
}

/**
 * Subclasses must either implement this method or its other overload by
 * returning an implementation of {@link IOIOLooper}. A dedicated thread
 * will be created for each available IOIO, from which the
 * {@link IOIOLooper}'s methods will be invoked. <code>null</code> may be
 * returned if the client is not interested to create a thread for this
 * IOIO. In multi-IOIO scenarios, where you want to identify which IOIO the
 * thread is for, consider overriding
 * {@link #createIOIOLooper(String, Object)} instead.
 * 
 * @return An implementation of {@link IOIOLooper}, or <code>null</code> to
 *         skip.
 */
protected IOIOLooper createIOIOLooper() {
    throw new RuntimeException(
            "Client must override one of the createIOIOLooper overloads!");
}

@Override
public IOIOLooper createIOIOLooper(String connectionType, Object extra) {
    return createIOIOLooper();
}

}

My MainActivity:

package com.example.ioiocontrol;

import ioio.lib.util.android.IOIOFragmentActivity;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.Menu;

public class MainActivity extends IOIOFragmentActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    FragmentManager fragmentManager = getSupportFragmentManager();
            FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
            DigIOIOFrag fragment = new DigIOIOFrag();
            fragmentTransaction.add(R.layout.digitalioio, fragment);
            fragmentTransaction.commit();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

}

My DigIOIOActivity (Class that is trying to inflate within the main layout):

package com.example.ioiocontrol;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import ioio.lib.util.android.IOIOFragmentActivity;
import android.support.v4.app.FragmentActivity;

public class DigIOIOFrag extends IOIOFragmentActivity{

public View onCreateView(LayoutInflater viewInflation, ViewGroup container, Bundle      SavedInstantState){
    View viewOne;
    viewOne = viewInflation.inflate(R.layout.digitalioio, container,false);
    return viewOne;
}

}
هل كانت مفيدة؟

المحلول

Your class DigIOIOFrag extends from FragmentActivity and its supposed to extend from Fragment, that's why the compiler is complaining,is expecting the arguments(int, Fragment), and you are passing(int, FragmentActivity), NOTICE that FragmentActivity is the support library for activities to be able to make use of getSupportedFragmentManager, but that's not the "Fragment" it self...

Hope this Helps!

Regards!

نصائح أخرى

As per the documentation, DigIOIOFrag needs to extend Fragment. Fragment and FragmentActivity are 2 different classes.

Have a look at this to understand how Fragments work ...

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top