Reputation: 6927
I'm trying to create an activity, RateCardActivity
, which has a spinner in it. My layout file for RateCardActivity
is rate_card
. My RateCardActivity
looks like the following.
public class RateCardActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.rate_card);
Spinner spinner = (Spinner) findViewById(R.id.select_city);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.select_city, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
The layout file rate_card
is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.olacabs.customer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:color/darker_gray"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="@string/rate_card"
android:textColor="@color/white"
android:textSize="20dp"
custom:customFont="litera_bold.ttf" />
<Spinner
android:id="@+id/select_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The RateCardActivity
is called from another activity using an intent (I'm sure there is nothing wrong with that part of the code as when I substitute RateCardActivity
with another activity, the application works fine). When I try to open the RateCardActivity
in the application in emulator, the application crashes and I got the message "The application has stopped unexpectedly. Please try again later."
I can't seem to understand what I'm doing wrong, and want to know how to correct this?
Upvotes: 2
Views: 381
Reputation: 1682
Hi you can use spinner activity by this way, I gave a sample code for the help..
public class MyActivity extends Activity {
public static EditText edtsample;
public static EditText edtchannel;
public static EditText edtencoding;
private static Spinner samplespin;
private static Spinner channelspin;
private static Spinner encodingspin;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
edtsample = (EditText)findViewById(R.id.audvalue1);
edtchannel = (EditText)findViewById(R.id.chanvalue1);
edtencoding = (EditText)findViewById(R.id.encodingvalue1);
edtchannel.setFocusable(false);
edtchannel.setClickable(false);
edtencoding.setFocusable(false);
edtencoding.setClickable(false)
samplespin = (Spinner) findViewById(R.id.audspinner1);
samplespin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
edtsample.setText(parent.getItemAtPosition(position).toString());
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
edtsample.setText("");
}
});
channelspin = (Spinner) findViewById(R.id.chanspinner1);
channelspin.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(parent.getItemAtPosition(position).equals("CHANNEL_PHONE")){
edtchannel.setText(R.string.chan1);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
if(parent.getItemAtPosition(position).equals("CHANNEL_CD")){
edtchannel.setText(R.string.chan2);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
if(parent.getItemAtPosition(position).equals("CHANNEL_HD")){
edtchannel.setText(R.string.chan2);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
edtchannel.setText("");
}
});
**And in XML part you do by this**
<Spinner
android:id="@+id/audspinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/audioText1"
android:spinnerMode= "dropdown"
android:entries="@array/sample_array" />
<EditText
android:id="@+id/audvalue1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/audspinner1"
android:hint="Enter Sampling Rate"
android:ems="10" >
Upvotes: 1
Reputation: 1667
Improve :public class RateCardActivity extends Activity
and add RateCardActivity
to AndroidManifiest.xml
Upvotes: 5