android: cannot find symbol TaskDescription - java

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.

Related

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).

Cannot find symbol class Intent

I am new in android development and coding java and xml.
but I was following this tutorial:
http://www.steventrigg.com/activities-and-the-action-bar-create-an-alarm-clock-in-android-tutorial-part-1/#comment-296
then I had this error when using Intent. The word "Intent" under switch became red and there is an error "cannot find symbol class Intent"
Can someone explain to me what is going on and how to solve this?
This is the last part of my code under AlarmListActivity.java
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
switch (item.getItemId()) {
case R.id.action_add_new_alarm: {
Intent intent = new Intent(this,
AlarmDetailsActivity.class);
startActivity(intent);
break;
}
}
return super.onOptionsItemSelected(item);
}
Look at your AlarmListActivity again and check the import statements at the top and make sure it includes the line:
import android.content.Intent;
If you intend to use any pre-existing classes that aren't part of the java.lang package, you generally have to import those classes. An Android Intent, for example, is a pre-built class, written by the Android development team, that allows for notification of other apps/activities. If you want to use Intent, you'd then have to import the package containing Intent.
When you write new Intent(), the compiler sees that you're requesting the construction of a new object, but because that object is not found in the java.lang package, it needs to know where to look for a blueprint to build that object. The import statement is the location of that blueprint.
I took a look at the tutorial and in the manner of experienced programmers, the author seems to have glossed over a few basic, but nonetheless, important things, such as the import statements that make his sample code work.
I had a same problem which I just resolved, by understanding how Android Studio indexes files, As you know Building an Android App is quite complicated process. So Android studio has some internal references which it keeps getting updated on change of every file that you have created.
I arrived at this post while searching for the solution,
This is how I got this problem
I usually wont create an activity under the main project package, I create sub packages to organize files as per the design pattern that I use, for eg If my APP name is com.example.testingaravind then inside that I usually create packages such as activites, services, models, managers etc ... So today I just created an activity first and then moved that activity into activites package via Android Studio, I started facing the same issue what you have described, Below was my source code
public class BootstrapActivity extends ActionBarActivity {
private static final String TAG = "BootstrapActivity";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bootstrap);
}
public void startServiceOnClickHandler(View view) {
Intent intent = new Intent(BootstrapActivity.this , AnalyzerService.class);
startService(intent);
}
}
In the method startServiceOnClickHandler it was showing an error saying,
"Cannot resolve constructor Intent" I searched a lot in google and found that
When I move a file from one package to other package, my manifest file wont get updated, in the manifest we mention the activity name and its package path in my case it should be
android:name=".activities.BootstrapActivity"
But it was
android:name=".BootstrapActivity"
Because of this, Android studio was unaware that a class called BootstrapActivity exists inside the activities folder,
This seems to be a bug in the way how android studio works. Android Studio has to update manifestfile when I move the activity class file from one package to another package.
I am posting this to help others who might arrive at this post with the similar usecase.
Check name in manifest file in activity tag specify correct package for example your
<activity
android:name="Your-Package.MainActivity"
android:exported="true" />
In second case If you are using kotlin class inside java class, after configuring kotlin in project can solve the project...

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

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

Just update ADT and Unable to resolve superclass errer appear

The error show after I update my ADT, the project is created before the update.
In Login Activity,
public class LoginActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
// Do something here... if login success, Start another activity
Intent i = new Intent(LoginActivity.this, MainActivity.class);
LoginActivity.this.startActivity(i);
}
.......
}
In MainActivity,
public class MainActivity extends FragmentActivity {
protected void onCreate(Bundle savedInstanceState) {
//......
}
}
I try to remove all the code inside and back to a blank activity. The error is not happen because of the code inside the activity. I think is abut the extend FragmentActivity
Here is the error trace:
Unable to resolve superclass of Lhk/iactive/imoodle/activity/MainActivity; (15)
Link of class 'Lhk/iactive/imoodle/activity/MainActivity;' failed
I get this error after I update my ADT.
I do some research on this topic. I try to import the android-support-v4.jar again and this do not fix the problem.
I also read some blog like THIS ONE can not help me too :(
I have solve the problem by myself. I am going to share the solution hope there is other people who suffer from the same error can save some time :D
I try many method for example go to build Path Config -> Order and Exprot, All the library is already checked. And the NoClassPathFound error is still showing.
I have no external Jar for the project and The error is from FragmentActivity from Android Support Library.
Then I delete the android-support-v4.jar complete and add it again by right clicked the project -> Android Tools -> Add support library...
After the new support library added. All the error fix. :D
This worked for me:
For all projects (app and library):
Right click the project, and choose Properties
Go to Java Build Path-Order and Export.
Tick 'Android Private Libraries'

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