首先,我是新的android和英語:)對不起。我遇到了Fragment經理的問題。我讀了一些在主要活動中使用片段的代碼。但是它不能在模擬器中運行,並且您可以在下面看到代碼。我認爲關於「導入android.support.v4.app.FragmentManager;」的問題線。我不知道也許我必須像這樣使用它「import android.app.FragmenManager」 我希望你能理解我的句子並幫助我。 謝謝。android片段無法在模擬器中打開
package com.example.ilk.fragmenttransactions;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends FragmentActivity {
FragmentManager fm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void addA(View v){
FragmentA f1 = new FragmentA();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, f1, "A");
transaction.commit();
}
public void removeA(View v){
FragmentA f1 = (FragmentA)fm.findFragmentByTag("A");
FragmentTransaction transaction = fm.beginTransaction();
if(f1 != null){
transaction.remove(f1);
transaction.commit();
}
else{
Toast.makeText(this, "There is no A! you cannot delete it", Toast.LENGTH_LONG).show();
}
}
public void addB(View v){
FragmentB f2 = new FragmentB();
FragmentTransaction transaction = fm.beginTransaction();
transaction.add(R.id.fragment_container, f2, "B");
transaction.commit();
}
public void removeB(View v){
FragmentB f2 = (FragmentB)fm.findFragmentByTag("B");
FragmentTransaction transaction = fm.beginTransaction();
if(f2 != null){
transaction.remove(f2);
transaction.commit();
}
else{
Toast.makeText(this, " There is no B! you cannot delete it ",Toast.LENGTH_SHORT).show();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
查看logcat的輸出 –
顯示在logcat中顯示錯誤消息必須在計算器哈哈的要求! – Jorgesys