在学习android SDK开发过程中经常遇到这样的问题:
android小程序在eclipse上没有错误提示,在模拟器上运行时出错了把代码贴出来 高手帮忙看看 哪里出错了~ package irdc.ex04_19;import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;public class EX04_19 extends Activity {
private static final String[] array =
{
"sunday","monday","tuesday","wednesday","thursday",
"friday","saturday"
};
LinearLayout myLinearLayout;
TextView myTextView;
ListView myListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//添加LInearLayout
myLinearLayout = new LinearLayout(this);
myLinearLayout.setOrientation(LinearLayout.VERTICAL);
myLinearLayout.setBackgroundColor(android.graphics.Color.WHITE);
//添加TextView
myTextView = new TextView(this);
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
myTextView.setText(R.string.title);
myTextView.setTextColor(getResources().getColor(R.drawable.blue));
//将TextView添加到myLinearLayout
myLinearLayout.addView(myTextView,param1);
//添加ListView
myListView = new ListView(this);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams
(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
myListView.setBackgroundColor(getResources().getColor(R.drawable.ltgray));
//将ListView添加到myLinearLayout
myLinearLayout.addView(myListView,param2);
//将LinearLayout添加到ContentView
setContentView(myLinearLayout);
//new ArrayAdapter对象并将array字符串数组传入
ArrayAdapter<String> adapter =
new ArrayAdapter<String>
(this, R.layout.my_simple_list_item, array);
//将ArrayAdapter添加到ListView对象中
myListView.setAdapter(adapter);
//myListView添加到OnItemSelectedListener
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected
(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
//使用getSelectedItem()将选择的值带入myTextView中
myTextView.setText("你选择的是"+ arg0.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
//TODO Auto-generated method stub
}
});
//myListView添加OnItemClickListener
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick
(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
//使用String[index],arg2为单击到ListView的index,并将值带入myTextView中
myTextView.setText("你选的是"+array[arg2]);
}
});
}
}res/layout/my_simple_list_item.xml<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myCheckedTextView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@drawable/black"
/>res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="black">#000000</drawable>
<drawable name="blue">#0000FF</drawable>
<drawable name="ltgray">#D3D3D3</drawable>
</resources>
为了大家能更清楚的找到错误
我就比较详细的都贴了出来了~
android小程序在eclipse上没有错误提示,在模拟器上运行时出错了把代码贴出来 高手帮忙看看 哪里出错了~ package irdc.ex04_19;import android.app.Activity;
import android.os.Bundle;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.view.View;public class EX04_19 extends Activity {
private static final String[] array =
{
"sunday","monday","tuesday","wednesday","thursday",
"friday","saturday"
};
LinearLayout myLinearLayout;
TextView myTextView;
ListView myListView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//添加LInearLayout
myLinearLayout = new LinearLayout(this);
myLinearLayout.setOrientation(LinearLayout.VERTICAL);
myLinearLayout.setBackgroundColor(android.graphics.Color.WHITE);
//添加TextView
myTextView = new TextView(this);
LinearLayout.LayoutParams param1 = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
myTextView.setText(R.string.title);
myTextView.setTextColor(getResources().getColor(R.drawable.blue));
//将TextView添加到myLinearLayout
myLinearLayout.addView(myTextView,param1);
//添加ListView
myListView = new ListView(this);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams
(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT
);
myListView.setBackgroundColor(getResources().getColor(R.drawable.ltgray));
//将ListView添加到myLinearLayout
myLinearLayout.addView(myListView,param2);
//将LinearLayout添加到ContentView
setContentView(myLinearLayout);
//new ArrayAdapter对象并将array字符串数组传入
ArrayAdapter<String> adapter =
new ArrayAdapter<String>
(this, R.layout.my_simple_list_item, array);
//将ArrayAdapter添加到ListView对象中
myListView.setAdapter(adapter);
//myListView添加到OnItemSelectedListener
myListView.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
@Override
public void onItemSelected
(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
//使用getSelectedItem()将选择的值带入myTextView中
myTextView.setText("你选择的是"+ arg0.getSelectedItem().toString());
}
@Override
public void onNothingSelected(AdapterView<?> arg0)
{
//TODO Auto-generated method stub
}
});
//myListView添加OnItemClickListener
myListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
{
@Override
public void onItemClick
(AdapterView<?> arg0,View arg1,int arg2,long arg3)
{
//使用String[index],arg2为单击到ListView的index,并将值带入myTextView中
myTextView.setText("你选的是"+array[arg2]);
}
});
}
}res/layout/my_simple_list_item.xml<?xml version="1.0" encoding="utf-8"?>
<CheckedTextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myCheckedTextView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@drawable/black"
/>res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>res/values/color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<drawable name="black">#000000</drawable>
<drawable name="blue">#0000FF</drawable>
<drawable name="ltgray">#D3D3D3</drawable>
</resources>
为了大家能更清楚的找到错误
我就比较详细的都贴了出来了~
07-30 03:00:33.734: ERROR/vold(27): Error opening switch name path '/sys/class/switch/test' (No such file or directory)
07-30 03:00:33.734: ERROR/vold(27): Error bootstrapping switch '/sys/class/switch/test' (No such file or directory)
07-30 03:00:33.734: ERROR/vold(27): Error opening switch name path '/sys/class/switch/test2' (No such file or directory)
07-30 03:00:33.734: ERROR/vold(27): Error bootstrapping switch '/sys/class/switch/test2' (No such file or directory)
07-30 03:00:45.592: ERROR/BatteryService(52): usbOnlinePath not found
07-30 03:00:45.592: ERROR/BatteryService(52): batteryVoltagePath not found
07-30 03:00:45.592: ERROR/BatteryService(52): batteryTemperaturePath not found
07-30 03:00:45.611: ERROR/SurfaceFlinger(52): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
07-30 03:00:50.242: ERROR/EventHub(52): could not get driver version for /dev/input/mouse0, Not a typewriter
07-30 03:00:50.242: ERROR/EventHub(52): could not get driver version for /dev/input/mice, Not a typewriter
07-30 03:00:50.521: ERROR/System(52): Failure starting core service
07-30 03:00:50.521: ERROR/System(52): java.lang.SecurityException
07-30 03:00:50.521: ERROR/System(52): at android.os.BinderProxy.transact(Native Method)
07-30 03:00:50.521: ERROR/System(52): at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
07-30 03:00:50.521: ERROR/System(52): at android.os.ServiceManager.addService(ServiceManager.java:72)
07-30 03:00:50.521: ERROR/System(52): at com.android.server.ServerThread.run(SystemServer.java:176)
07-30 03:00:50.531: ERROR/AndroidRuntime(52): Crash logging skipped, no checkin service
07-30 03:00:59.122: ERROR/ActivityThread(94): Failed to find provider info for android.server.checkin
07-30 03:01:00.752: ERROR/ActivityThread(94): Failed to find provider info for android.server.checkin
07-30 03:01:00.932: ERROR/ActivityThread(94): Failed to find provider info for android.server.checkin
07-30 03:01:03.293: ERROR/MediaPlayerService(31): Couldn't open fd for content://settings/system/notification_sound
07-30 03:01:03.322: ERROR/MediaPlayer(52): Unable to to create media player
07-30 03:01:13.342: ERROR/AndroidRuntime(139): ERROR: thread attach failed
07-30 03:01:17.613: ERROR/AndroidRuntime(208): ERROR: thread attach failed
不过今天重新启动了下模拟器既然正常运行了
难以理解