Android snippets at lower API

Well, I'm starting to understand Android Fragments, but this is still confusing for me. I need a little help. As they say, Android fragments are supported from API level 11, but you can download the “support.v4” library package for the lower-level API. First I create a new project to try fragments at API level 15. Then I did the same with API level 8 and it doesn’t work ... I imported an external jar and it sees all the necessary imports as it should ... What seems to be the problem here?

Here is my main class:

import android.support.v4.app.FragmentActivity; import android.os.Bundle; public class Fragment2Activity extends FragmentActivity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } } 

My main XML format is:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" /> <fragment android:name="com.frag.Fragment2.frag" android:id="@+id/frag" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> 

My fragment class:

 import android.os.Bundle; import android.support.v4.app.Fragment; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; public class frag extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment View v = inflater.inflate(R.layout.hello_fragment, container, false); return v; } } 

And my XML fragment:

 <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <TextView android:layout_width="fill_parent" android:layout_height="match_parent" android:text="THIS IS FRAGMENT" android:background="@drawable/ic_launcher" /> </LinearLayout> 

Fixed

+4
source share
4 answers

You are still using the android.view.fragment class that does not exist. You should switch to android.support.v4.app.fragment on older devices.

+6
source

If you use a lower API due to minSdkVersion, you should know that you can use the latest sdk and set minSdkVersion to a lower value. They do not have to be the same.

0
source

OK, so I had the same problem, and finally it worked. First of all, make sure you have the latest ADT tools.

1) Go into every action that uses fragments and continue with FragmentActivity instead of Activity

2) if you did not import the support library into your assembly, then right-click your project-> Android Tools-> Add Support Library

3) make sure that in your xml tags are <fragment /> and not <android.support.v4.app.fragment />

Hope this helps you, it took a while to get it to work.

0
source

Actually, this problem is related to ADT tools.

  • reorder Order and Export in Java Build Path by src> gen> Private> other ...

  • Add support library → android-support-v4.jar

0
source

Source: https://habr.com/ru/post/1411063/


All Articles