微调器活动不起作用

时间:2012-10-09 06:23:39

标签: android android-spinner

我正在尝试创建一个活动RateCardActivity,其中包含一个微调器。我RateCardActivity的布局文件是rate_card。我的RateCardActivity如下所示。

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);

}

}

布局文件rate_card是:

<?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>

使用intent从另一个活动调用RateCardActivity(我确信代码的这一部分没有任何问题,因为当我用另一个活动替换RateCardActivity时,应用程序正常工作) 。当我尝试在模拟器中打开应用程序中的RateCardActivity时,应用程序崩溃,我收到消息“应用程序意外停止。请稍后再试。”

我似乎无法理解我做错了什么,想知道如何纠正这个问题?

2 个答案:

答案 0 :(得分:5)

改进:public class RateCardActivity extends Activity 并将RateCardActivity添加到AndroidManifiest.xml

答案 1 :(得分:1)

您好,您可以通过这种方式使用微调器活动,我提供了帮助的示例代码..

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" >