代码无效。 Android中的下拉菜单

时间:2014-05-25 10:54:14

标签: android drop-down-menu

这是代码的java文件,后跟xml文件。 程序显示错误“意外停止工作” 我也无法通过logCat找到错误。有人可以建议吗?

Java文件:

package com.example.ui;

import android.os.Bundle;
import android.app.ListActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class Tp extends ListActivity implements OnItemSelectedListener{

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_tp);

    String[] technology = {"PHP", "Ruby", "Java", "SQL"};
    ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
    final Spinner spinnertech = (Spinner) findViewById(R.id.spinner);
    spinnertech.setAdapter(adp);
    spinnertech.setOnItemSelectedListener(this);

}


@Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,
        long arg3) {

    int position = arg0.getSelectedItemPosition();

    if(position == 0) {
        Toast.makeText(getApplicationContext(), " 0 selected",
                   Toast.LENGTH_LONG).show();
    } else if(position == 1) {
        Toast.makeText(getApplicationContext(), " 1 selected",
                   Toast.LENGTH_LONG).show();
    } 

    else {
        Toast.makeText(getApplicationContext(), " extra selected",
                   Toast.LENGTH_LONG).show();}
}

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


}

}

XML文件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">


    <TextView
    android:id="@+id/textView100"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    />


<Spinner
    android:id="@+id/spinner"
    android:layout_width="248dp"
    android:layout_height="36dp"
    android:prompt="@string/title" />

</LinearLayout>

这是LogCat

05-25 16:33:31.270: D/AndroidRuntime(566): Shutting down VM

05-25 16:33:31.270: W/dalvikvm(566): threadid=1: thread exiting with uncaught exception (group=0x4001d800)

05-25 16:33:31.289: E/AndroidRuntime(566): FATAL EXCEPTION: main

05-25 16:33:31.289: E/AndroidRuntime(566): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ui/com.example.ui.Tp}: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.os.Handler.dispatchMessage(Handler.java:99)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.os.Looper.loop(Looper.java:123)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.ActivityThread.main(ActivityThread.java:4627)

05-25 16:33:31.289: E/AndroidRuntime(566):  at java.lang.reflect.Method.invokeNative(Native Method)

05-25 16:33:31.289: E/AndroidRuntime(566):  at java.lang.reflect.Method.invoke(Method.java:521)

05-25 16:33:31.289: E/AndroidRuntime(566):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)

05-25 16:33:31.289: E/AndroidRuntime(566):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

05-25 16:33:31.289: E/AndroidRuntime(566):  at dalvik.system.NativeStart.main(Native Method)

05-25 16:33:31.289: E/AndroidRuntime(566): Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.ListActivity.onContentChanged(ListActivity.java:245)

05-25 16:33:31.289: E/AndroidRuntime(566):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.Activity.setContentView(Activity.java:1647)

05-25 16:33:31.289: E/AndroidRuntime(566):  at com.example.ui.Tp.onCreate(Tp.java:18)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)

05-25 16:33:31.289: E/AndroidRuntime(566):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

05-25 16:33:31.289: E/AndroidRuntime(566):  ... 11 more

05-25 16:33:33.089: I/Process(566): Sending signal. PID: 566 SIG: 9

2 个答案:

答案 0 :(得分:1)

我想在下面的陈述中有问题

Toast.makeText(getApplicationContext(), " 0 selected",
                   Toast.LENGTH_LONG).show();

更改为

Toast.makeText(this, " 0 selected",
                   Toast.LENGTH_LONG).show();

Toast.makeText(tp.this, " 0 selected",
                   Toast.LENGTH_LONG).show();

编辑:--------------------------------------------- -----------------------------

public class Tp extends Activity implements OnItemSelectedListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tp);

        String[] technology = {"PHP", "Ruby", "Java", "SQL"};
        ArrayAdapter<String> adp = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, technology);
        final Spinner spinnertech = (Spinner) findViewById(R.id.spinner);
        spinnertech.setAdapter(adp);
        spinnertech.setOnItemSelectedListener(this);

    }

如果这不起作用,那么在xml文件中包含listview。

答案 1 :(得分:0)

这是你的问题

java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

如果您使用的是ListActivity,则您的布局文件R.layout.activity_tp必须具有ListView android:id="@android:id/list"属性。请看这里:http://developer.android.com/reference/android/app/ListActivity.html例如。