What is the difference between specifying a class in these two ways? - java

I can run this code in my computer:
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
inside onResume(). I sent the code to someone else for testing. But they tell me that that this line shows an error in their SDK, and replacing it with
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
works, while in my case, both the codes work. Why is this happening?
PS: My SDK version is the one with the zip file name adt-bundle-windows-x86-20130522. I am not sure the exact version they are using, but it is newer than mine.

WindowManager is a class in the Android SDK. Its fully qualified name is android.view.WindowManager. The WindowManager.LayoutParams is a nested class of WindowsManager and its fully qualified name is android.view.WindowManager.LayoutParams
You can either specify the fully qualified name when you want to use it.
getWindow().addFlags(android.view.WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
or you can import its outer class
import android.view.WindowManager;
and use it directly
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

Related

How to move from a AbilitySlice to a Ability in HarmonyOS?

I am trying to move from an AbilitySlice to an Ability. I tried the below code,
But was it not working as expected.
Operation systemOperation = new Intent.OperationBuilder()
.withBundleName(getBundleName())
.withAbilityName(MainAbility.class.getSimpleName())
.build();
intent.setOperation(systemOperation);
startAbility(intent);
for moving from an AbilitySlice to Ability in Harmony OS?
Try to Delete Simple from getSimpleName. Like following:
Operation systemOperation = new Intent.OperationBuilder()
.withBundleName(getBundleName())
.withAbilityName(MainAbility.class.getName())
.build();
intent.setOperation(systemOperation);
startAbility(intent);
"Not working as expected" generally is not a valid error description. I'd suspect the AbilitySlice might belong to MainAbility and the whole operation might therefore be pointless, as navigation from A to B could not happen. The example which #Gowtham provided has one small difference (which appears to consider the device on which to launch the Intent with Super Device):
.withDeviceId("")
Have you ever tried to start anything else but MainAbility?
I see that you have mentioned that the package name of your target ability is different than the package name of your ability slice in your reply to #Martin.
Then, you need to make sure the bundleName(or package name) specified in the Intent's Operation builder is having the target ability's package name and not the calling ability's/abilityslice's package name.
Operation systemOperation = new Intent.OperationBuilder()
.withBundleName("enter_package_name_of target_ability_here")
.withAbilityName(MainAbility.class.getName())
.build();
intent.setOperation(systemOperation);
startAbility(intent);

PeerConnectionFactory in Android Studio 2 has no methods

After adding compile 'org.webrtc:google-webrtc:1.0.+' to my build.gradle file I try to init PeerConnectionFactory, but this class has no any useful methods.
What am I doing wrong?
UPDATE:
enter image description here
The last version org.webrtc:google-webrtc:1.0.21217
You can init by following codes
PeerConnectionFactory.InitializationOptions.Builder optionBuilder =
PeerConnectionFactory.InitializationOptions.builder(/* Put context here */);
optionBuilder.setEnableInternalTracer(true);
optionBuilder.setFieldTrials("WebRTC-FlexFEC-03/Enabled/");
optionBuilder.setEnableVideoHwAcceleration(true);
PeerConnectionFactory.initialize(optionBuilder.createInitializationOptions());
First, try to use an specific version like: compile 'org.webrtc:google-webrtc:1.0.20198'
And then make sure you rebuild your project (not only refresh gradle, since it might not be enough for the autocomplete to work).
In your screenshot, it looks like you are trying to autocomplete outside of any method. Since Android Studio tries to only show you valid stuff, it won't display the other methods unless you write it on a valid context (i.e.: inside of some method's implementation).

android: cannot find symbol TaskDescription

I am trying to set the color of the ActionBar while my app is "minimized" using the TaskDescription as suggested by the answer to this post: Setting header color of app in overview (recent apps) screen
Here is the relevant code:
...
import android.app.ActivityManager;
...
public class MainActivity extends SherlockFragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
...
int c = Color.parseColor("#0A782F");
ActivityManager.TaskDescription td = new ActivityManager.TaskDescription(R.string.app_name, R.drawable.launcher, c);
((SherlockFragmentActivity) this).setTaskDescription(td);
}
...
}
Although I have not posted the xml resource files for this project, you may assume that R.string.app_name and R.drawable.launcher are defined in xml files.
Here's the problem: when I compile this code, I get the following error:
error: cannot find symbol
...
symbol: class TaskDescription
location: class MainActivity
Now, from what I understand, Java throws the cannot find symbol error when you refer to a class or variable name that does not exist. In the case of classes, it is usually caused by forgetting to import the class you need. However, that cannot be the cause in this case, as I have clearly imported ActivityManager at the top of my class file.
Here are some other examples where people have done exactly the same things, just to show that I have done my research: http://www.programcreek.com/java-api-examples/index.php?class=android.app.ActivityManager&method=TaskDescription
Finally, I thought the problem might have been that you can only instantiate the TaskDescription class in an Activity context. However, I am using SherlockFragmentActivity instead of the standard AppCompatActivity. Unfortunately, I tried changing this and I still get the same error. Any ideas?
Thanks in advance for your help!
Found the solution! I didn't mention in the problem that I had built my project using gradle. The issue was that I was using an old Sdk version (in particular, I was using a version of the android Sdk which had not yet defined the TaskDescription class). Therefore, to fix the problem, all I had to do was add the following line to the appropriate place in my build.gradle file:
compileSdkVersion 21
Hope this helps someone else who is feeling lost for the same reason.

Intermediate Progress doesn't work with ActionBarSherlock running on Gingerbread

I setup ActionBarSherlock with my app, and I'm trying to use the Intermediate Progress, I'm using this:
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setSupportProgressBarIndeterminateVisibility(false);
In my onCreate, and then using:
setSupportProgressBarIndeterminateVisibility(true);
To enable it.
It works fine in ICS but it doesn't work at all in Gingerbread or Froyo, does anyone know how to get it to work? Thanks
I just had the same problem. Jake's solution above did not fix it for me - the method is undefined.
I found a working solution posted by Jake on the bug list for ActionBarSherlock here:
Action Bar Indeterminate Progress Bar Not Disappearing
See Jake's response to the poster - the trick is to call getSupportActionBar() first, to "trigger creation of the views".
So my onCreate() method is:
protected void onCreate(Bundle arg0)
{
super.onCreate(arg0);
// allow window to show progress spinner in the action bar
requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
getSupportActionBar();
setSupportProgressBarIndeterminateVisibility(false);
}
Update based on comment from Laux:
Make sure your imports reflect com.actionbarsherlock.view.Window.FEATURE_INDETERMINATE_PROGRESS for this to work.
Here is part of my import block from an app that uses this pattern:
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.view.ActionProvider;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.actionbarsherlock.view.Window;
import com.actionbarsherlock.widget.ShareActionProvider;
This is a very good thing to remember when working with ABS - many of your normal Android imports should be updated to refer to ABS instead.
It may be a good idea to revisit your import block, or possibly remove it entirely and let Eclipse rebuild it for you (CTRL-SHIFT-O) to which point Eclipse will prompt you for each import that ABS redeclares.
This was also explained by Glebbb in his answer.
I'm sure you've probably figured it out by now, but the most likely culprit is you including the wrong file because it's so easy to do automatically.
Replace any import of android.view.Window with com.actionbarsherlock.view.Window and the needed features will work.
You need to call supportRequestWindowFeature.
requestWindowFeature is a final method on Activity and couldn't be overriden.
Check, if you are using Theme.Sherlock.NoActionBar or similar no action bar theme for this activity.
In this case setSupportProgressBarIndeterminateVisibility method fails for me with
Caused by: java.lang.NullPointerException
at com.actionbarsherlock.internal.ActionBarSherlockCompat.updateProgressBars(ActionBarSherlockCompat.java:710)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.onIntChanged(ActionBarSherlockCompat.java:686)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.updateInt(ActionBarSherlockCompat.java:681)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.setFeatureInt(ActionBarSherlockCompat.java:665)
at com.actionbarsherlock.internal.ActionBarSherlockCompat.setProgressBarIndeterminateVisibility(ActionBarSherlockCompat.java:637)
at com.actionbarsherlock.app.SherlockFragmentActivity.setSupportProgressBarIndeterminateVisibility(SherlockFragmentActivity.java:282)
I guess you should use a progress dialog instead to indicate loading process or regular Theme with activity title bar and then use setProgressBarIndeterminateVisibility method for older platforms.

Main layout not recognized, immediately after starting new Android project

The below code was automatically generated when I started a new android project.
I'm getting an error on the "R.layout.main" saying it does not exist.
I do in fact have a main.xml and I can see the layout change as I edit it in the Graphical Layout tab.
How can I fix this so I can run my application?
public class ComplimentGeneratorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
The package name of the R file and the package name of your source code may not match. Make sure the package your code is in is the same as the package defined in the manifest file. Otherwise you will need to import the R file with the full package name (ex: com.example.R.layout.main).
If they match, for some reason your R file was not generated properly. Try cleaning your project.
Also, start accepting some answers. I almost didn't answer this because of your horrible acceptance rate.
This may be a very simple answer, but it has happened to me before. Try to restart eclipse. File -> Restart
Try to import the R.java file with the full path inside the main activity class file...
import com.example.packagename.R;
Hope, this will solve your query.

Categories