80,351
社区成员
发帖
与我相关
我的任务
分享
<?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"
>
<ListView
android:id="@+id/MyListView"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</ListView>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:focusable="false"
android:paddingTop="28dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="@+id/chb">
</CheckBox>
<TextView
android:text="重力感应横屏"
android:paddingTop="18dip"
android:id="@+id/topTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20dip">
</TextView>
<TextView
android:text="重力感应自动横竖屏"
android:layout_below="@+id/topTextView"
android:id="@+id/bottomTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TextView>
</RelativeLayout>
package ztwz.test;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CompoundButton;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.*;
import android.widget.CheckBox;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class testActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String,Object>> listitem=new ArrayList<HashMap<String,Object>>();
CheckBox chb=(CheckBox)findViewById(R.id.chb);
ListView mylistview =(ListView) findViewById(R.id.MyListView);
HashMap<String,Object> map =new HashMap<String,Object>();
map.put("Title", "自动横屏");
map.put("ItemText", "自动感应重力横竖屏");
listitem.add(map);
SimpleAdapter listitemAdapter = new SimpleAdapter(this,
listitem,
R.layout.mylistviewstyle,
new String[]{"Title","ItemText"},
new int [] {R.id.topTextView,R.id.bottomTextView}
);
mylistview.setAdapter(listitemAdapter);
问题:添加上这个单击事件后在模拟器中就报错,删除就可以正常,为是为什么?哪错了?
模拟器中报:the applicatio 重力横竖屏(process ztwz.test) has stopped unexpectedly.please try again.
chb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
}
});
//mylistview单击监听
mylistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
CheckBox chb=(CheckBox)findViewById(R.id.chb);
if(chb.isChecked())
{
Toast.makeText(testActivity.this, "重力感应自动横屏开启",1000).show();
}
else
{
Toast.makeText(testActivity.this, "重力感应自动横屏关闭",1000).show();
}
}
});
}
}
package ztwz.test;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CompoundButton;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.View.OnCreateContextMenuListener;
import android.widget.*;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.CompoundButton.OnCheckedChangeListener;
public class testActivity extends Activity {
/** Called when the activity is first created. */
private MyAdapter myAdapter;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String,Object>> listitem=new ArrayList<HashMap<String,Object>>();
myAdapter=new MyAdapter();
CheckBox chb=(CheckBox)findViewById(R.id.chb);
ListView mylistview =(ListView) findViewById(R.id.MyListView);
HashMap<String,Object> map =new HashMap<String,Object>();
map.put("Title", "自动横屏");
map.put("ItemText", "自动感应重力横竖屏");
listitem.add(map);
SimpleAdapter listitemAdapter = new SimpleAdapter(this,
listitem,
R.layout.mylistviewstyle,
new String[]{"Title","ItemText"},
new int [] {R.id.topTextView,R.id.bottomTextView}
);
mylistview.setAdapter(listitemAdapter);
//mylistview单击监听
mylistview.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
CheckBox chb=(CheckBox)findViewById(R.id.chb);
chb.setOnCheckedChangeListener(new CheckBox.OnCheckedChangeListener(){
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(isChecked)
{
Toast.makeText(testActivity.this, "重力感应自动横屏开启",1000).show();
}
else
{
Toast.makeText(testActivity.this, "重力感应自动横屏关闭",1000).show();
}
}
});
}
});
}
}