我創建了兩個活動並使用了意圖從第一個活動切換到第二個活動。我還在第二項活動中使用了片段。當我嘗試運行該應用程序時,該應用程序即將啓動,並且不幸地停止。當我從我的應用程序中刪除碎片時,應用程序將運行。我該怎麼做才能擺脫這個錯誤?這是我的代碼...將碎片附加到活動
First Activity code
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i=new Intent(MainActivity.this,FirstActivity.class);
startActivity(i);
}
});
}
@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;
}
}
次活動代碼
public class FirstActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
public void selectFrag(View view)
{
Fragment fr;
if(view == findViewById(R.id.button2))
{
fr = new FragmentOne();
}
else if(view==findViewById(R.id.button3)){
fr = new FragmentOne();
}
else
{
fr=new FragmentOne();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_first, fr);
fragmentTransaction.commit();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.first, menu);
return true;
}
}
片段代碼
public class FragmentOne extends Fragment {
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.fragment_one, container, false);
}
}
次活動的清單中,我試圖用片段
<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=".FirstActivity" >
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button2"
android:layout_alignParentBottom="true"
android:layout_alignRight="@+id/button2"
android:layout_marginBottom="66dp"
android:text="@string/IDE" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button3"
android:layout_alignLeft="@+id/button1"
android:layout_marginBottom="42dp"
android:layout_toLeftOf="@+id/fragment_first"
android:text="@string/Applications" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/fragment_first"
android:layout_alignBottom="@+id/fragment_first"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp"
android:text="@string/Operating_Systems" />
<fragment
android:id="@+id/fragment_first"
android:name="com.example.test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/button3"
android:layout_alignParentTop="true"
android:layout_marginTop="56dp"
android:layout_toRightOf="@+id/button1" />
我如何獲得堆棧跟蹤?我沒有附上任何try-catch語句! – user3494875
你的意思是說「佈局」,而不是顯示? – Eenvincible
用if(view.getId()== R.id.button2)替換所有if(view == findViewById(R.id.button2)) –