从mainActivity动态添加到片段中的listView

时间:2016-07-01 10:34:22

标签: android listview android-fragments fragment

我在TabLayout中有一个包含列表的片段 我希望每次收到推送通知时填写该列表(来自MainActivity.OnCreate

TabFragment2.java:

public class TabFragment2 extends Fragment {

    ListView lv;
    List<String> your_array_list;
    ArrayAdapter<String> arrayAdapter;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
        lv = (ListView)view.findViewById(R.id.listView);
        return view;
    }

    public void addToList(String str) {

        your_array_list = new ArrayList<String>();
        your_array_list.add(str);

        arrayAdapter = new ArrayAdapter<String>(
                getActivity(),
                android.R.layout.simple_list_item_1,
                your_array_list );
        lv.setAdapter(arrayAdapter);



    }

}

MainActivity.java:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
...
...
...
        Bundle extras = getIntent().getExtras();
        // If we returned from a notification
        if(extras !=null) {
            String value = extras.getString("keynme");
            TabFragment2 tab2 = new TabFragment2();
            tab2.addToList("faaa");
        }

    }
}

问题是当MainActivity调用时,TabFragment2在getActivity()上崩溃。 主要活动是否必须通过片段活动?它还会被添加到片段中吗?

修改 崩溃日志:

07-01 13:41:50.315 20922-20922/com.push.myapplication E/AndroidRuntime: 

FATAL EXCEPTION: main
Process: com.push.myapplication, PID: 20922
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.push.myapplication/com.push.myapplication.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442)
  at android.app.ActivityThread.access$800(ActivityThread.java:156)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351)
  at android.os.Handler.dispatchMessage(Handler.java:102)
  at android.os.Looper.loop(Looper.java:211)
  at android.app.ActivityThread.main(ActivityThread.java:5373)
  at java.lang.reflect.Method.invoke(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:372)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.Object android.content.Context.getSystemService(java.lang.String)' on a null object reference
  at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
  at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:153)
  at com.push.myapplication.TabFragment2.addToList(TabFragment2.java:44)
  at com.push.myapplication.MainActivity.onCreate(MainActivity.java:59)
  at android.app.Activity.performCreate(Activity.java:5990)
  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2332)
  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2442) 
  at android.app.ActivityThread.access$800(ActivityThread.java:156) 
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1351) 
  at android.os.Handler.dispatchMessage(Handler.java:102) 
  at android.os.Looper.loop(Looper.java:211) 
  at android.app.ActivityThread.main(ActivityThread.java:5373) 
  at java.lang.reflect.Method.invoke(Native Method) 
  at java.lang.reflect.Method.invoke(Method.java:372) 
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1020) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:815)

4 个答案:

答案 0 :(得分:0)

我认为你应该使用interface来实现这个

答案 1 :(得分:0)

更好地将您的片段实例创建为TabFragment2 tab2 = TabFragment2.newInstance();而不是TabFragment2 tab2 = new TabFragment2();

答案 2 :(得分:0)

如果在相应片段的onAttach之前调用它,则getActivity()可以返回null。

可能的方法:将数组your_array_list作为类变量并在onActivityCreated方法中调用Adapter初始化

答案 3 :(得分:0)

你不能这样做

if(extras !=null) {
            String value = extras.getString("keynme");
            TabFragment2 tab2 = new TabFragment2();
            tab2.addToList("faaa");// if your fragment is not attached to activity subsequent call will crash app.
        }


arrayAdapter = new ArrayAdapter<String>(
                getActivity()// if your fragment is not attached to any activity this will return null,
                android.R.layout.simple_list_item_1,
                your_array_list );