Fragment的View元素是Null

时间:2013-10-15 10:30:02

标签: android android-fragments

我有一个Selectl主机活动类表单,我在其中实例化AddHttpLinksFragment。之后我调用了Fragment的getPls()方法。只要我没有在Fragments的View方法中调用任何getPls()元素(小部件,适配器),一切正常。似乎当我调用此方法时,片段的视图尚未创建,因此它的小部件是Null。我该如何防止这种情况?

Selectl.class:

        fragmentManager = getFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        AddHttpLinksFragment fragment2 = new AddHttpLinksFragment();
        fragmentTransaction.add(R.id.fragment_container1, fragment2, "addlinksF");
        fragmentTransaction.commit();
        fragmentManager.executePendingTransactions();

         AddHttpLinksFragment fragment = (AddHttpLinksFragment) getFragmentManager().findFragmentByTag("addlinksF");
        fragment.getPls(path);

AddHttpLinksFragmen.class:

public class AddHttpLinksFragment extends android.app.Fragment implements View.OnClickListener {

EditText httpAddressEdit;
EditText linkText;
URI uriPls;
ListView lv;
ArrayList<String> strList = new ArrayList<String>();
ArrayAdapter<String> arrayAdapter;
int itemPosition;
DBHttpLinks db;


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

    setRetainInstance(true);
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.addhttplinksfragment, container, false);

    // ListView init
    lv = (ListView) v.findViewById(R.id.listView);

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

    return v;

}

// ...

public void getPls(Uri path) {

    try {
        uriPls = new URI(path.toString());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    PlsReader mPlsReader = new PlsReader(uriPls);
    strList.addAll(mPlsReader.Read());

   arrayAdapter.notifyDataSetChanged();     // <--- ANR, line 233
    TextView tv = (TextView) getActivity().findViewById(R.id.textView_long_press); // <--- ANR
    tv.setText(getString(R.string.long_press_to));   // <---ANR
}

// ...

}

logcat的:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.app/com.example.app.Selectl}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
        at android.app.ActivityThread.access$600(ActivityThread.java:153)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:5227)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at com.krisk.astream.AddHttpLinksFragment.getPls(AddHttpLinksFragment.java:233)

2 个答案:

答案 0 :(得分:1)

如果片段没有显示视图,则不会调用onCreateView。所以arrayadapter为null并且不调用此行:

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

更新:

将其放在onCreateView

 TextView tv = (TextView) getActivity().findViewById(R.id.textView_long_press); 
tv.setText(getString(R.string.long_press_to)); 

将您的方法更改为:

public void getPls(Uri path) {

    try {
        uriPls = new URI(path.toString());
    } catch (URISyntaxException e) {
        e.printStackTrace();
    }

    PlsReader mPlsReader = new PlsReader(uriPls);
    strList.addAll(mPlsReader.Read());


}

创建onResume

@Override
public void onResume() {
    super.onResume();  // Always call the superclass method first

    if (strList != null) {       

        arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, strList);
        lv.setAdapter(arrayAdapter);
    }
}

答案 1 :(得分:1)

根据您的评论,您说片段xml中有textview

因此,您需要使用onCreateView

中的膨胀视图对象初始化textview
TextView tv ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    setRetainInstance(true);
    View v = inflater.inflate(R.layout.addhttplinksfragment, container, false);
    lv = (ListView) v.findViewById(R.id.listView);
    tv =  (TextView) view.findViewById(R.id.textView_long_press);
    arrayAdapter = new ArrayAdapter<String>(getActivity(),android.R.layout.simple_list_item_1, strList);
    lv.setAdapter(arrayAdapter);
    return v;

 }

getPls(path)中致电onResume

Resumed

The fragment is visible in the running activity.

http://developer.android.com/guide/components/fragments.html