gpt4 book ai didi

java - 自定义列表开始新 Activity ?

转载 作者:行者123 更新时间:2023-12-01 05:39:52 25 4
gpt4 key购买 nike

我构建了一个自定义列表项,通过膨胀每个列表项来替换 Android 的简单列表项。自定义列表正在运行,它由 ImageView TextView 组成。

我的问题是,当我在单击列表项后尝试启动新的 Activity 时,没有任何反应,即使应用程序不会崩溃。那么,有没有办法使用 ListView 启动 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;
}
}
}

这是第一类

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);
}
}

是的,我的 Activity 已添加到manifest xml 文件中。

感谢您的帮助。

<小时/>

@Vineet Shukla 我不知道该怎么做,我放置了一个断点,但我无法调试,我是 Eclipse 环境的新手,你能解释一下吗

@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

最佳答案

您没有有效地使用此 API,您应该切换到 SimpleAdapter,因为它可以处理更复杂的列表项布局和数据绑定(bind)。您需要有效地使用列表的基础数据,而不是创建棘手的分支语句。

关于java - 自定义列表开始新 Activity ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7434997/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com