私はフラグメントについて新しいです。フラグメントを実装するアプリがあり、レイアウトが異なります。アプリをスマートフォンとタブレットで縦向きに実行すると、1つのレイアウト(1つのフラグメント)のみが表示され、横向きのタブレットでは、2つのペインレイアウト(2つのフラグメント)が表示されます。
フラグメントレイアウトでビュー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>
これは、ボタン、edittext、およびlistviewを含むフラグメントビュー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の問題が解決しました。ただし、ProgressBaridとTextViewidには他の問題があります。これがlogcatです。
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