Gonenc Turkekul
Gonenc Turkekul

Reputation: 285

Android Fragment cannot resolve add method

I'm kinda new on Android development. I've been watching videos from Udemy and it seems some of codes are outdated, which lead errors. When I try to add my PlaceholderFragment object to transaction, it says "Cannot resolve method add..",in addition to that, at the else statement it says "Incompatible types".However the taskFragment object is inheriting from the Fragment class and findFragmentByTag returns Fragment type. Here is the structure of code.

Here I try use getSupportFragmentManager method to begin transaction.

 @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();
}

I have a class which extends 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 class is like this. 'L' is a custom class which logs the messages and shows toasts. It is irrelevant I assume.

 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 + "");
        }
    }

This is the ProcessXML method:

  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());

    }

Here is the list of imported packages:

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;

And this is the inside of the build.gradle

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

I guess this is about the version of support class. Mine is v7 as you see. How these can be solved. Thanks for help!

Upvotes: 1

Views: 1712

Answers (1)

IntelliJ Amiya
IntelliJ Amiya

Reputation: 75788

@Courtesy To Md

Use import android.support.v4.app.Fragment instead import android.app.Fragment

android.support.v4.app.Fragment is the Fragment class in the android support library, which is a compatibility package that allows you to use some of the newer features of Android on older versions of Android.Reference

Please add this in your gradle dependencies

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

Upvotes: 1

Related Questions