0

我对片段很陌生。我有实现片段的应用程序,它有不同的布局。当应用程序在智能手机和平板电脑中以纵向运行时,它只显示一种布局(带有一个片段),而在平板电脑中横向运行时,它显示两个窗格布局(带有两个片段)。

尝试在片段布局中获取视图 ID 时出现错误。下面是 res/layout 和 res/layout-large 中的 activity_main_screen.xml 代码,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/fragment_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</LinearLayout>

这是 res/layout-large-land 中的 activity_main_screen.xml 代码,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="@color/grey">

    <fragment android:name="com.pongodev.dr.know.QuestionFragment"
              android:id="@+id/question_fragment"
              android:layout_weight="1"
              android:layout_width="0dp"
              android:layout_height="match_parent" 
              android:layout_marginRight="2dp"/>

    <fragment android:name="com.pongodev.dr.know.AnswerFragment"
              android:id="@+id/answer_fragment"
              android:layout_weight="2"
              android:layout_width="0dp"
              android:layout_height="match_parent" />

</LinearLayout>

这是包含按钮、编辑文本和列表视图的片段视图 question_view.xml,

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainScreen" 
    android:background="@drawable/app_background">

    <LinearLayout 
        android:id="@+id/lytSearchBar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:orientation="horizontal"
        android:layout_alignParentTop="true"
        android:gravity="center_vertical"
        android:padding="5dp">
        <EditText 
            android:id="@+id/edtSearch"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:textSize="14sp"
            android:hint="@string/type_question"
            android:textColor="@android:color/widget_edittext_dark"
            android:layout_marginRight="5dp"
            android:background="@drawable/edittext_style"
            android:padding="12dp"
            android:singleLine="true"/>
        <ImageButton 
            android:id="@+id/btnSearch"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@android:drawable/ic_menu_search"
            android:background="@drawable/button_style"
            android:padding="5dp"/>
    </LinearLayout>
    <ListView 
            android:id="@id/android:list"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:fadeScrollbars="true"
            android:fastScrollEnabled="true"
            android:divider="@android:color/transparent"
    android:dividerHeight="3dp"
    android:layout_below="@+id/lytSearchBar"/>
</RelativeLayout>

这是 QuestionFragment.java 代码,

public class QuestionFragment extends ListFragment {
    OnQuestionSelectedListener mCallback;
    ImageButton btnSearch;
    QuestionListAdapter qla;

    public interface OnQuestionSelectedListener{

        public void onQuestionSelected(int position);
    }

    @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
    Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View v = inflater.inflate(R.layout.question_view, container, false);
    //list = (ListView) v.findViewById(R.id.list);

    prgLoading = (ProgressBar) v.findViewById(R.id.prgLoading);
    txtAlert = (TextView) v.findViewById(R.id.txtAlert);
    btnSearch = (ImageButton) v.findViewById(R.id.btnSearch);

    btnSearch.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Toast.makeText(getActivity(), "button click", Toast.LENGTH_SHORT).show();
        }
    });

    return v;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);


    qla = new QuestionListAdapter(getActivity());
    new getQuestionList().execute();
    //setListAdapter(qla);


}

@Override
public void onStart() {
    super.onStart();

    // When in two-pane layout, set the listview to highlight the selected list item
    // (We do this during onStart because at the point the listview is available.)
    if (getFragmentManager().findFragmentById(R.id.answer_fragment) != null) {
        getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
    }
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

    // This makes sure that the container activity has implemented
    // the callback interface. If not, it throws an exception.
    try {
        mCallback = (OnQuestionSelectedListener) activity;
    } catch (ClassCastException e) {
        throw new ClassCastException(activity.toString()
                + " must implement OnHeadlineSelectedListener");
    }
}

@Override
public void onListItemClick(ListView l, View v, int position, long id) {
    // Notify the parent activity of selected item
    mCallback.onQuestionSelected(position);

    // Set the item as checked to be highlighted when in two-pane layout
    getListView().setItemChecked(position, true);
}

public class getQuestionList extends AsyncTask<Void, Void, Void>{

    getQuestionList(){
        if(!prgLoading.isShown()){
            prgLoading.setVisibility(0);
            txtAlert.setVisibility(8);
        }
    }

    @Override
     protected void onPreExecute() {
      // TODO Auto-generated method stub

    }

    @Override
    protected Void doInBackground(Void... arg0) {
        // TODO Auto-generated method stub
        //parseJSONData();

        return null;
    }

    @Override
    protected void onPostExecute(Void result) {
        // TODO Auto-generated method stub
        prgLoading.setVisibility(8);
        if((Question.length > 0)){
            setListAdapter(qla);
        }else{
            txtAlert.setText(R.string.no_connection_alert);
            txtAlert.setVisibility(0);
        }


    }
}
    }

当我尝试在 QuestionFragment 中获取按钮 ID 时,应用程序出错。如何解决此错误?

更新:我不知道为什么,突然我的按钮 id 问题解决了。但是,ProgressBar id 和 TextView id 还有其他问题。这是日志猫。

01-08 21:53:34.942: E/AndroidRuntime(23579): FATAL EXCEPTION: main
01-08 21:53:34.942: E/AndroidRuntime(23579): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.pongodev.dr.know/com.pongodev.dr.know.MainScreen}: java.lang.NullPointerException
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1970)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1995)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.ActivityThread.access$600(ActivityThread.java:128)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1161)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.os.Handler.dispatchMessage(Handler.java:99)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.os.Looper.loop(Looper.java:137)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.ActivityThread.main(ActivityThread.java:4514)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at java.lang.reflect.Method.invokeNative(Native Method)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at java.lang.reflect.Method.invoke(Method.java:511)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at dalvik.system.NativeStart.main(Native Method)
01-08 21:53:34.942: E/AndroidRuntime(23579): Caused by: java.lang.NullPointerException
01-08 21:53:34.942: E/AndroidRuntime(23579):    at com.pongodev.dr.know.QuestionFragment$getQuestionList.<init>(QuestionFragment.java:140)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at com.pongodev.dr.know.QuestionFragment.onCreate(QuestionFragment.java:97)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.support.v4.app.Fragment.performCreate(Fragment.java:1437)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:877)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:551)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1137)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.Activity.performStart(Activity.java:4475)
01-08 21:53:34.942: E/AndroidRuntime(23579):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1943)
01-08 21:53:34.942: E/AndroidRuntime(23579):    ... 11 more
4

1 回答 1

0

您需要确保在针对它们运行代码之前初始化所有视图。正如 njzk 所说,getQuestionList 正在抛出,因此 prgloading 或 txtAlert 为空,因此将访问 getQuestionList 的代码从 OnCreate 移动到覆盖的 onActivityResult。(片段生命周期 - https://developer.android.com/reference/android/app/Fragment#Lifecycle

于 2013-01-08T15:31:08.300 回答