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...
Related
I've been trying to get IAP to work with my LibGDX game for Android for over 6 hours. First I tried to use gdx-pay, but sadly the documentation for it is very unclear to me and was therefore not able to get it to work.
Because my games are only for Android (for now) gdx-pay was overkill anyway, so I decided to directly implement Google Play IAP. I found a tutorial that is pretty clear, but sadly it doesn't explain where I should put the IInAppBillingService.aidl file.
I tried putting it in a newly created directory in the Android directory, namely /android/vending/billing, but that doesn't seem to work and the directory also doesn't show when I have my project view on Android.
I also tried creating a new package "andoird.vending.billing" in the java directory where the game package with the AndroidLauncher is in but that doesn't work either.
With both these methods I'm not able to import the class IabHelper in the following tutorial code which is in AndroidLauncher.java located in my game's package:
String base64EncodedPublicKey = "xxx";
// compute your public key and store it in base64EncodedPublicKey
mHelper = new IabHelper(this, base64EncodedPublicKey);
mHelper.startSetup(new IabHelper.OnIabSetupFinishedListener() {
public void onIabSetupFinished(IabResult result) {
if (!result.isSuccess()) {
// Oh noes, there was a problem.
Log.d("IAB", "Problem setting up In-app Billing: " + result);
}
// Hooray, IAB is fully set up!
Log.d("IAB", "Billing Success: " + result);
}
});
Where (and how) am I supposed to put the "IInAppBillingService.aidl" file to be able to import the IabHelper class in my LibGDX project? If you have a clearer tutorial on gdx-pay or on a technique similar to the one I'm using now I would also appreciate it.
Thanks in advance!
I have created an *.aar File from a Android Studio project. This file I have successfully imported to an another Android Studio project file. Now I want start the activity from the included libraries. But i am hanging and I try hour to hour...
In wich file I have to include the statement like this?
public void button_to_start_the_activity(View view)
{
Intent intent = new Intent(StartActivity.this, MainActivity.class);
startActivity(intent);
}
Should I implement this in the modul java file or in the application java file?
(Please check this out, I have not found a activity java file in my module :-/ glglgl)
Steps bevor, i have created a new activity in my main-project and i was able to start the activity without trouble.
Okay... Next question:
In the Main Manifest,...what to hell I shout declare in android:name"....?...." like this:
<activity
android:name="....??????...."
android:label="DLC"
android:screenOrientation="portrait"
android:theme="#style/Theme.AppCompat.NoActionBar">
</activity>
Maybe, or for sure, simple question... But i try to learn a little bit AS since a week... For your understanding, i will try to explain my project with simple words....:
Try to merge two app projects in one app!
I have created a app, then i have created an another app. Now i want to merge. Simple startpage (allready exist) with the function to call the both activitys... :-/
My final solution to start a modul activity in the main app (For poeple with less experience like me)
Create in Android Studio a modul from the existing app
Open Main-Project an insert the new modul (File-->New-->Import Modul)
Right click on project folder. Search App (left side), then dependencies (right side) --> Add new from modul --> Add your modul
Check your build.cradle (Main-App) for compile project(':yourmodul')
Add this in your activity (from the Main-App)
public class StartActivity extends AppCompatActivity {
public void gotocalc(View view)
{
Intent intent = new Intent(StartActivity.this, YourModulClass.class);
startActivity(intent);
}
gotocalc is for start with a button android:onClick="gotocalc" set in your layout.xml in the Main App.
Clean, Rebuild
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.
I read almost every answer obout this topi c but i cannot find a working solution for my case or maybe I'm missing somthing.
I use eclipse as IDE and I would like to use an external library for example I'm trying to add this Library to my project but I don't understand what i'm doing wrong.
I tried download the full project and import it into the workspace, mark it as library and then under my project and add it as reference.
If everything is ok how should I call an activity from the linke library?
I tried to defind the linked activity into my manifest file but without success.
Could you please point me in the right way?
Thank you
You need to write code which will call the library activity inside onCreate() or any other method which you choose like
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity);
//Here you need to call directory chooser activity.
final Intent chooserIntent = new Intent(this, DirectoryChooserActivity.class);
final DirectoryChooserConfig config = DirectoryChooserConfig.builder()
.newDirectoryName("DirChooserSample")
.allowReadOnlyDirectory(true)
.allowNewDirectoryNameModification(true)
.build();
chooserIntent.putExtra(DirectoryChooserActivity.EXTRA_CONFIG, config);
startActivityForResult(chooserIntent, REQUEST_DIRECTORY);
}
Import DirectoryChooserActivity and DirectoryChooserConfig class in your activity; If you are not able to import the mentioned class then you are not added library into the project correctly.
What I did was create two .java files. One that can compile and run on a 1.5 phone (SDK3) and then one that works on 2.0(SDK5) So for this example i'll call the 1.5 file ExampleOld and the new one Example. I was wondering if i just made activity like this if it would work sort of like a "portal" and pick the activity to load depending on the SDK so there is no crash or compile errors. Are there any changes I should make to my code? Maybe anyone out there that's had to do this before. thanks!
package com.my.app;
import android.app.Activity;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
public class ExamplePortal extends Activity {
int sdk=new Integer(Build.VERSION.SDK).intValue();
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (sdk<5) {
Intent v = new Intent(this, ExampleOld.class);
startActivity(v);
}
else {
Intent v = new Intent(this, Example.class);
startActivity(v);
}
}
}
What you're doing (correct me if I'm wrong) is trying to maintain backwards compatibility while making use of new APIs if the user is running a newer android version. The best way to do this is to follow the tutorial Google posted here. This avoids any verification issues and is really the best way to do stuff imho.
I would put this decision in a Factory Class to avoid having these if-else statements all over the codebase.