这是代码的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
我猜下面的语句有问题
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。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句