【问题标题】:Problems with fragments and inflating from main activity碎片和主要活动膨胀的问题
【发布时间】:2014-01-30 17:11:03
【问题描述】:

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

}

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

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

}

【问题讨论】:

  • 请发res/layout/activity_main的内容

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


【解决方案1】:

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

希望对您有所帮助!

问候!

【讨论】:

  • 如果我需要使用 IOIOActivity 与我正在连接到我的 android 设备的电路板连接;我还能如何让它实现片段框架?
  • 啊我明白了;对困惑感到抱歉。我曾以为 FragmentActivity 和 Fragment 是一回事。感谢您的澄清。如果我有代表,我会投票给你 :)
  • 当然,FragmentActivity 只是一个支持库,可以在您的活动中使用 Fragments,但不是它本身的“Fragment”,所以它们是不同的。如果此回复有帮助,请将其标记为正确,以便人们花时间回答您未来的问题。问候
【解决方案2】:

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

查看this 以了解 Fragments 的工作原理...

【讨论】:

  • 没问题 ;-) 我建议阅读 Reto Meier 的“Professional Android 4 Applications Development”或 CommonsWare“The Busy Coder's Guide to Android Development”。值得投资一点,因为这些都是很好的学习资源。快乐编码!
猜你喜欢
  • 2016-09-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多