I have one problem with the comments in Android Studio.
My template file (File Header) Is like this:
/**
* Created by ${USER} on ${DATE}.
*/
When I create new class, or Interface my comment header gets automatically generated, and this works.
But when I generate: activity, fragment, application, services, the comment does not generate. I really do not know where is the problem.
Here is the Activity template description. But when I create new Activity, the comment does not appear, and every time I need to retype it.
package ${PACKAGE_NAME};
import android.app.Activity;
import android.os.Bundle;
#parse("File Header.java")
public class ${NAME} extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
Thanks!
To generate comments type /** above the method name and press enter. You can also use a tool under Android Studio menu:
Tools > Generate JavaDoc
i am beginner in android programming and English language :) !!
i want to get names from user and show the names that user inputs randomly by pressing a button.
how can i store a user input names in a array list?
and As regards I don't know how many names he wants to enter and don't show a name twice and after showing the name erase that name in array list.
tanks a LOT.
package farshid.mk.teststringarraylist;
import java.util.ArrayList;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class StringArraylistActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ArrayList<String> inputs = new ArrayList<String>();
Well, there are many ways to do this but one would be:
Create an EditText, a view that allows user input
Create a Button that the user would click to indicate that he/she is done entering the name in the EditText
Create an OnClickHanlder for the button so that you can get the text from the EditText, convert it to a String (EditText returns an Editable object), and add it to your ArrayList<String>
// add the string
myArrayList.add(myEditText.getText().toString().trim());
To remove duplicates from the List you will need to convert it to a Set... google "java arraylist remove duplicates (First Link)
Also, in Java, it's best practice to use Generics:
// psuedocode
List inputs = new ArrayList
There are many ways you could display this list of text to the user in Android, you will need to research that on your own.
You simply clear the ArrayList when you are done with the names.
This should get you going =)
There's a lot of things you'd need to do to achieve this:
Define the EditText in your layout's XML.
Get a reference to it from the Activity by calling findViewById(R.id.your_edittext_id).
From that reference, call getText().toString() to get the value from the EditText.
Add that String to your ArrayList.
That's basically the main idea behind what you're trying to achieve. I could give you the code but then perhaps its best for you to do it yourself (so that the next time you need to do this, you'd understand the concept).
Good luck! :)
It is saying that popupWindow.OnDismissListner Can't be resolved as a type and when I hover the mouse over it the only option I get is to make it an interface, which isn't what I want to do.
public class MainActivity extends Activity implements OnItemSelectedListener,
PopupWindow.OnDismissListener, View.OnClickListener {
I am unsure exactly what is wrong. I imported android.widget.PopupWindow.OnDismissListener; and it still is giving the error. I am trying to get a menu to pop up with a list of dice and when a dice is clicked it will return a matching int value based on how many sides the dice has. I have no other errors except the one mentioned above.
Try replacing
import android.widget.PopupWindow.OnDismissListener;
with
import android.widget.PopupWindow;
Or simply use OnDismissListener in your code (not PopupWindow.OnDismissListener).
I'm having trouble following a guide on using SQLite in Android. I'm using a ListFragment instead of a ListActivity(as in the example), so I have the ListFragment implement LoaderManager.LoaderCallbacks<Cursor> instead. Then, in the fillData() method in the ListFragment:
private void fillData() {
// Fields from the database (projection)
// Must include the _id column for the adapter to work
String[] from = new String[] { NotesSQLiteHelper.COLUMN_TITLE };
// Fields on the UI to which we map
int[] to = new int[] { R.id.label };
getLoaderManager().initLoader(0, null, this); //error
adapter = new SimpleCursorAdapter(getApplicationContext(), R.layout.notes_row, null, from, to, 0);
setListAdapter(adapter);
}
I get the error:
The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, NotesActivity.ArrayListFragment)
on the marked line even though this implements LoaderManager.LoaderCallbacks<Cursor>.
Thank you for any ideas.
You are not using the right implementations of CursorLoader and Loader.
Remove your old imports and use these ones:
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
But I have the same Problem using SherlockActionBar:
As I have to extend SherlockListActivity there is NO method getSupportLoadManager().
Any ideas on this?
EDIT: follow this tutorial if you do not know how to use fragments. Create a new Class with extends SherlockFragment and move your display logic there. Make your old activity extend SherlockFragmentActivity and show the newly created SherlockFragment. This way I got it working. Thanks to #JakeWharton!
A few things to watch out for (from my recent experience battling with this):
If your minSDK is set to less than 11 (i.e. level 10 for Gingerbread) and you are using the Support Pack for backward compatibility, make sure you use
getSupportLoaderManager().initLoader(LOADER_ID, null, this);
You mentioned this when you said you are using ListFragment, but it bears repeating: Do not extend Activity, otherwise the support package will not work. Instead, extend the FragmentActivity class or the ListFragment class.
For your imports, make sure you are using the correct versions if your minSDK < 11:
android.support.v4.app.FragmentActivity;
android.support.v4.app.LoaderManager;
android.support.v4.content.Loader;
Hope this helps you... or at least someone else...
Casting the third argument solved the problem in my case:
from
getLoaderManager().initLoader(0, null, this);
to
getLoaderManager().initLoader(0, null, (android.app.LoaderManager.LoaderCallbacks<Cursor>) this);
Note:
minSdk was 8 and i was using support library v4.
(android.support.v4.app.LoaderManager.LoaderCallbacks<Cursor>) this)
did not work.
getSupportLoaderManager() or getSupportLoadManager()
did not work.
This code was inside activity not fragment
It's late but maybe help some one.
if you use loader maneger in fragment and you min api is below HONEYCOMB
you shuld use this imports
import android.support.v4.app.LoaderManager;
import android.support.v4.content.CursorLoader;
import android.support.v4.content.Loader;
import android.support.v4.widget.CursorAdapter;
import android.support.v4.app.Fragment;
and for initiate loader use this code
getActivity().getSupportLoaderManager().initLoader(/loader stuff*/);
hope this help some one.
Change your imports to
import android.support.v4.app.Fragment;
import android.support.v4.app.LoaderManager;
import android.support.v4.content.Loader;
and use
getSupportLoaderManager().initLoader(0, null, this);
This is what you have:
getLoaderManager().initLoader(0, null, this);
This is what you should have:
LoaderManager.getInstance(this).initLoader(0, null, this);
Reason for the first one not working:
/**
* #deprecated Use
* {#link LoaderManager#getInstance(LifecycleOwner) LoaderManager.getInstance(this)}.
*/
My error was beacause this:
//import android.app.ListFragment; Error when doesnt import from support.v4
import android.support.v4.app.ListFragment;
In my case I had to have my Activity extend ActionBarActivity from the android.support.v7.app package. I was then able to use
getSupportLoaderManager().initLoader(0, null, this);
I'm using ActionBarSherlock with my app and I as well was running into this issue and worked through all the steps discussed by others in this question. However I was continuing to have the same problem after trying all the suggested resolutions.
The method initLoader(int, Bundle, LoaderManager.LoaderCallbacks<D>) in the type LoaderManager is not applicable for the arguments (int, null, this)
The issue was I had fallen into expecting Eclipse to tell me when I was missing something and it was telling me that but not in the way I was used to. Typically Eclipse in other cases would tell me I'm missing the overrides to make something work but it's not directly saying that here. I finally picked up on the "LoaderManager.LoaderCallbacks" being the issue and realized I had no callbacks for it thus this error was actually a very valid error. Adding the basic overrides resolved my issue and allowed me to move forward.
// Creates a new loader after the initLoader () call
#Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
// do work
return null;
}
#Override
public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
adapter.swapCursor(data);
}
#Override
public void onLoaderReset(Loader<Cursor> loader) {
// data is not available anymore, delete reference
adapter.swapCursor(null);
}
I was having a similar issue where my AsyncTaskLoader was not returning my List, but my resolution was to change the call from .initLoader to .restartLoader.
So I actually never called .initLoader and just immediately called .restartLoader.
I had a onListItemClick listener in my fragment and every time backed out of the fragment and reloaded the onListItemClick and navigated through the app it kept crashing.
It might just be specific to my app but hopefully it helps others if they are having fragment backstack reload issues.
This was part of a file manager portion in my app so it is specific to clicking multiple onListItemClicks in the fragment you are loading.
I got around this by using a SherlockListFragment (or you could use ListFragment, I suppose), but have it contained within a Activity. I first created a generic FragmentHolderActivity class that looks like this:
FragmentHolderActivity.java
public class FragmentHolderActivity extends SherlockFragmentActivity {
public static final String FRAGMENT_LAYOUT_RESOURCE_ID = "fragment_layout_resource_id";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Integer fragmentLayoutResourceId = getIntent().getIntExtra(FRAGMENT_LAYOUT_RESOURCE_ID, Integer.MAX_VALUE);
Assert.assertNotSame(fragmentLayoutResourceId, Integer.MAX_VALUE);
setContentView(fragmentLayoutResourceId);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish();
return false;
default:
return super.onOptionsItemSelected(item);
}
}
}
Then you need to create an XML file for your fragment.
your_list_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="com.example.YourListFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment" />
And here's the code you use to start the fragment:
Intent intent = new Intent();
intent.setClass(this, FragmentHolderActivity.class);
intent.putExtra(FragmentHolderActivity.FRAGMENT_LAYOUT_RESOURCE_ID, R.layout.your_list_fragment);
startActivity(intent);
You just tell the FragmentHolderActivity to use the your_list_fragment layout, which in turn loads the YourListFragment.java
You can then use: getSherlockActivity().getSupportLoaderManager().initLoader(...) in YourListFragment.java
Not sure if this is the correct approach, but it keeps all my logic in Fragments, which is nice.
Ran into the same problem. My minSdkVersion is 14, so cannot use android.support.v4 package.
I figured it by extending LoaderCallbacks, instead of LoaderManager.LoaderCallbacks and use these packages
import android.app.LoaderManager.LoaderCallbacks;
import android.content.CursorLoader;
import android.database.Cursor;
import android.widget.SimpleCursorAdapter;
If you have tried all the above methods and still facing the same error with the "this" parameter, then follow these steps :
Go to settings and enable imports on the fly option.(by default it'll
be enabled and you can do this by using Alt+Enter keys).
Cut the whole code of the activity which had implemented
LoaderCallback... and paste it in a text editor.
Then at last copy the whole code of that activity from the text editor
where you had pasted, without any import commands.( Only the code of
the class/activity).
You'll get lots of errors as you have imported nothing yet.
Just press Alt+Enter wherever you're getting the errors and it's
libraries will be imported automatically.
Note : Choose android.app... library for CursorLoaders.
Try these 2 lines it will work
android.support.v4.app.LoaderManager loaderManager = getSupportLoaderManager();
loaderManager.initLoader(LOADER_ID, null, this);
I am using minSDK 27 and had this same issue, I tried to cast like #Dexter suggest, but that gave an error saying cannot be cast to android.app.LoaderManager$LoaderCallbacks, so I then tried to use different import statements and this worked. I commented out the v4 versions and this is what I have now and the app is working:
//import android.support.v4.app.LoaderManager;
import android.app.LoaderManager;
import android.support.v4.app.NavUtils;
//import android.support.v4.content.CursorLoader;
import android.content.CursorLoader;
//import android.support.v4.content.Loader;
//import android.content.Loader;
Not quite sure when/how the v4 versions were imported.
0
After a long sacrifice i got this solution if you are using fragments then just use this code.
getActivity().getSupportLoaderManager().initLoader(1, null, YourActivity.this);
i hope this is helpful for you
I was trying to call initLoader() inside an activity. For me this worked
changing
getSupportLoaderManager().initLoader(LOADER_ID,null,this);
to
getLoaderManager().initLoader(LOADER_ID,null,this);
If you use API 28 or later, instead of getLoaderManager use:
getSupportLoaderManager().initLoader(id, null, this);
If you use API 29, instead of getLoaderManager use:
getSupportLoaderManager().initLoader(id, null, this);
I'm getting "cannot be resolved to a type" in my code. I'm looking for solution but all solutions are not working.
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
public class Vortex extends Activity {
private static final String LOG_TAG = Vortex.class.getSimpleName();
private VortexView _vortexView; //there is the problem
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_vortexView = new VortexView(this);
setContentView(_vortexView);
}
}
The problem is with VortexView in this code. Eclipse tell me that VortexView cannot be resolved to a type.
Any solution?
Do you have VortexView class in your project, if so, ctrl+shift+O will resolve the issue. Otherwise add class/jar to classpaht and do ctrl+shift+O (organize imports).
VortexView is your custom view? If there exists VortexView.class
Probably, you have made either of the following two mistakes.
You have not extended a view named VortexView at all
You have extended it correctly, but you haven't made it available to the current package
When you are doing such things, it is better to define custom views inside the same package. Or even though you are defining it somewhere else, make it avilable where it is used.
import yourPackage.VortexView;
Hope this helps.