I have ,in a fragment, a method call who open an AlertDialog when an user tap on an button, in that dialog I would like to show a Spinner with countries ( Spain, Italy, French....)
My code for the spinner is the following:
RestCountries restCountries = new RestCountries();
List<RestCountries.Datum> countries = restCountries.data;
String mCities ="";
ArrayList<String> citiesArrayList = new ArrayList<>();
for(RestCountries.Datum data : countries){
mCities = data.name;
citiesArrayList.add(mCities);
}
ArrayAdapter spinnerAdapter = new ArrayAdapter(getActivity(),android.R.layout.simple_spinner_dropdown_item, citiesArrayList );
mCountrySpinner.setAdapter(spinnerAdapter);
The spinner is showed emphy after the dialog is opened.
On the logcat I get
Could not find class 'android.widget.ThemedSpinnerAdapter', referenced
from method
android.support.v7.widget.AppCompatSpinner$DropDownAdapter.<init>
Any idea about what I am doing wrong
In my case I solved the problem just setting for all the modules in the project the same SDKCompileVersion. Here it is my complete answer in a similar question
Cheers
Could not find class 'android.widget.ThemedSpinnerAdapter' [Android Studio]
I faced and win this problem!
In case if you are using AndriodAnnotations here the problem is that I filled the lists in the method onCreate().
I used to get View via findViewById(R.id...) and worked with them.
Now, as it turned out during debugging, all Views is not created in onCreate() yet!
The problem was solved when I found an annotation #AfterViews in the docs, and the method under this annotation now fills all my actions and does initialization of fields.
So, anyway, check your code on NullPointerException caused by invoking empty view object.
This may not help everyone, but I was having this issue trying to add a spinner to a PopupWindow.
I updated my compileSdkTarget from 23 to 25, and my support library version to 25.1.0, but it didn't help.
It turned out that changing the spinnerMode to "dialog" worked round the problem:
<Spinner
android:id="#+id/group_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
/>
It doesn't completely fix it of course if you really want a dropdown spinner.
There are several different causes of this problem.
In my case (was trying to sign up to Parse), I got this error trying the app on a tablet.When I switched to an Android phone, I got the error message:
You must register this ParseObject subclass before instantiating it
So, In my App.java class I did this:
public class App extends Application {
public void onCreate() {
super.onCreate();
Parse.enableLocalDatastore(this);
Parse.initialize(this, "PARSE APPLICATION ID", "PARSE CLIENT KEY");
}
}
and then in my manifest, I did this:
<application
android:name=".App"
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
...
That was it.Had nothing to do with Spinner
Related
Here's a simple app, I'm trying to create logs in the printToLogs method.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.v("Log0","test");
}
public void printToLogs(View view){
TextView menuTextView1 = findViewById(R.id.menu_item_1);
TextView menuTextView2 = findViewById(R.id.menu_item_2);
TextView menuTextView3 = findViewById(R.id.menu_item_3);
String item1 = (String) menuTextView1.getText();
String item2 = (String) menuTextView2.getText();
String item3 = (String) menuTextView3.getText();
Log.v("Log1",item1);
Log.v("Log2",item2);
Log.v("Log3",item3);
}
but logs with the tags Log1, Log2, Log3 are not shown at all in the logcat, what does show up is the Log0 in the onCreate method, but the other ones in printToLogs never show up when I search for them at all. I attempted re-installing the app and restarting logging. That didn't work.
The menu items are: Mango sorbet, Blueberry pie, Chocolate lava cake. And yes, I tried searching for them, and they are not in the logcat either.
If this is your actual code, you aren't even calling printToLogs in the onCreate method. You should be more diligent before posting something simple like this.
Barring a serious runtime environment issue, this problem should be fairly easy to solve.
It seems as if the printToLogs(View view) method is to be executed in response to the user clicking a button. If so, try including the following line in your activity_main.xml if you haven't already:
android:onClick="printToLogs"
This will bind the button on the UI with the printToLogs(View view) method.
If, on the other hand, printToLogs(View view) is intended to be a standalone method (i.e. one that should execute regardless of user input) it should not accept a View as an argument. For your purposes, its parameter list should be completely empty. In other words, the method signature should read:
public void printToLogs()
Also, it should be called within the onCreate(Bundle savedInstances) method. Add the following to the onCreate(Bundle savedInstances) method:
printToLogs();
This will initiate the execution of the method as soon as the app begins to run.
Make sure the logcat filter is set to "Verbose" when testing like so: (img is link since apparently I need 10 rep. to embed images into answers directly)
logcat filter
heyy add your method/function name in your button by using property section or just android:onClick in your xml file and then it will be solved
I've followed Google docs and created an inner class to popup a dialog in my android app.
I've been running it fine in eclipse for a while, unbeknownst to me there was an error that came up when I imported the app into android studio to start using the stable release. I was getting the "Fragment inner class should be static" error upon trying to build.
OK so I understand now after researching that this is a bad thing, but when I change it to static, I now have a bunch of references to (a) global variables , and (b) "MainActivity.class" , that are now errors.
So inside this DialogFragment inner class, how do I access my globals, or pass them in, and how do I reference MainActivity.class ?
Eg:
final EditText input = new EditText(MainActivity.this);
for (NewsEvent ne : filteredList) { //filteredList is global List of objects
...
I am calling the dialog in the onOptionsItemSelected like this:
AlertDialogFragment alert = new AlertDialogFragment();
alert.show(this.getFragmentManager(), "Alerts");
To send a Data to the DialogFragment use static newInstance(params) method.
Put the Data in a Intent and on the onCreate() method get your Data from the Intent.
Check this example from the Android dev
I am creating a DialogFragment that I use for my login dialog. I want to have autocompletion for the email addresses that are used as user ids. I found a good tutorial here, but it didn't really deal with using a DialogFragment.
My current code is:
DialogFragment dialog = new LoginDialogFragment();
AutoCompleteTextView emailLoginId = (AutoCompleteTextView) findViewById(R.id.login_user_id);
if (null == emailLoginId) {
Log.d(tag, "EmailLoginId is null!");
}
// get list of drivers
ArrayAdapter<String> driverList = dbHelper.getAllDriverEmailStringAdapter();
Log.d(tag, "Driver List size is " + driverList.getCount());
emailLoginId.setAdapter(driverList);
dialog.show(getSupportFragmentManager(), "LoginDialogFragment");
As you can see, I'm creating the dialog, then attaching the list of names. Unfortunately, this gives me a null pointer exception on the line:
emailLoginId.setAdapter(driverList);
I'm sure this will probably be some embarrassingly simple oversight on my part, but that's okay, if it gets me moving forward.
I've tried this same code, with appropriate modifications, in the onStart() and onCreateView() portions of the LoginDialogFragment. In all cases, it can't find the fields, either the login id field, or the password field.
When calling from within the onStart() and onCreateView() routines, I used:
AutoCompleteTextView emailLoginId = (AutoCompleteTextView) getActivity().findViewById(R.id.login);
Is that wrong?
The only other thing I can figure that might be causing the problem is that the dialog isn't actually created until dialog.show(getSupportFragmentManager(), "LoginDialogFragment") is called, but that doesn't jibe with the documentation and you'd still think this would work in the onStart() routine at the very least.
Thanks, in advance, for your time.
My second book for Android programming Hello, Android by Ed Burnette. I'm using eclipse. The code matches the book and it matches the code downloaded from the website of the book. But I know I'm doing something wrong here. I added a bunch of breakpoints where I figure (mostly guessing) where the problem might be happening. What I've come to is that this line of code is the culprit (SudokuActivity.java line 21) You can download the entire code here
http://kbsoftware.dlinkddns.com/Sudoku.zip
aboutButton.setOnClickListener(this);
but I just can't figure out why ? It must be the result of something I'm doing wrong somewhere else. I've deleted and recreated the avd and that made no difference so not it. I'm at a lost here.
public class SudokuActivity extends Activity implements OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View aboutButton = findViewById(R.id.about_button);
aboutButton.setOnClickListener(this);
}
I want to thank everyone who responded, it's all fixed and working and I could not have done it without your help. I've learned more working on this problem then I would of in weeks if not months of problem free programming.
Yout aboutButton is not getting bound properly.
Do something like
Button aboutButton = (Button) findViewById(R.id.about_button);
I downloaded your code and it runs correctly on my phone. So if your code is the same it should run.
It seems that findViewById didn't find the view and then calling a method on a null object caused the nullpointerexception.
My dumb question: have you tried a project clean up? You can even try saving your classes, deleting the project and creating a new one.
Hope it helps
Well basicly I have a textview and when the application is created it sets a string as the textviews text not hard, but I get a force close error when I run the app on my phone.
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
Then I have a error on a togglebutton also
ToggleButton silent=(ToggleButton)findViewById(R.id.silentbutton);
silent.setChecked(false);
And I have errors for all my other buttons/textviews can anyone help, please?!
EDIT:
I cant post pics because I am a new member, :(
Link to imgshack http://imageshack.us/photo/my-images/849/unledggp.png/
If code for the whole textview snippet.
if (android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_UNMOUNTED)) {
TextView sdcard=(TextView)findViewById(R.id.sd_textview);
sdcard.setText(R.string.not_mounted);
}
OnCreate Function
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
checkSD();
checkRing();
checkWifi();
checkBt();
}
Look for all instances of sd_textview and make sure the one that you're trying to reference is a TextView. If you want more clarity you can debug your code and see what object is actually being returned by not casting into a TextView:
View sdcard = findViewById(R.id.sd_textview); //debug this
//you can also log the View object to see the type
Log.d("Test", "" + sdcard);
Looking at your error log (assuming its the right error log) you have a ClassCastException in the checkWifi method. Edit your question and include ALL of the onCreate method and all of the checkWifi method, but I expect you are using the same id for multiple views.
Two things I can think of (although seeing more code would help).
Make sure you have called setContentView(R.layout.main) (or whatever your layout file is called). Do this BEFORE any attempt to use findViewById(...).
Secondly sdcard.setText(R.string.not_mounted); in this statement R.string.not_mounted is a resource ID (int) and not a string. You would need to use...
sdcard.setText(getString(R.string.not_mounted));