Android - 从活动中调用片段

时间:2015-05-09 04:54:57

标签: android

[1]我需要从溢出菜单中点击一个片段。当我在我的Bankingactivity.java上时,当我调用我的MoneyTransferFragment.java时,它很容易被访问,但是当我试图从任何其他活动中调用MoneyTransferFragment.java时它会崩溃。我没有MoneyTransferFragment.java的活动,也因为一些数据解析问题而无法生成。

在BankingActivity.java中,我调用了Fragment manager来替换片段。给定的代码片段来自BankingActivity.java。当我试图粘贴在任何其他活动中的相同代码时,它正在崩溃。我粘贴了Bnkingactivity.java的代码片段

case R.id.i3:
fragmentTransaction = fragmentManager.beginTransaction();
        moneyTransferFragment = new MoneyTransferFragment();
        fragmentTransaction.replace(R.id.fragmentholder,moneyTransferFragment);
        fragmentTransaction.addToBackStack("money");
        fragmentTransaction.commit();

        break;

private void defaultSummaryScreen()
    {
        fragmentTransaction = fragmentManager.beginTransaction();
        accountSummaryFragment = new AccountSummaryFragment();
        fragmentTransaction.replace(R.id.fragmentholder,accountSummaryFragment);
        // fragmentTransaction.addToBackStack("summary");
        fragmentTransaction.commit();
    }

// statementactivity.java

public class StatementActivity extends ActionBarActivity implements FragmentConnector{

private MiniStatement miniStatement;
private DetailedStatement detailedStatement;
private FragmentTransaction fragmentTransaction;
private MoneyTransferFragment moneyTransferFragment;
private FragmentManager fragmentManager;
private AccountSummaryFragment accountSummaryFragment;

//PagerTitleStrip pagerTitle;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_statements);
    miniStatement = new MiniStatement();
    detailedStatement = new DetailedStatement();

    ViewPager pager = (ViewPager) findViewById(R.id.pager);
    MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
    pager.setAdapter(myPagerAdapter);

    ActionBar actionBar=getSupportActionBar();
    actionBar.setTitle(Html.fromHtml("<font color='#ffffff'>Statements </font>"));
    actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#787777")));
    actionBar.setIcon(R.drawable.new_ic);
    actionBar.setDisplayShowHomeEnabled(true);
    actionBar.setDisplayHomeAsUpEnabled(true);


}

/*public void handleClick(View v) {
    // TODO Auto-generated method stub
    switch (v.getId()) {
    case R.id.pager_title_strip:
        pagerTitle.setVisibility(0);
        ViewPager pager = (ViewPager) findViewById(R.id.pager);
        MyPagerAdapter myPagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        pager.setAdapter(myPagerAdapter);
        break;

    default:
        break;
    }

}*/

public class MyPagerAdapter extends FragmentPagerAdapter {
    public MyPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {

        if (position == 0){
            return miniStatement;
        }else if (position == 1){
            return detailedStatement;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }

    @Override
    public CharSequence getPageTitle(int position) {

        if (position == 0) {
            return "Mini Statement";
        }
        if (position == 1) {
            return "Detailed Statement";
        }

        return null;
    }
}





@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;

}
@Override
public boolean onOptionsItemSelected(MenuItem item) {


    switch (item.getItemId()) {
    case R.id.i1:

        Intent intent = new Intent(StatementActivity.this,BankingActivity.class);
        startActivity(intent);
        break;

    case R.id.i2:
        Intent intent1 = new Intent(StatementActivity.this, StatementActivity.class);
        startActivity(intent1);
        break;

    case R.id.i3:
        ActionBar actionBar=getSupportActionBar();
        getActionBar().setHomeButtonEnabled(true);
        getActionBar().setDisplayHomeAsUpEnabled(true);

        actionBar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#FFCCFF")));
        actionBar.setTitle(Html.fromHtml("<font color='#000000'>Money Transfer </font>"));

        fragmentTransaction = fragmentManager.beginTransaction();
        moneyTransferFragment = new MoneyTransferFragment();
        fragmentTransaction.replace(R.id.pager,moneyTransferFragment);
        fragmentTransaction.addToBackStack("money");
        fragmentTransaction.commit();

        break;

    case R.id.i4:
        Intent intent3 = new Intent(StatementActivity.this, ChequeRequestActivity.class);
        startActivity(intent3);
        break;  
    case R.id.home:
        NavUtils.navigateUpFromSameTask(this);

        break;
    case R.id.i5:
        Intent inten4 = new Intent(StatementActivity.this, MainActivity.class);
        startActivity(inten4);
        break;  
    }
    return super.onOptionsItemSelected(item);

}

@Override
public void CallBackFromFragment(String fragmentName) {
    // TODO Auto-generated method stub

}

}

LogCat ..Cant上传图片

05-09 02:20:36.590:W / dalvikvm(2212):threadid = 1:线程退出未捕获的异常(group = 0xb2a4cba8) 05-09 02:20:36.600:E / AndroidRuntime(2212):致命异常:主要 05-09 02:20:36.600:E / AndroidRuntime(2212):进程:com.example.mybankapp,PID:2212 05-09 02:20:36.600:E / AndroidRuntime(2212):java.lang.IllegalArgumentException:找不到ID为0x7f05003d(com.example.mybankapp:id / fragmentholder)的片段,用于片段MoneyTransferFragment {b2e6c8c0#2 id = 0x7f05003d} 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:919) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1467) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.support.v4.app.FragmentManagerImpl $ 1.run(FragmentManager.java:440) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.os.Handler.handleCallback(Handler.java:733) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.os.Handler.dispatchMessage(Handler.java:95) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.os.Looper.loop(Looper.java:136) 05-09 02:20:36.600:E / AndroidRuntime(2212):在android.app.ActivityThread.main(ActivityThread.java:5017) 05-09 02:20:36.600:E / AndroidRuntime(2212):at java.lang.reflect.Method.invokeNative(Native Method) 05-09 02:20:36.600:E / AndroidRuntime(2212):at java.lang.reflect.Method.invoke(Method.java:515) 05-09 02:20:36.600:E / AndroidRuntime(2212):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:779) 05-09 02:20:36.600:E / AndroidRuntime(2212):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 05-09 02:20:36.600:E / AndroidRuntime(2212):at dalvik.system.NativeStart.main(Native Method)

0 个答案:

没有答案