簡體   English   中英

使用帶有textview的片段的NullPointerException

[英]NullPointerException using fragments with textview

我在tv.setMovementMethod(new ScrollingMovementMethod());上獲得了NullPointerException tv.setMovementMethod(new ScrollingMovementMethod()); 但我完全不知道為什么! 我在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編程非常新!

您正在錯誤的視圖中查找TextView。 您應該搜索膨脹的視圖,而不是父視圖,主要是因為您沒有在onCreateView() 所以替換:

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

有了這個

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

視圖未附加到任何內容,因為您使用false作為inflate()的第三個參數。 因此,您需要將findViewById()范圍擴展到具有TextView的布局。 采用:

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM