I am trying to inflate a layout containing a Fragment using the backwards compatibility package and SDK level 10. I took the jar file and placed it in the libs folder of my project. I extended FragmentActivity.
It all works perfectly when I run at API level 11 on an XLarge screen device.
When I drop back to compiling against level 10, and running on a normal sized screen, I get failure at the point where it create a new Activity and inflate the fragment in it.
Caused by:
java.lang.ClassNotFoundException:
android.view.fragment in loader
dalvik.system.PathClassLoader[/data/app/com.motoappsummitagenda-1.apk]
04-01 01:07:14.311 2870 2870 E
AndroidRuntime: at
dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
So it looks like something, somewhere is looking for android.view.fragment, and not the compatibility version, android.support.v4.app.Fragment. Of course, android.view.fragment won't be found on API level 10. But where is that android.view.fragment coming from?
The XML that is being inflated is
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.motorapp.SessionFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/sessionfragment">
</fragment>
The code for it starts:
package com.motorapp;
import java.util.List;
import java.util.ListIterator;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.ScrollView;
import android.widget.TextView;
import android.support.v4.app.*;
import android.support.v4.util.*;
import android.support.v4.*;
import android.support.v4.widget.*;
public class SessionFragment extends android.support.v4.app.Fragment {
The symptoms are similar to this problem, but I don't have those mistakes
inflating fragments with compatibility package android
The android.compatibility.v4.android-support-v4 jar file is on my build path in Eclipse
under Project > Properties > Java build path
I have in the manifest (and I have tried different variations of the name).
In getting started with fragments, I first used API 11 on a honeycomb device, and that all works perfectly. It is in getting the same code (modified to use the slightly different compatibility API) working with the compatibility library that I have this problem. Any ideas would be very welcome. Thanks,
Peter
I just solved this problem in Android API 8 machine (Samsung Galaxy S).
Please change Activity class to FragmentActivity.
public class FragmentLayout extends Activity {}
-->
public class FragmentLayout extends FragmentActivity {}
public static class DetailsActivity extends Activity {}
-->
public static class DetailsActivity extends FragmentActivity {}
finally
getFragmentManager() --> getSupportFragmentManager()
register android-support-v4.jar to Eclipse's referenced Libraries
put android-support-v4.jar to {root directory of your project}/libs directory
change to API 10 enum (ex:simple_list_item_1) from simple_list_item_activated_1
import android.support.v4.app.Fragment;
I had the same wrong class problem with the compatibility library and tried everything mentioned above with no luck. I finally figured it out: I was using the incorrect XML element Fragment rather than correct one fragment. Capitalization seems to matter.
I bet you are trying to call setConetentView before super.onCreate in your FragmentActivity
I had exactly this problem with an app running on pre-Honeycomb devices. I based my Fragment coding on the example given in Dianne Hackborn's February 2011 blog post and other standard Fragment reference material. The solution to the problems is contained in the SDK reference link, http://developer.android.com/sdk/compatibility-library.html#Using. There are minor differences in class and method names that must be used in apps with Fragments, when those apps are to be deployed with the Compatibility library. The link explains this.
The compatibility library, as downloaded and installed, includes example code using Fragments. Ref: Android Developers > Resources > Sample Code > API 4+ Support Demos
The imports that you have provided all look correct. Do double check any other classes to make sure Eclipse didn't auto import the wrong Fragment some where. The other thing that is key is you need to add the compatibility library to a lib\ that you must create at your project's root to make sure it gets bundled with your app. I don't know if it is required, but I always have my project build path refer to it's copy.
Your layout seems to be missing the class parameter. I think you meant to use class where you have used name in your layout.
<fragment class="com.example.<class to path to your fragment>" android:id="#+id/items"
android:layout_weight="1" android:layout_width="0px"
android:layout_height="fill_parent" />
In Eclipse you should see android-support-v4.jar under both Referenced Libraries and libs.
I think you should delete android-support-v4.jar from build path. This will remove it from referenced libraries in Eclipse. Than creace folder libs and put the jar in there. Clean your project and Eclipse will add the jar to Android dependencies. This did the trick for me as I was struggling with class not found exception.
I got the
java.lang.NoClassDefFoundError: com.android.example.SupportFragment
at com.android.example.SupportFragmentActivity.onCreate()
on
SupportFragment extends SherlockFragment implements PopupMenu.OnMenuItemClickListener
...
#Override
public boolean onMenuItemClick(android.view.MenuItem item) {
return onOptionsItemSelected(item);
}
when trying to make a api 17 app compatible with api 8, the only indication was the logcat error above, so check that all your imported classes are supported if you get this error.
Related
My Android Studio Project (migrated to AndroidX) (TheInstrumentals) automatically imports android.app.Fragment whenever I make a Fragment from Android Studio's Fragment Wizard. How can I make it auto-import androidx.fragment.app.Fragment?
I've tried making a new Blank Fragment from another project (ViewPagerTest) (also migrated to AndroidX) and it imports androidx.fragment.app.Fragment automatically instead of the deprecated android.app.Fragment.
My other project (Udacity Project) (not in AndroidX) automatically imports android.support.v4.app.Fragment when the same thing was done.
com.mydomain.theinstrumentals.BlankFragment.java:
package com.mydomain.theinstrumentals;
import ...
import android.app.Fragment; //imported automatically
import ...
public class BlankFragment extends Fragment /*Fragment was strikedthrough */ {...}
com.mydomain.viewpagertest.BlankFragment.java:
package com.mydomain.viewpagertest;
import androidx.fragment.app.Fragment; //imported automatically
import ...
public class BlankFragment extends Fragment {...}
com.example.android.miwok.NumbersFragment:
package com.example.android.miwok.NumbersFragment;
import ...
import android.support.v4.app.Fragment; //imported automatically
import ...
public class NumbersFragment extends Fragment {...}
My answer to the linked post isn't old - the imports are still alphabetical and the options to auto import are modifiable in the settings
Short answer - AndroidX is extra, and later on the classpath, not on the core API, therefore it's listed later in the import menu
Plus, the Fragment wizard uses internal template files for creating new classes for all components, and can't assume that you have AndroidX as part of your code. If it did, then it would have to insert those plugins into the Gradle build, for example
So when i create a new android application project with ANDROID 6.0 library i instantly get the a lot of errors in the MainActivity code.
Starting with The import android.support.v7.app cannot be resolved
To The method onCreate(Bundle) of type MainActivity must override or implement a supertype method
When i add the appcompat.v7 library obviously Some of the errors related are gone but other are coming instead. Like:
The type android.support.v4.widget.DrawerLayout$DrawerListener cannot be resolved. It is indirectly referenced from required .class files on the package line.
And still The method onCreateOptionsMenu(Menu) of type MainActivity must override or implement a supertype method.
Ok. So add the v4 jar too. Ok. I added it too to the project properties and no errors!
Wait,but when i run the app now I get a ClassNotFoundException.
So what the hell android 6.0???
Thanks for the help.
EDIT
MainActivity.java:
package com.minyan.get.dl;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
The main reason is that Android Studio is the main IDE now.
Eclipse is no longer supported.
I suggest switching to Android Studio.
We are trying to use the Robolectric testing framework in Android Studio in order to test the Facebook API. The Facebook Login button works so the Facebook API is working. However, the following test fails:
package com.airportapp.test.Models;
import android.app.Activity;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.annotation.Config;
import org.robolectric.Robolectric;
import com.airportapp.test.MyRobolectricTestRunner;
import com.airportapp.LoginActivity;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
#RunWith(MyRobolectricTestRunner.class)
public class LoginActivityTest {
#Before
public void setup() {
//do whatever is necessary before every test
}
#Test
public void testActivityFound() {
Activity activity = Robolectric.buildActivity(LoginActivity.class).create().get();
Assert.assertNotNull(activity);
}
}
And the error is that Android Studio could not find the android.support file when we run the tests. You can see the error here:
The other error that shows up is:
android.view.InflateException: XML file app/src/main/res/layout/activity_login.xml line #-1 (sorry, not yet implemented): Error inflating class com.facebook.widget.LoginButton
So Android Studio is not happy with the facebook login button as well :( But it works... We think that we need to import something, but we don't know where to put it.
The InflateException is because Robolectric cannot find the resources from the Facebook SDK. To solve this, the project.properties file has to be updated to point to the Facebook SDK project. Do this by adding the following line to it:
android.library.reference.1={Path}
{Path} should be a relative path, from the project.properties file to the folder containing the AndroidManifest.xml of the Facebook SDK project. In my case that is ../../build/intermediates/exploded-aar/com.facebook.android/facebook/3.21.0.
Note that this works for all Android Library Projects which contain resources that aren't found by Robolectric. Further reading: here and here. Also, more documentation about project.properties can be found here. Note that in my project the Robolectric tests are located in the Android app project itself. So you could also try placing the project.properties file in the same directory as the AndroidManifest.xml used for testing.
As for your first problem; I have no personal experience with it. But it appears to be because Gradle cannot find the support libraries when compiling the unit tests. The answer from this question should fix it.
With the new SDK Tools and ADT versions 22.6.2, the default options for creating a new project with a blank activity produces a project which uses Fragments and refers to a library project (a new one for each project) named appcompat_v7_x.
The relevant parts of the stub code for the main activity are:
package com.myname.miniandroid;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
.....
.....
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
....
}
public static class PlaceholderFragment extends Fragment {
......
......
}
}
.
We can see that it uses a Fragment and an ActionBarActivity. The automatically created library project has both the v4 and the v7 jars in its libs folder, the main project has just the v4 jar. As built by default, hovering over either of these classes gives no javadoc information.
To get such support I've had to :
1) insert a file android-support-v7-appcompat.jar.properties into the libs folder of my automatically created appcompat_v7_4 library project. It contains the lines:
doc=c:\\dev\\tools\\android-sdk-windows4.4\\docs\\reference
src=C:\\dev\\tools\\android-sdk-windows4.4\\extras\\android\\support\\v7\\appcompat\\src
(Both lines seem to be necessary)
2) insert a file anndroid-support-v4.jar.properties into the libs folder of the main project. It contains the line:
src=C:\\dev\\tools\\android-sdk-windows4.4\\extras\\android\\support\\v4\\src
3) Close and reopen both projects and clean all projects.
I've got a brand new installation of Eclipse Kepler and an up to date installation of the SDK.
I feel that there must be a quicker way of getting set up to start a new project with the recommended default options. If anyone can tell me what it is, I would be most grateful.
You have use the "New" button in the upper left corner, select Android Application Project, choose the name, icon...
When you have done, you should have a working "Hello world" app.
If you want to add external libraries this is the answer: https://stackoverflow.com/a/3643015/3203988
I have a class set up as an EViewGroup. In it, I'm trying to reference another Activity set up as an EActivity. For some reason, the import isn't resolving, but it resolves fine if I reference the Activity in question from another EActivity.
The code compiles fine using ant on our Jenkins server, but doesn't compile within Ecipse.
Any ideas?
Here's some code for ViewGroup:
import android.app.Activity;
import android.content.Intent;
import com.googlecode.androidannotations.annotations.Click;
import com.googlecode.androidannotations.annotations.EViewGroup;
#EViewGroup
public class MainMenu extends SlidingMenu
{
public void navigate(Class<? extends Activity> klass)
{
getContext().startActivity(new Intent(getContext(), klass));
toggle();
}
#Click(R.id.textView_bring_it)
public void bringItClick()
{
navigate(ActivityBringIt_.class);
}
}
The compile errors I'm seeing:
The import com.beachbody.p90x.bringit.ActivityBringIt_ cannot be
resolved
The method navigate(Class) in the type MainMenu
is not applicable for the arguments (Class)
ActivityBringIt_ cannot be resolved to a type
Here is my .factorypath file:
<factorypath>
<factorypathentry kind="WKSPJAR" id="/common/compile-libs/androidannotations-2.7.1.jar" enabled="true" runInBatchMode="false"/>
</factorypath>
It seems that ActivityBringIt_ isn't generated. Your compiling errors are just noises because of that.
Mostly AA can't generate subclasses if you have errors in you Android's xml files.
You should take a look on this.
Also, could you copy/paste your .factory file ?
Thanks DayS for pointing me in the right direction. There is a bug in Eclipse causing this issue. The workaround is to import the entire package in question. In my case it is:
import com.beachbody.p90x.bringit.*;
Here is the bug report w/ Eclipse:
https://bugs.eclipse.org/bugs/show_bug.cgi?id=387956