Does the app name belong in the "import R" line? - android

I had to explicitly add my "import R" to my main activity's source.
However, when I run the app nothing displays in the Emulator to let me know my app is running. I'm wondering if the "import" I added is wrong.
Assuming my package is named com.goSitOnAPotatoPanOtis.This.
should it be this:
import com.goSitOnAPotatoPanOtis.R;
or this:
import com.goSitOnAPotatoPanOtis.This.R;
?

From my experience you should not have to add an import *.R class, and it will actually cause problems when building. That class should be available to your activities already.

The R.java class is generated in the application package as defined by the manifest package attribute:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.goSitOnAPotatoPanOtis" android:versionCode="1"
android:versionName="1.00">
If your activity is in that package you do not need to import R.java because of Java package visibility. If your activity is in another package you need to import it with
import com.goSitOnAPotatoPanOtis.R;
Even if you import the R class when it is not necessary it should not cause your application to stop running.

It should be import com.goSitOnAPotatoPanOtis.R; you can then reference that R using just the R.id.some_id or whatever you are trying to reference from the generated R class.

Related

import R package without product package name

project: qt5.10/android/qml
I need to use the icon from the resources - R. drawable.icon
Code below is successfully built. But it uses the name of the application package.
import myapp.foobar.com.R;
...
.setSmallIcon(R.drawable.icon)
I would like to import an R package without being bound to the application package name. Is that possible?
Or maybe there is a way to access resources without importing the R package?
Or import R-package by specifying the name of the main package indirectly? something like this: import auto.R
No, its not possible...R will always consist of Package name with starting

R.layout Android 2.2.3

Why am i having to go all the way in my class so he can recognize this layout i created? Because it simply does not recognize as R.layout.my_layout
OK:
super(context, com.example.leonardoinhoqui.tcc2.R.layout.my_layout);
Problem:
super(context,R.layout.my_layout);
Most probably you have a wrong import in your code (this sometimes happen when Android Studio resolves imports)
import android.R.*;
and because of that you have to provide the full name of your own R package

Cannot resolve symbol R when move class out of default package

When my "ExampleAdapter" class is in the sample package as my MainActivity, I don't get an error. Let's say my MainActivity is in "com.example". When I create a new package inside of the "com.example" package (let's say "com.example.adapters"), and move my "ExampleAdapter" to the new package, I get the dreaded error, "Cannot resolve symbol 'R'". Why does it matter which package my adapter is in?
Why does it matter which package my adapter is in?
The R and BuildConfig classes are always code-generated into the Java package that you name in the package attribute of your <manifest> element in your AndroidManifest.xml file. Any Java classes outside of that package that need to refer to R or BuildConfig need to add the corresponding import statement, as with any other Java class from a foreign package.

Cannot get reference to layouts and id in res. folder

This question has been up a couple of times but still I haven't found an answer that helped me.
1:st one:
I cannot find any of my R.Layout.activity_article_detail and other layout xml files through reference. It stopped working after did a "clean project" in Eclipse and tried to build it up again.
I've tried removing and adding and removing the .R import but right now it wont find any of them. My layout files doesn't include a Capital letter, are spelled correcly.
code:
package martin.larsson.kopingsrssreader;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.NavUtils;
import android.view.MenuItem;
import martin.larsson.kopingsrssreader.R;
public class ArticleDetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_article_detail);
Second problem is it cant find my res.menu either. I got 2 files in it. detailmenu.xml and refreshmenu.xml. In both i cant find any of the id tags
<item android:id="#+id/actionbar_markunread"
<item android:id="#+id/actionbar_saveoffline"
<item android:id="#+id/actionbar_refresh"
Does anyone got a clue?
SOLUTION: Uncheck "build automatically" and do a "clean project", and a Eclipse restart.
Clean your project or restart the eclipse.
Look to the imports, the R imported maybe is not correct.
If anything is wrong in your layouts, new updates aren't added to the R file. Look at the errors panel, and possibly to a clean rebuild to see what errors are popping up.
if u have an error in any of the resource files R will show up red unless all Errors are fixed and the project is built

Looking for wrong fragment class with Android compatibility library

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.

Categories

Resources