Android Fragment无法解析add方法

时间:2015-08-17 11:15:12

标签: android android-fragments android-support-library fragmentmanager

我对Android开发有点新意。我一直在观看Udemy的视频,似乎有些代码已经过时,导致错误。当我尝试将我的Pl​​aceholderFragment对象添加到事务时,它会说“无法解析方法添加..”,除此之外,在else语句中它显示“不兼容的类型”。然而 taskFragment 对象继承自Fragment类, findFragmentByTag 返回 Fragment 类型。这是代码的结构。

这里我尝试使用getSupportFragmentManager方法开始事务。

 @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
        taskFragment = new PlaceholderFragment();
        getSupportFragmentManager().beginTransaction().add(taskFragment, "MyFragment").commit();

    } else {
        taskFragment = getSupportFragmentManager().findFragmentByTag("MyFragment");
    }
    taskFragment.startTask();
}

我有一个扩展Fragment的类:

public static class PlaceholderFragment extends Fragment {

    TechCrunchTask downloadTask;

    public PlaceholderFragment() {
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        setRetainInstance(true);
    }

    public void startTask() {
        if (downloadTask != null)
            downloadTask.cancel(true);
        else {
            downloadTask = new TechCrunchTask();
            downloadTask.execute();
        }
    }

}

TechCrunchTask类是这样的。 'L'是一个自定义类,用于记录消息并显示toasts。我认为这无关紧要。

 public static class TechCrunchTask extends AsyncTask<Void, Void, Void> {
    @Override
    protected Void doInBackground(Void... params) {
        String downloadURL = "http://http://feeds.feedburner.com/techcrunch/android?format=xml";
        try {
            URL url = new URL(downloadURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.setRequestMethod("GET");
            InputStream inputStream = connection.getInputStream();
            ProcessXML(inputStream);
        } catch (Exception e) {
            L.m(e + "");
        }
    }

这是ProcessXML方法:

  public void ProcessXML(InputStream inputStream) throws Exception {
        DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
        Document xmlDocument = documentBuilder.parse(inputStream);
        Element rootElement = xmlDocument.getDocumentElement();
        L.m("" + rootElement.getTagName());

    }

以下是导入包的列表:

import android.app.Activity;
import android.app.Fragment;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.AppCompatActivity;
import android.view.Menu;
import android.view.MenuItem;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.xml.sax.Parser;
import org.xml.sax.SAXException;

import java.io.IOException;
import java.io.InputStream;
import java.lang.annotation.ElementType;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

这是build.gradle的内部

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:+'
}

我想这是关于支持类的版本。如你所见,我的是v7。如何解决这些问题。谢谢你的帮助!

1 个答案:

答案 0 :(得分:1)

@Courtesy To Md

使用import android.support.v4.app.Fragment代替import android.app.Fragment

android.support.v4.app.Fragment是Android支持库中的Fragment类,它是一个兼容包,允许您在旧版Android上使用Android的一些较新功能。Reference

请在您的gradle依赖项中添加此内容

compile 'com.android.support:support-v4:22.1.1'