片段问题和主要活动的膨胀

时间:2014-01-30 17:11:03

标签: java android android-fragments extends layout-inflater

我正在尝试使用IOIO(有点像arduino)框架设置一个零碎的应用程序; 问题是当我将IOIOActivity.java类的扩展从扩展活动更改为扩展FragmentActivity然后我围绕IOIOFragmentActivity构建我的类时,我收到错误。

这是我的错误: * fragmentTransaction.add(R.layout.digitalioio,fragment); ** FragmentTransaction类型中的方法add(int,Fragment)不适用于参数(int,DigIOIOFrag)

以下是一些代码:

原始IOIOActivity类:

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

}

我的IOIOFragmentActivity类:

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

}

我的主要活动:

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

}

我的DigIOIOActivity(试图在主布局中膨胀的类):

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

}

2 个答案:

答案 0 :(得分:1)

您的类DigIOIOFrag从FragmentActivity扩展,它应该从Fragment扩展,这就是编译器抱怨的原因,期望参数(int,Fragment),并且您正在传递(int,FragmentActivity),注意FragmentActivity是支持用于能够使用getSupportedFragmentManager的活动的库,但这不是它自己的“片段”......

希望这有帮助!

问候!

答案 1 :(得分:0)

根据documentation,DigIOIOFrag需要扩展FragmentFragmentFragmentActivity是两个不同的类。

查看this以了解Fragments的工作方式......