繁体   English   中英

主要活动造成碎片和膨胀的问题

[英]Problems with fragments and inflating from main activity

我正在尝试使用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;
}

}

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

希望这可以帮助!

问候!

根据文档 ,DigIOIOFrag需要扩展Fragment FragmentFragmentActivity是2个不同的类。

看看这个,以了解Fragments是如何工作的...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM