我是小白,照着写个Fragment的实例来看看,结果运行出了问题,老是显示什么【HELLO WORLD,FRAGMENTEX!】,运行正常,也没报错。而这不是我想实现的画面。我也没写什么HELLOWORLD啊,求哪位高手给看看,谢谢。
我用的是ECLIPSE做的。
package net.npaka.fragmentex;import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;public class FragmentEx extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.main);
} public static class TitlesFragment extends ListFragment {
private int pos = -1; @Override
public void onActivityCreated(Bundle bundle) {
// TODO Auto-generated method stub
super.onActivityCreated(bundle);
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_activated_1,
new String[]{ "Page0", "Page1", "Page2" })); getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetails(0);
} @Override
public void onListItemClick(ListView l, View v, int position, long id) {
// TODO Auto-generated method stub
showDetails(position);
} private void showDetails(int index) {
getListView().setItemChecked(index, true);
if (pos == index)
return; DetailsFragment fragment = DetailsFragment.newInstance(index);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.details, fragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
pos = index;
} public static class DetailsFragment extends Fragment { public static DetailsFragment newInstance(int index) {
DetailsFragment fragment = new DetailsFragment();
Bundle bundle = new Bundle();
bundle.putInt("index", index);
fragment.setArguments(bundle);
return fragment;
} } @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle bundle) {
// TODO Auto-generated method stub if (container == null)
return null;
TextView textView = new TextView(getActivity());
textView.setText("ページ" + getArguments().getInt("index", 0) + "Page");
textView.setTextSize(24);
return textView; } }}
main.xml<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="net.npaka.fragmentex.FragmentEx$TitlesFragment"
android:id="@+id/titles" android:layout_weight="1"
android:layout_width="0px" android:layout_height="match_parent" />
<FrameLayout android:id="@+id/details"
android:layout_weight="3" android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>

解决方案 »

  1.   

    你在manifest.xml文件里注册你的activity了吗
    把原来带有的
    <intent-filter>
    <action android:name="android.intent.action.BigKnowledgeQuerySystem" />
    <category android:name="android.intent.category.DEFAULT" />
    </intent-filter>
    剪切到你的activity下
      

  2.   

    注册了的<?xml version="1.0" encoding="utf-8"?>
    <manifest xmlns:android="http://schemas.android.com/apk/res/android"
          package="net.npaka.fragmentex"
          android:versionCode="1"
          android:versionName="1.0">
        <uses-sdk android:minSdkVersion="11" />    <application android:icon="@drawable/icon" android:label="@string/app_name">
            <activity android:name=".FragmentEx"
                      android:label="@string/app_name">
                <intent-filter>
                    <action android:name="android.intent.action.MAIN" />
                    <category android:name="android.intent.category.LAUNCHER" />
                </intent-filter>
            </activity>
        </application>
    </manifest>
    楼上发的那段有什么区别呢??