gpt4 book ai didi

android - 使用 ListView 选择项设置布局 - Android

转载 作者:行者123 更新时间:2023-11-30 04:54:50 24 4
gpt4 key购买 nike

我有一个问题。

  • 我想使用 ListView 中的选定项目重定向布局。
  • 我想重定向的布局有空 View 对象,我想用所选 ListView 对象中的数据填充它们
  • 但我做不到,我进行了调试,但在第 67 行出现错误单击 ListView 对象。我该怎么办?

请帮我做作业

这段代码在onCreate方法中代码:

    final ListView listView = (ListView) findViewById(R.id.lw_yemekTarifleri);
customAdapter adapter = new customAdapter(this, yemekTarifleriList);
listView.setAdapter(adapter);


listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//Log.i("OnClick", "position = " + position);


ImageView yemekResim = (ImageView) findViewById(R.id.iw_yemekResim);
TextView yemekIsim = (TextView) findViewById(R.id.tw_yemekIsim);
TextView yemekSure = (TextView) findViewById(R.id.tw_yemekSure);
TextView yemekMalzemeler = (TextView) findViewById(R.id.tw_malzemeler);
TextView yemekTarif = (TextView) findViewById(R.id.tw_tarif);

yemekTarifi secilenYemek = yemekTarifleriList.get(position);

yemekResim.setImageResource(secilenYemek.getResim()); //(line 67)!!!!!!!!!!!!
yemekIsim.setText(secilenYemek.getIsim());
yemekSure.setText(secilenYemek.getHazirlanisSuresi());
yemekMalzemeler.setText(secilenYemek.getMalzemeler());
yemekTarif.setText(secilenYemek.getTarif());

setContentView(R.layout.yemek_tarifi);



}
});

错误:

E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.erensayar_yemektarifi, PID: 13293 java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ImageView.setImageResource(int)' on a null object reference at com.example.erensayar_yemektarifi.anaSayfa_yemekTarifleri$1.onItemClick(anaSayfa_yemekTarifleri.java:67) at android.widget.AdapterView.performItemClick(AdapterView.java:318) at android.widget.AbsListView.performItemClick(AbsListView.java:1159) at android.widget.AbsListView$PerformClick.run(AbsListView.java:3136) at android.widget.AbsListView$3.run(AbsListView.java:4052) at android.os.Handler.handleCallback(Handler.java:873) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:193) at android.app.ActivityThread.main(ActivityThread.java:6669) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)

代码已上传至github:github.com/erensayar/android

最佳答案

检查 secilenYemek 对象的值是否真的用值初始化。我的建议是在第 67 行之前设置一个调试点,即(第 66 行)并正确检查 secilenYemek 对象的值。因为它因此抛出空指针异常。

关于android - 使用 ListView 选择项设置布局 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59442944/

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