2012-10-09 90 views
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時,應用程序崩潰,我收到消息「應用程序意外停止,請稍後再試。」

我似乎無法理解我做錯了什麼,並想知道如何解決這個問題?

+1

你聲明的RateCardActivity你清單文件? ,你可以發佈logcat –

+0

你錯過了一些事情,首先你擴展活動並檢查XML,我給你一個工作示例代碼.. – Satyam

+0

@Sharath,我沒有聲明清單中的活動。這很粗心。不管怎麼說,還是要謝謝你。 – Ankush

回答

5

改善:public class RateCardActivity extends Activity 並添加RateCardActivityAndroidManifiest.xml

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