tabwidget Activity 中的 Android listView 出现异常

发布于 2024-10-08 23:36:12 字数 2138 浏览 8 评论 0原文

使用此处找到的 Google 的“Hello TabWidget”教程我能够构建一个 tabWidget 布局,但我在尝试将列表视图粘贴到其中一个选项卡中时遇到了非常困难的时间。

我还阅读了有关此问题的几个线程,但它们都与将 ListView 分配给特定 id“列表”有关,我已经这样做了,但我仍然遇到空指针异常。

我在这个基本目标上花了很多时间,所以我向上帝寻求帮助,但我认为他现在很忙。有人午休吗?

调用 ListView 的活动

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class SettingsActivity extends Activity  {
private ListView lv;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    String[] settingsList = getResources().getStringArray(R.array.settingsStringArray);

    lv = (ListView) findViewById(R.id.list);
    lv.setAdapter(new ArrayAdapter<String>(this, R.id.TextView01, settingsList));

    lv.setOnItemClickListener
        (new OnItemClickListener() 
            {

                public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
                {

                    Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

在 setContentView(R.layout.settings) 中调用 res/layout 的 xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<ListView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list">
</ListView>
<TextView  
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</TextView>
</LinearLayout>

谢谢大家。

Using Google's "Hello TabWidget" tutorial found here I was able to build a tabWidget layout, but I'm having a seriously difficult time trying to stick a listview within one of the tabs.

I've also read through a couple threads here about this issue, but they all pertain to assigning the ListView to the specific id "list", which I have done, but I still get a null pointer exception.

I have spent wayyyyyyy to much time on this elementary objective, so I've asked God for help, but I think he's busy right now. Anyone on a lunch break?

the activity in which the ListView is called

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


public class SettingsActivity extends Activity  {
private ListView lv;

public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.settings);

    String[] settingsList = getResources().getStringArray(R.array.settingsStringArray);

    lv = (ListView) findViewById(R.id.list);
    lv.setAdapter(new ArrayAdapter<String>(this, R.id.TextView01, settingsList));

    lv.setOnItemClickListener
        (new OnItemClickListener() 
            {

                public void onItemClick(AdapterView<?> parent, View view,int position, long id) 
                {

                    Toast.makeText(getApplicationContext(), ((TextView) view).getText(),Toast.LENGTH_SHORT).show();

                }
            });
        }
    }

xml file from res/layout called within setContentView(R.layout.settings)

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent">
<ListView 
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:id="@+id/list">
</ListView>
<TextView  
    android:id="@+id/TextView01" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content">
</TextView>
</LinearLayout>

Thanks, everyone.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深海少女心 2024-10-15 23:36:12

您的 TextView01 需要进入一个单独的 XML 文件,例如 textview01。 (名称必须为小写,尽管您将在控制台中看到与此相关的唯一错误消息。)然后您可以通过 R.layout.textview01 访问它。

Your TextView01 needs to go into a separate XML file, say textview01. (The name needs to be lowercase, although the only error message you'll see about this will be in the console.) You would then access it as R.layout.textview01.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文