gpt4 book ai didi

java - fragment 问题和主要 Activity 的膨胀问题

转载 作者:行者123 更新时间:2023-12-01 13:32:53 25 4
gpt4 key购买 nike

我正在尝试使用 IOIO(有点像 arduino)框架设置一个 fragment 应用程序;问题是,当我将 IOIOActivity.java 类的扩展从扩展 Activity 更改为扩展 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();
}

}

我的主要 Activity :

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 是支持 Activity 库能够使用 getSupportedFragmentManager,但这并不是“Fragment”本身...

希望这有帮助!

问候!

关于java - fragment 问题和主要 Activity 的膨胀问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21462990/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com