【问题标题】:NullPointerException using fragments with textviewNullPointerException 使用带有 textview 的片段
【发布时间】:2012-11-09 03:36:10
【问题描述】:

我在tv.setMovementMethod(new ScrollingMovementMethod()); 上收到了NullPointerException,但我完全不知道为什么!我已经在 XML 文件中声明了 textView 并在 onCreateView 中对其进行了初始化。任何帮助,将不胜感激。

这里是java代码的相关部分:

public class TestConnection extends Fragment {
    public TestConnection() {

    }


    JSONParser jParser = new JSONParser();

    //button pressed to get the items
    Button getAllItems;

    //declare list view
    TextView tv;

    //Progress dialog
    private ProgressDialog pDialog;

    //Make an arrayList containing all items
    ArrayList<HashMap<String, String>> itemsList;

    //url to get items
    private static String urlAllItems = "http://jamesalfei.netne.net/php/get_all_items.php";

    //JSON node/tag names
    private static final String TAG_SUCCESS = "success";
    private static final String TAG_ITEMS = "items";
    private static final String TAG_PID = "pid";
    private static final String TAG_NAME = "name";

    //items array
    JSONArray items = null;

    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.sql_layout,
                container, false);
        getAllItems = (Button) getActivity().findViewById(R.id.SQLGetItems);

        //listview
        tv = (TextView) getActivity().findViewById(R.id.sqltv); 

        tv.setMovementMethod(new ScrollingMovementMethod());

        getAllItems.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // Launching All products Activity
                retreiveProducts();
            }
        });
        return view;
    }

这里是xml

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <Button
            android:id="@+id/SQLGetItems"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:text="@string/getSQL" />

        <TextView
            android:id="@+id/sqltv"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentRight="true"
            android:layout_below="@+id/SQLGetItems"
            android:text="@string/sqlLabel" />

    </RelativeLayout>

抱歉,如果我使用了任何不好的编码做法,但我对 android 编程非常陌生!

【问题讨论】:

  • 发布您的 LogCat 错误。

标签: java android xml textview nullpointerexception


【解决方案1】:

您正在错误的视图中寻找您的 TextView。您应该搜索膨胀视图,而不是父视图,主要是因为您没有添加到 onCreateView() 中。所以替换:

tv = (TextView) getActivity().findViewById(R.id.sqltv); 

有了这个

tv = (TextView) view.findViewById(R.id.sqltv); 

【讨论】:

  • 非常感谢!我将其更改为“查看”。对于 textView 和 Button ,现在效果很好!谢谢!但是现在我的 SQL 连接出现错误...重新开始工作!
  • 知道如何解决我的问题吗? stackoverflow.com/questions/39907625/…
【解决方案2】:

视图没有附加到任何东西,因为您使用false 作为inflate() 中的第三个参数。因此,您需要将 findViewById() 范围限定为具有您的 TextView 的布局。使用:

tv = (TextView) view.findViewById(R.id.sqltv); 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-11-28
    • 1970-01-01
    • 1970-01-01
    • 2014-12-05
    • 1970-01-01
    • 2020-06-19
    • 2021-07-13
    相关资源
    最近更新 更多