Android:在Fragment中动态添加TextView - 保持gettin null引用

时间:2017-03-15 15:34:26

标签: java android android-fragments

我刚开始为Android开发,我正在尝试动态地将TextView对象添加到片段布局中,但是我一直得到这个空引用错误:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.takemeout/com.takemeout.android.event.EventOverviewActivity}: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                                                                    Caused by: android.view.InflateException: Binary XML file line #16: Binary XML file line #16: Error inflating class fragment
                                                                    Caused by: android.view.InflateException: Binary XML file line #16: Error inflating class fragment
                                                                    Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.addView(android.view.View)' on a null object reference
                                                                       at com.takemeout.android.event.EventListFragment.onCreateView(EventListFragment.java:36)
                                                                       at android.support.v4.app.Fragment.performCreateView(Fragment.java:2189)
                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1255)
                                                                       at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1472)
                                                                       at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1691)
                                                                       at android.support.v4.app.FragmentManagerImpl.onCreateView(FragmentManager.java:3413)
                                                                       at android.support.v4.app.FragmentController.onCreateView(FragmentController.java:120)
                                                                       at android.support.v4.app.FragmentActivity.dispatchFragmentsOnCreateView(FragmentActivity.java:378)
                                                                       at android.support.v4.app.BaseFragmentActivityHoneycomb.onCreateView(BaseFragmentActivityHoneycomb.java:33)
                                                                       at android.support.v4.app.FragmentActivity.onCreateView(FragmentActivity.java:79)
                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:777)
                                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                                       at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                                                                       at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                                                                       at com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
                                                                       at android.app.Activity.setContentView(Activity.java:2414)
                                                                       at com.takemeout.android.event.EventOverviewActivity.onCreate(EventOverviewActivity.java:33)
                                                                       at android.app.Activity.performCreate(Activity.java:6679)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:154)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:6119)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

我不确定如何在onCreateView中初始化TextView对象,因为我尝试过的所有内容都产生了同样的消息。这是我的代码:

package com.takemeout.android.event;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup; 
import android.widget.LinearLayout;
import android.widget.TextView;

import com.takemeout.android.R;

import java.util.Calendar;
import java.util.List;



public class EventListFragment extends Fragment {

  EventOverviewProvider provider = new EventOverviewProvider();
  List<EventOverviewProjection> eventsToday = provider.getTodayEvents();


  @Override
  public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_event_list, container, false);
    //getEventsToday(eventsToday, v);
    LinearLayout ll = (LinearLayout) v.findViewById(R.id.fragment_event_list);

    TextView text = new TextView(this.getActivity());
    text.setText("Sample text");
    ll.addView(text);


    // Inflate the layout for this fragment
    return v;
 }

 @Override
 public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 }



 }

这是我的fragment_event_list.xml:

   <?xml version="1.0" encoding="utf-8"?>
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
       xmlns:app="http://schemas.android.com/apk/res-auto"
       android:orientation="vertical" android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:weightSum="1">


   </LinearLayout>

任何人都知道我可能做错了什么?

3 个答案:

答案 0 :(得分:3)

您的LinearLayout缺少ID。只需将android:id="@+id/fragment_event_list添加到xml中的LinearLayout即可。但是你应该选择一个更好的名字,比如@+id/list_view

答案 1 :(得分:2)

在Xml中添加ID:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_event_list"    //add this
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:weightSum="1">


</LinearLayout>

未正确添加ID。这就是为什么它给出了null。

答案 2 :(得分:0)

将您的onCreateView方法更新为:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                     Bundle savedInstanceState) {

View v = inflater.inflate(R.layout.fragment_event_list, container, false);
//getEventsToday(eventsToday, v);
LinearLayout ll = (LinearLayout) v;

TextView text = new TextView(this.getActivity());
text.setText("Sample text");
ll.addView(text);


// Inflate the layout for this fragment
return v;
}

由于您的父级layout本身就是您要用来添加layout的{​​{1}},因此您只需textView就可以了。