2012-11-20 90 views
2

我得到NullPointerExceptiontv.setMovementMethod(new ScrollingMovementMethod());但我完全不知道为什么!我已经在XML文件中声明了textView并在onCreateView中初始化它。任何帮助,将不胜感激。NullPointerException使用带有textview的片段

下面是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编程!

+2

安置自己的logcat的错误。 – Sam

回答

5

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

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

与此

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

打败我吧。愿分数是你的;) –

+0

非常感谢你!我将它改为“查看”。对于textView和Button,它现在工作的很棒!谢谢! 但是,现在我有SQL连接错误...返回工作! – James

+0

任何想法如何解决我的? http://stackoverflow.com/questions/39907625/why-is-accessing-textview-of-a-fragment-inside-activity-throwing-an-error – Si8

3

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

tv = (TextView) view.findViewById(R.id.sqltv); 
相关问题