2

我正在尝试创建一个活动RateCardActivity,其中有一个微调器。我的布局文件RateCardActivityrate_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>

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

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

4

2 回答 2

5

改进 :public class RateCardActivity extends Activity 并添加RateCardActivityAndroidManifiest.xml

于 2012-10-09T06:32:48.437 回答
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" >
于 2012-10-09T06:33:03.200 回答