I'm having a trouble with findViewById but I can't find where the problem is.
Here's my FirstFragment class code:
public class VehicleInformation extends Fragment implements AdapterView.OnItemSelectedListener {
private TextView text_vehicle_no, text_chassis_no;
private TextView text_company_name,text_insurance_id;
RadioGroup radioGroup;
RadioButton radioButton;
private SpinnerAdapter text_vehicle_class;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_vehicle_information, container, false);
return rootView;
radioGroup = (RadioGroup) rootView.findViewById(R.id.insuranceYesNo);
text_vehicle_no = (TextView) rootView.findViewById(R.id.vehicle_no);
text_chassis_no = (TextView) rootView.findViewById(R.id.chassis_no);
Spinner spinner_vehicle_class = (Spinner) rootView.findViewById(R.id.vehicle_class);
Spinner spinner_vehicle_company = (Spinner) rootView.findViewById(R.id.vehicle_company);
Spinner spinner_vehicle_model = (Spinner) rootView.findViewById(R.id.vehicle_model);
Spinner spinner_vehicle_fuel = (Spinner) rootView.findViewById(R.id.vehicle_fuel);
Spinner spinner_vehicle_color = (Spinner) rootView.findViewById(R.id.vehicle_color);
Spinner spinner_vehicle_types = (Spinner) rootView.findViewById(R.id.vehicle_types);
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.vehicle_class_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter1 = ArrayAdapter.createFromResource(this,R.array.vehicle_company_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter2 = ArrayAdapter.createFromResource(this,R.array.vehicle_model_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter3 = ArrayAdapter.createFromResource(this,R.array.vehicle_fuel_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter4 = ArrayAdapter.createFromResource(this,R.array.vehicle_color_array, android.R.layout.simple_spinner_item);
ArrayAdapter adapter7 = ArrayAdapter.createFromResource(this,R.array.vehicle_type_array, android.R.layout.simple_spinner_item);
//specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//aaply the adapter to the spinner
spinner_vehicle_class.setAdapter(adapter);
spinner_vehicle_company.setAdapter(adapter1);
spinner_vehicle_model.setAdapter(adapter2);
spinner_vehicle_fuel.setAdapter(adapter3);
spinner_vehicle_color.setAdapter(adapter4);
spinner_vehicle_types.setAdapter(adapter7);
spinner_vehicle_class.setOnItemSelectedListener(this);
spinner_vehicle_company.setOnItemSelectedListener(this);
spinner_vehicle_model.setOnItemSelectedListener(this);
spinner_vehicle_fuel.setOnItemSelectedListener(this);
spinner_vehicle_color.setOnItemSelectedListener(this);
spinner_vehicle_types.setOnItemSelectedListener(this);
}
public void onStart(){
super.onStart();
EditText datePicker=(EditText) findViewById(R.id.datePicker);
datePicker.setOnFocusChangeListener(new View.OnFocusChangeListener(){
public void onFocusChange(View view, boolean hasfocus){
if(hasfocus){
DateDialog dialog=new DateDialog(view);
android.support.v4.app.FragmentTransaction ft =getFragmentManager().beginTransaction();
dialog.show(ft, "DatePicker");
}
}
});
}
public void radiobtn(View view) {
int radiobtnselect = radioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) view.findViewById(radiobtnselect);
Toast.makeText(VehicleInformation.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String sSelected=adapterView.getItemAtPosition(i).toString();
Toast.makeText(VehicleInformation.this,sSelected,Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
Also showing error on line
ArrayAdapter adapter = ArrayAdapter.createFromResource(this,R.array.vehicle_class_array, android.R.layout.simple_spinner_item);"
saying wrong 1st argument type
Also showing error on line Toast messsage saying cannot resolve method maketext
Unlike the Activity class, Fragment doesn't have a built-in findViewById() method. This means you need to use View.findViewById(), which you already are doing everywhere except in your onStart() method. Here, you can just change this line:
EditText datePicker=(EditText) findViewById(R.id.datePicker);
to this:
EditText datePicker=(EditText) getView().findViewById(R.id.datePicker);
As for your adapter and toast issues, the problem is that (again, unlike Activity), the Fragment class is not a subclass of Context, so you need to pass a different first argument. Try passing getContext() instead of VehicleInformation.this.
Related
My application crashes when I am using Spinner in Fragment.
public class TestRideFragment extends Fragment implements AdapterView.OnItemSelectedListener {
String[] country = { "India", "USA", "China", "Japan", "Other", };
public TestRideFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//return inflater.inflate(R.layout.fragment_test_ride, container, false);
View v = inflater.inflate(R.layout.fragment_test_ride, container, false);
Spinner spin = (Spinner) getView().findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(getContext(),android.R.layout.simple_spinner_item,country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
return v;
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { }
#Override
public void onNothingSelected(AdapterView<?> adapterView) { }
}
Could anyone help me spot the issues with my code, please?
You are trying to get view before it's being created, so that's why it is returning null.
So, you can do like,
Spinner spin = (Spinner) v.findViewById(R.id.spinner1);
or, you can move your code to onViewCreated where view will be created,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_test_ride, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Spinner spin = (Spinner) view.findViewById(R.id.spinner1);
spin.setOnItemSelectedListener(this);
//Creating the ArrayAdapter instance having the country list
ArrayAdapter aa = new ArrayAdapter(getContext(), android.R.layout.simple_spinner_item, country);
aa.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//Setting the ArrayAdapter data on the Spinner
spin.setAdapter(aa);
}
Change your code:
Spinner spin = (Spinner) getView().findViewById(R.id.spinner1);
Into this:
Spinner spin = (Spinner) v.findViewById(R.id.spinner1);
Try using v.findViewById(R.id.spinner1) also make sure that spinner1 is declared in the xml layout fragment_test_ride
Try adding array in strings.xml
<array name="countries">
<item>India</item>
<item>USA</item>
<item>China</item>
<item>Japan</item>
<item>Other</item>
</array>
and in your xml
<android.support.v7.widget.AppCompatSpinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/countries"
android:id="#+id/spinner1" />
Remove the ArrayAdapter but do not this one will make your options static based on whats in the array declare on the string xml
Maybe the problem is in your adapter which should be;
ArrayAdapter<CharSequence> aa= ArrayAdapter.createFromResource(this,
country, android.R.layout.simple_spinner_item);
and you have;
ArrayAdapter<CharSequence> aa= ArrayAdapter.createFromResource(this,
android.R.layout.simple_spinner_item, country);
my text-view is in view class and i want to access it in setting class to change it's font size ... i had tried different methods but still i don't have solution :( please help me out... my code for two classes are ...
Setting class code :
public void addItemsOnSpinner1() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
List<String> list = new ArrayList<String>();
list.add("Small");
list.add("Medium");
list.add("Large");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(dataAdapter);
}
public void addListenerOnSpinnerItemSelection() {
spinner1 = (Spinner) findViewById(R.id.spinner1);
apply = (Button) findViewById(R.id.apply);
spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
apply.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(String.valueOf(spinner1.getSelectedItem())=="Small")
{
// small1=(TextView)findViewById(R.id.textfile1);
//small1.setText("raman rayat");
// setContentView(R.layout.view);
//LayoutInflater inflater = getLayoutInflater();
//View myView = inflater.inflate(R.layout.view, null);
//TextView myTextView = (TextView)myView.findViewById(R.id.textfile1);
//view1.text1.setTextSize(50);
// myTextView.setTextSize( 5);
//setContentView(R.layout.view);
view1 obj =new view1();
obj.small();
}
else if(String.valueOf(spinner1.getSelectedItem())=="Medium")
{
// code
}
else if(String.valueOf(spinner1.getSelectedItem())=="Large")
{
// code
}
}
});
view class code :
public class view1 extends menu {
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.view);
text1=(TextView)findViewById(R.id.textfile1);
Intent myintent = getIntent();
String product = myintent.getStringExtra("product");
text1.setText(product);
}
public void small() {
text1.setText("small font");
}
}
You should use SharedPreferences. This allows you to store the font settings in your Settings class and load them from the View class.
You need to pass activity instance to the class for ex using constructor. and now u can find view or can call methods of that activity class
Alright, so I'm new to Android programming, so far my experience has been quite interesting and challenging. But I fear I have now encountered the first problem I'm not able to overcome on my own.
Simply put, all I want to do is have 2 Spinners:
1 for country selection
1 for province/state selection
What I want to accomplish is that when the user selects his/her country the province/state Spinner is updated with the correct adapter. Currently I'm only using 2 country for testing purposes.
When I launch the Activity, I get an exception and my app crashes.
Here's my code, any pointers would be appreciated !
public class ManageAccountActivity extends Activity {
final ArrayAdapter<CharSequence> adapterSexe = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.sex_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterProvince = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.province_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterStates = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.state_array_fr, android.R.layout.simple_spinner_item);
final ArrayAdapter<CharSequence> adapterCountry = ArrayAdapter.createFromResource(ManageAccountActivity.this, R.array.country_array_fr, android.R.layout.simple_spinner_item);
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_management);
Spinner spinSexe = (Spinner) findViewById(R.id.spin_sex);
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
Spinner spinCountry = (Spinner) findViewById(R.id.spin_country);
adapterSexe.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterProvince.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterStates.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapterCountry.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinSexe.setAdapter(adapterSexe);
spinProvince.setAdapter(adapterProvince);
spinCountry.setAdapter(adapterCountry);
spinCountry.setOnItemSelectedListener(new CountryOnItemSelectedListener());
}
public class CountryOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
setContentView(R.layout.account_management);
Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province);
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
}
Here's the LogCat message I'm getting.
01-10 20:41:01.024: E/AndroidRuntime(1275):
java.lang.RuntimeException: Unable to instantiate activity
ComponentInfo{gggolf.android.minutegolf/gggolf.android.minutegolf.ManageAccountActivity}:
java.lang.NullPointerException
You cannot instantiate your ArrayAdapter directly on class attribut, because createFromResource() use Context, and it's not exists at this time, do it in onCreate() methode instead.
In addition, you get spin province wrong in your listener, you can't call findViewById on view local variable, because it's not your layout, but an inflate of android.R.layout.simple_spinner_item
The good way:
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
Try having your Activity implement AdapterView.OnItemSelectedListener itself. Notice I've moved the Spinners and left out some of your code and replaced it with comments - make sure you include it where necessary...
public class ManageAccountActivity extends Activity
implements AdapterView.OnItemSelectedListener {
// Your ArrayAdapters as before
Spinner spinSexe = null;
Spinner spinProvince = null;
Spinner spinCountry = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.account_management);
spinSexe = (Spinner) findViewById(R.id.spin_sex);
spinProvince = (Spinner) findViewById(R.id.spin_province);
spinCountry = (Spinner) findViewById(R.id.spin_country);
// Call setDropDownViewResource on your ArrayAdapters
// Call setAdapter on your Spinners
spinCountry.setOnItemSelectedListener(this);
}
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
if (parent.getItemAtPosition(pos).toString().equals("Canada")) {
spinProvince.setAdapter(adapterProvince);
} else {
spinProvince.setAdapter(adapterStates);
}
}
public void onNothingSelected(AdapterView parent) {
}
}
Why do you have setContentView(R.layout.account_management); in your onItemSelected() method? That should not be necessary.
Furthermore, you should instantiate your adapters in the onCreate() method of your activity, and pass your actual activity instance as the context.
And the code for retrieving the Spinner object in the select listener should be changed from
Spinner spinProvince = (Spinner) view.findViewById(R.id.spin_province);
into
Spinner spinProvince = (Spinner) findViewById(R.id.spin_province);
Calling findViewById() on the local view object in your select listener will return NULL because the view does not contain the Spinner.
I am trying to change textview properties of a textview that looks like this:
In a seperate Activity that looks like this:
I tried to do this with bundles but I can't get it to work.
This is how my BookActivity looks like:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.book_activity);
//this is where the size property comes in
Integer size = getIntent().getExtras().getInt("SGRkey");
TextView test2 = (TextView) findViewById(R.id.booktext);
test2.setTextSize(size);
Spinner spinner = (Spinner) findViewById(R.id.kapitelspinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.kapitel_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
}
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] theporn = getResources().getStringArray(R.array.allkapitel);
TextView text = (TextView) findViewById(R.id.booktext);
text.setText(theporn[pos]);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
(i pick the chapter string in the spinner and that works just fine.)
And this is how my SettingsActivity looks like:
public class SettingsActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_view);
Spinner SGRspinner = (Spinner) findViewById(R.id.schriftgroeße_spinner);
ArrayAdapter<CharSequence> SGRadapter = ArrayAdapter.createFromResource(
this, R.array.schriftgroesse_list, android.R.layout.simple_spinner_item);
SGRadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
SGRspinner.setAdapter(SGRadapter);
}
public class SGROnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Intent answer = new Intent();
Toast.makeText(parent.getContext(),
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
final String[] SGRstring = getResources().getStringArray(R.array.schriftgroesse_list);
int SGRint = Integer.parseInt(SGRstring[pos]);
Bundle size = new Bundle();
size.putInt("SGRkey", SGRint);
Intent nextActivity = new Intent(com.asm.reader.SettingsActivity.this, com.asm.reader.BookActivity.class);
nextActivity.putExtras(size);
com.asm.reader.SettingsActivity.this.startActivity(nextActivity);
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
I get an error when I try this. All activities are declared in the manifest. I really don't know how to go on. I'm pretty new to this, so sorry if this is something simple, but any help would be greatly appreciated!! :-)
Make your textview static. That is, declare it as a public static class variable. Then you can call it directly from the other activity like this: firstActivity.myTextView.setText("foo");
following code is throwing a NullPointerException:
public class test extends Activity implements OnItemSelectedListener {
private TextView explanation;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.psqlpicker);
explanation = (TextView) findViewById(R.id.picker_explanation_text);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.picker_array,
android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setOnItemSelectedListener(new test());
spinner.setAdapter(adapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
explanation = (TextView) findViewById(R.id.picker_explanation_text);
}
}
It is thrown because of explanation = (TextView) findViewById(R.id.picker_explanation_text); in the onItemSelected(...) method, but I have no idea why. It is, after all, working in the onCreate(...) method.
Instead of
spinner.setOnItemSelectedListener(new test());
use
spinner.setOnItemSelectedListener(this);
You want to use your real activity as the target; you are creating a new object that is never initialized with a context so when it is called it crashes.