我目前正在學習android片段編程幾個小時,但是我的片段沒有顯示出來。 我沒有收到任何錯誤。調試顯示片段類的onCreateView()永遠不會被調用。Android片段沒有顯示
這是我的主要佈局activity_main.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello_world" />
<LinearLayout
android:id="@+id/FragCont"
android:layout_width="100dp"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:layout_toRightOf="@+id/textView1"
android:orientation="vertical" >
</LinearLayout>
這是我的主要活動:
package k.myApp;
import com.actionbarsherlock.app.SherlockActivity;
import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import android.os.Bundle;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
SherlockFragment Fragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CentralData.getDataSource().open();
CentralData.getDataSource().close();
FragmentShowNotes fsn = new FragmentShowNotes();
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.FragCont, fsn);
transaction.commit();
}
}
這是我的片段類,FragmentShowNotes.java: K組.myApp;
import org.holoeverywhere.LayoutInflater;
import android.app.Fragment;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import com.actionbarsherlock.app.SherlockFragment;
public class FragmentShowNotes extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
return inflater.inflate(R.layout.frag_show_notes, container, false);
}
}
最後但並非最不重要的我的片段佈局,frag_show_notes.xml:
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Dies ist das ShowNotesFragment" />
</LinearLayout>
有誰知道是怎麼回事?謝謝!
任何例外......? – Triode
不,沒有錯誤或例外 – user1956123