我在一側有一個ListView,另一側有一個分段。我正在嘗試製作一個簡單的應用程序來了解碎片的工作原理。 這裏是我的代碼:爲什麼我的分段不工作
主要活動:
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
//
setContentView(R.layout.activity_main);
String[] adobe_products = getResources().getStringArray(R.array.adobe_products);
//List view
final ListView list = (ListView)findViewById(R.id.questionsList);
ArrayAdapter<String> adapter;
adapter = new ArrayAdapter<String>(this, R.layout.list_item, adobe_products);
//final TextView text = (TextView)findViewById(R.id.textView1);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id){
//String selectedFromList =(String) (list.getItemAtPosition(position));
//text.setText(selectedFromList);
}
});
list.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@SuppressLint("ValidFragment")
public class MyFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.activity_fragment, container, false);
}
}
}
主要活動XML佈局
<LinearLayout 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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity" >
<ListView
android:id="@+id/questionsList"
android:layout_width="144dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_weight="0.07" >
</ListView>
<fragment android:name="com.example.Layout.MyFragment"
android:id="@+id/article_fragment"
android:layout_weight="2"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
更新:感謝您的幫助,我注意到,該片段是子類,所以我將它包含在我的活動中。但仍然得到相同的結果。它停止,甚至不開放。無論如何,我更新了整個代碼logCat。
的logcat:
02-22 00:06:50.944: E/AndroidRuntime(2886): FATAL EXCEPTION: main
02-22 00:06:50.944: E/AndroidRuntime(2886): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.layout/com.example.layout.MainActivity}: android.view.InflateException: Binary XML file line #21: Error inflating class fragment
02-22 00:06:50.944: E/AndroidRuntime(2886): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
02-22 00:06:50.944: E/AndroidRuntime(2886): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
02-22 00:06:50.944: E/AndroidRuntime(2886): at android.app.ActivityThread.access$600(ActivityThread.java:141)
任何想法,使其正常工作?
+1適當的格式,即使你是新用戶:) – Kedarnath
如果你的類'Fragment'旨在成爲片段,它應該擴展'android.app.Fragment',你應該重寫'onCreateView'。此外,請參閱此[android開發人員鏈接](http://developer.android.com/training/implementing-navigation/descendant.html#master-detail) – Vino