-1

我有一个Selectl宿主 Activity 类表单,我在其中实例化我的AddHttpLinksFragment.Just 之后我调用 Fragment 的getPls()方法。只要我没有View在 Fragments 的getPls()方法中调用任何元素(小部件、适配器),一切都会正常工作。似乎当我调用这个方法时,片段的视图还没有创建,所以它的小部件是Null. 我怎样才能防止这种情况?

选择类:

        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);

添加HttpLinksFragmen.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
}

// ...

}

日志猫:

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)
4

2 回答 2

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);
    }
}
于 2013-10-15T10:41:58.873 回答
1

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

所以你需要用膨胀的视图对象初始化textviewonCreateView

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

于 2013-10-15T10:50:35.280 回答