【问题标题】:Android - Dynamic Table Rows NullPointerExceptionAndroid - 动态表行 NullPointerException
【发布时间】:2012-07-31 16:12:12
【问题描述】:

我正在制作一个 android 应用程序,它根据从您的偏好中读取的字符串创建一个动态界面:

ScrollView sv = new ScrollView(this);
    LinearLayout ll = new LinearLayout(this);
    ll.setOrientation(LinearLayout.VERTICAL);

    TableLayout tl = new TableLayout(this);
    TableRow[] tr = null;

    SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
    String Favs = SP.getString("Favs", "None");
    String[] Genveje;
    if (!Favs.equals("None"))
    {
        Genveje = Favs.split(";");
        tr = new TableRow[Genveje.length];
        for (int i = 0; i < Genveje.length; i++)
        {
            String BtnName = Genveje[i].split("@")[0];
            String BtnPath = Genveje[i].split("@")[1];
            Button btn = new Button(this);

            btn.setText(BtnName);
            btn.setTag(BtnPath);
            btn.setOnClickListener(this);
            btn.setOnLongClickListener(this);


            tr[i].addView(btn);

                    .........
        }

问题是:当我尝试用 tr[i] 做任何事情时,我得到一个 NullPointerException,我不知道为什么。

有人有建议吗?

【问题讨论】:

    标签: android user-interface nullpointerexception tablelayout tablerow


    【解决方案1】:
    tr[i]=new TableRow(this);
    tr[i].addView(btn);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      相关资源
      最近更新 更多