2014-01-30 35 views
0

我试图用IOIO(有点像arduino)框架设置一个碎片化的应用程序; 问题是,当我将IOIOActivity.java类的扩展从扩展活动更改为扩展FragmentActivity,然后围绕该IOIOFragmentActivity生成我收到错误的类时。碎片和主要活动膨胀的问题

这是我的错误: * fragmentTransaction.add(R.layout.digitalioio,fragment); **在类型FragmentTransaction的方法Add(INT,片段)是不适用的参数(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(); 
} 

} 

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

} 

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

} 
+0

如果我需要使用IOIOActivity与董事会,我布线到Android接口请张贴的'RES /布局/ activity_main' – Karakuri

回答

1

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

希望这有助于!

问候!

+0

内容设备;我怎么能让它实现片段框架? – Tukajo

+0

只需从你的类中的片段扩展DigIOIOFrag ... –

+0

啊我现在看到;对困惑感到抱歉。我曾认为FragmentActivity和Fragment是一回事。感谢您的澄清。我会投你一票,如果我有代表:) – Tukajo

0

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

看一看this了解片段是如何工作的?

+0

没问题;-)我会推荐阅读Reto Meier的“专业Android 4应用程序开发”或CommonsWare“繁忙的编码器Android开发指南”。这是值得投资的,因为这些都是很好的学习资源。快乐的编码! – 2Dee