我已经构建了一个自定义列表项,通过膨胀每个列表项来替换android的简单列表项。自定义列表正常运行,它由image view
和text view
组成。
我的问题是,当我点击列表项后尝试启动新的activity
时,即使应用程序不会崩溃也没有任何反应。那么,有没有办法使用列表视图启动活动???
public class activityone extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activitylayout);
setListAdapter(new MyAdapter(this, android.R.layout.simple_list_item_1, R.id.textView1,
getResources().getStringArray(R.array.names)));
}
public void onListItemClick(ListView l, View v, int position,
long id) {
// TODO Auto-generated method stub
super.onListItemClick(l, v, position, id);
Object o = this.getListAdapter().getItem(position);
if(getSelectedItemPosition() == 0){
Intent intent = new Intent(activityone.this,no1.class);
startActivity(intent);
}
.
.
.
.
}
private class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int resource, int textViewResourceId,
String[] strings) {
super(context, textViewResourceId, strings);
// TODO Auto-generated constructor stub
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = inflater.inflate(R.layout.list_item, parent, false);
String[] items = getResources().getStringArray(R.array.names);
ImageView iv = (ImageView) row.findViewById(R.id.imageView1);
TextView tv = (TextView) row.findViewById(R.id.textView1);
tv.setText(items[position]);
if(items[position].equals("john")){
iv.setImageResource(R.drawable.john);
}
.
.
.
.
return row;
}
}
}
这是no1类
public class no1 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.name_profile);
CharSequence t1 = "Name: john";
CharSequence t2 = "Age: 31";
CharSequence t3 = "Nationality: Saudi";
CharSequence t4 = "Number: 765646454";
ImageView iv = (ImageView) findViewById(R.drawable.imageView1);
TextView tv1 = (TextView) findViewById(R.id.textView1);
TextView tv2 = (TextView) findViewById(R.id.textView2);
TextView tv3 = (TextView) findViewById(R.id.textView3);
TextView tv4 = (TextView) findViewById(R.id.textView4);
iv.setImageResource(R.drawable.john);
tv1.setText(t1);
tv2.setText(t2);
tv3.setText(t3);
tv4.setText(t4);
}
}
是的,我的activities
已添加到manifest xml file
。
感谢您的帮助。
@Divyesh
我编辑了代码,当我在此处按下列表项时,应用程序崩溃了是Logcat跟踪
09-17 10:10:57.686: ERROR/AndroidRuntime(365): FATAL EXCEPTION: main
09-17 10:10:57.686: ERROR/AndroidRuntime(365): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.abc.act/com.abc.act.no1}: java.lang.NullPointerException
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.os.Handler.dispatchMessage(Handler.java:99)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.os.Looper.loop(Looper.java:123)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.main(ActivityThread.java:4627)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invokeNative(Native Method)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at java.lang.reflect.Method.invoke(Method.java:521)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at dalvik.system.NativeStart.main(Native Method)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): Caused by: java.lang.NullPointerException
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at com.abc.act.no1.onCreate(no1.java:31)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
09-17 10:10:57.686: ERROR/AndroidRuntime(365): ... 11 more
答案 0 :(得分:0)
您没有有效地使用此API,您应该切换到SimpleAdapter,因为处理更复杂的List Item布局和数据绑定。您需要有效地使用列表的基础数据,而不是创建难以处理的分支语句。