Android中的微调器与片段

时间:2014-05-12 19:26:57

标签: java android spinner

我在android中使用微调器时遇到问题! 有人能告诉我吗?

我在ListViewFragment中的代码是

public class ListViewFragment extends Activity implements OnItemSelectedListener {
    public void ListViewFragments (){}

private String[] state = { "Cupcake", "Donut", "Eclair",
        "Froyo", "Gingerbread", "HoneyComb", "IceCream Sandwich",
        "Jellybean", "kitkat"};

Spinner spinnerOsversions;
TextView selVersion;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    System.out.println(state.length);
    selVersion = (TextView) findViewById(R.id.selVersion);
    spinnerOsversions = (Spinner) findViewById(R.id.osversions);
    ArrayAdapter<String> adapter_state = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, state);
    adapter_state
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
    spinnerOsversions.setAdapter(adapter_state);
    spinnerOsversions.setOnItemSelectedListener(this);

}

public void onItemSelected(AdapterView<?> parent, View view, int position,
        long id) {
    spinnerOsversions.setSelection(position);
    String selState = (String) spinnerOsversions.getSelectedItem();
    selVersion.setText("Selected Android OS:" + selState);
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {

}

类似于fragment_list.xml

<TextView
    android:id="@+id/selVersion"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_below="@+id/osversions"
    android:layout_marginLeft="10dp"
    android:layout_marginTop="20dp" />

<Spinner
android:id="@+id/osversions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below=@+id/textView2"
android:layout_marginTop="38dp" />

我在哪里得到错误是在MainActivity.java

Fragment fragment = null;

            switch (position) {
            case 0:
                fragment = new HomeFragment();
                break;
            case 1:
                fragment = new ListViewFragment();
                break;
            case 2:
                fragment = new StyleFragment();
                break;
            case 3:
                fragment = new DatabaseFragment();
                break;
            case 4:
                fragment = new PrefsFragment();
                break;
            case 5:
                fragment = new WebViewFragment();
                break;
            case 6:
                fragment = new WebServiceFragment();
                break;

            case 7:
                fragment = new RssReaderFragment();
                break;

            default:
                break;
            }

错误消息是:

Description Resource    Path    Location    Type

类型不匹配:无法从ListViewFragment转换为Fragment MainActivity.java / Nagarik Sahayata / src / com / yogeshojha / nagarikshayata line 235 Java问题

1 个答案:

答案 0 :(得分:1)

在这里查看您的代码:

 ListViewFragment extends Activity

==&GT;你的ListViewFragment扩展了Activity类而不是Fragment!

==&GT;重新思考你想做什么。 ListViewFragment实际上应该是片段还是活动?无论如何,你不能像这样将一个Activity压缩成一个片段:fragment = new ListViewFragment();