54

我收到以下错误:“方法 getSupportFragmentManager() 未定义类型 new View.OnClickListener(){}”在我的片段文件中,如下所示。

我有通过 ABS 引用的兼容性库和适当的导入。我重新安装了带有兼容性库的 ABS 库,清理了项目,重新启动了 Eclipse,但没有任何效果。

本质上,我试图让片段通过对话框片段显示日期选择器。选择日期后,必须将其返回到片段,以便可以使用它来计算基于该日期的信息。

这是我的片段的代码:

package com.owentech.abstabsviewpager;

import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.Toast;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import com.actionbarsherlock.app.SherlockFragment;

public class ObstetricsFragment1 extends SherlockFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    //Fragment Layout
    View view = inflater.inflate(R.layout.obstetricsfragment1, container, false);

    Button mPickLMPDate = (Button) view.findViewById(R.id.pickLMPDate);

    mPickLMPDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            LMPDatePickerDialogFragment d = LMPDatePickerDialogFragment.newInstance();
            d.show(getSupportFragmentManager(), "dialog");
        }

    });

    return view;
}

public Dialog onCreateDialog(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    return null;
}

}

这是对话框片段的代码:

package com.owentech.abstabsviewpager;

import android.app.Dialog;
import android.os.Bundle;
import android.widget.DatePicker;
import android.app.DatePickerDialog;

public class LMPDatePickerDialogFragment extends ObstetricsFragment1 implements DatePickerDialog.OnDateSetListener {

static LMPDatePickerDialogFragment newInstance() {
    LMPDatePickerDialogFragment d = new LMPDatePickerDialogFragment();
    return d;
}

private int mLMPYear;
private int mLMPMonth;
private int mLMPDay;

public Dialog onCreateDialog(Bundle savedInstanceState) {
    return new DatePickerDialog(getActivity(), this, mLMPYear, mLMPMonth, mLMPDay);
}

public void onDateSet(DatePicker view, int year, int month, int day) {
    mLMPYear = year;
    mLMPMonth = month;
    mLMPDay = day;
}
}

最后,这是我的活动的代码:

package com.owentech.abstabsviewpager;

import java.util.ArrayList;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuInflater;
import android.widget.TextView;
import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.actionbarsherlock.app.ActionBar.Tab;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;


public class Obstetrics extends SherlockFragmentActivity
{
ViewPager mViewPager;
TabsAdapter mTabsAdapter;
TextView tabCenter;
TextView tabText;

// START Action Bar Menu Items  
@Override
public boolean onCreateOptionsMenu(Menu menu) {
   MenuInflater inflater = getSupportMenuInflater();
   inflater.inflate(R.menu.main_menu, menu);
   return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) {
    switch (item.getItemId()){
    case R.id.menuLog:
        ChangeLog cl = new ChangeLog(this);
        cl.getFullLogDialog().show();
        return true;
    case R.id.menuEmail:
        Intent emailIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("mailto:support@medicaldoctorapps.com"));
        startActivity(emailIntent);
        return true;
    case R.id.menuRate:
        Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=appinventor.ai_shawn_m_gee.MedicalDoctor"));
        startActivity(browserIntent);
        return true;
    case android.R.id.home:
        // App icon in action bar clicked; go home
        Intent intent = new Intent(this, Home.class);
        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
        startActivity(intent);
        return true;
    case R.id.menuExit:
        this.finish();
        return true;
    default:
        return super.onOptionsItemSelected(item);
    }
}      
//END Action Bar Menu Items

// START Tabs View Pager (Add tabs by adding mTabsAdapter.addTab)   
@Override
public void onCreate(Bundle savedInstanceState)
{

    // Information you want returned to your application, via onCreate(), if the activity is destroyed and restarted due to some implicit reason        
    super.onCreate(savedInstanceState);

    mViewPager = new ViewPager(this);
    mViewPager.setId(R.id.pager);

    setContentView(mViewPager);
    ActionBar bar = getSupportActionBar();
    bar.setDisplayHomeAsUpEnabled(true);
    bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
    mTabsAdapter = new TabsAdapter(this, mViewPager);

    mTabsAdapter.addTab(
            bar.newTab().setText("Wheel"),
            ObstetricsFragment1.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Physical"),
            HistoryPhysicalFragment2.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("ROS"),
            HistoryPhysicalFragment3.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("CAGE"),
            HistoryPhysicalFragment4.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("SIGECAPS"),
            HistoryPhysicalFragment5.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Glasgow"),
            HistoryPhysicalFragment6.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Neuro"),
            HistoryPhysicalFragment7.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Dermat"),
            HistoryPhysicalFragment8.class, null);
    mTabsAdapter.addTab(
            bar.newTab().setText("Minicog"),
            HistoryPhysicalFragment9.class, null);
}

public static class TabsAdapter extends FragmentPagerAdapter implements
        ActionBar.TabListener, ViewPager.OnPageChangeListener
{
    private final Context mContext;
    private final ActionBar mActionBar;
    private final ViewPager mViewPager;
    private final ArrayList<TabInfo> mTabs = new ArrayList<TabInfo>();

    static final class TabInfo
    {
        private final Class<?> clss;
        private final Bundle args;

        TabInfo(Class<?> _class, Bundle _args)
        {
            clss = _class;
            args = _args;
        }
    }

    public TabsAdapter(SherlockFragmentActivity activity, ViewPager pager)
    {
        super(activity.getSupportFragmentManager());
        mContext = activity;
        mActionBar = activity.getSupportActionBar();
        mViewPager = pager;
        mViewPager.setAdapter(this);
        mViewPager.setOnPageChangeListener(this);
    }

    public void addTab(ActionBar.Tab tab, Class<?> clss, Bundle args)
    {
        TabInfo info = new TabInfo(clss, args);
        tab.setTag(info);
        tab.setTabListener(this);
        mTabs.add(info);
        mActionBar.addTab(tab);
        notifyDataSetChanged();
    }

    @Override
    public int getCount()
    {
        return mTabs.size();
    }

    @Override
    public Fragment getItem(int position)
    {
        TabInfo info = mTabs.get(position);
        return Fragment.instantiate(mContext, info.clss.getName(),
                info.args);
    }

    public void onPageScrolled(int position, float positionOffset,
            int positionOffsetPixels)
    {
    }

    public void onPageSelected(int position)
    {
        mActionBar.setSelectedNavigationItem(position);
    }

    public void onPageScrollStateChanged(int state)
    {
    }

    public void onTabSelected(Tab tab, FragmentTransaction ft)
    {
        Object tag = tab.getTag();
        for (int i = 0; i < mTabs.size(); i++)
        {
            if (mTabs.get(i) == tag)
            {
                mViewPager.setCurrentItem(i);
            }
        }
    }

    public void onTabUnselected(Tab tab, FragmentTransaction ft)
    {
    }

    public void onTabReselected(Tab tab, FragmentTransaction ft)
    {
    }
}
// END Tabs View Pager

}
4

8 回答 8

84

您将通过使调用对话框的活动扩展 FragmentActivity 来摆脱这个问题

public class ObstetricsFragment1 extends FragmentActivity{

这是支持库所固有的,如DIALOG SPECSSUPPORT LIBRARY SPECS中所示

于 2013-01-18T17:01:22.717 回答
9

尝试将您的代码更改为:

public class ObstetricsFragment1 extends SherlockFragment{

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState)
{
    //Fragment Layout
    View view = inflater.inflate(R.layout.obstetricsfragment1, container, false);

    Button mPickLMPDate = (Button) view.findViewById(R.id.pickLMPDate);

    mPickLMPDate.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {
            LMPDatePickerDialogFragment d = LMPDatePickerDialogFragment.newInstance();
            FragmentManager fm = ObstetricsFragment1.this.getSherlockActivity().getSupportFragmentManager();
            d.show(fm, "dialog");
        }

    });

    return view;
}
于 2012-11-06T06:47:15.380 回答
4

我对 Marco 的回答做了一个比较,看看他实际上建议改变什么:只是

d.show(getSupportFragmentManager(), "dialog");

FragmentManager fm = 
    ObstetricsFragment1.this.getSherlockActivity().getSupportFragmentManager();
d.show(fm, "dialog");
于 2013-05-04T06:26:01.803 回答
3

如果有人正在使用SherlockActivity而不是SherlockFragment仅仅将活动扩展到SherlockFragmentActivity.

于 2014-04-10T13:07:41.550 回答
2

我在加载课程项目(https://developer.android.com/training/multiple-threads)时遇到了类似的问题。

为了解决这个问题,我不得不添加一个外部 jar(sdk/extras/android/support/v4/android-support-v4.jar)。

希望这可以帮助。

于 2013-10-11T18:19:11.620 回答
1

您需要从 SherlockDialogFragment 扩展 ObstetricsFragment1。

于 2013-05-15T22:05:04.940 回答
0

进入FragmentManager片段调用getChildFragmentManager()。看到这个。

改变这个

mPickLMPDate.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        LMPDatePickerDialogFragment d = LMPDatePickerDialogFragment.newInstance();
        d.show(getSupportFragmentManager(), "dialog");
    }

});

mPickLMPDate.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
        LMPDatePickerDialogFragment d = LMPDatePickerDialogFragment.newInstance();
        d.show(getChildFragmentManager(), "dialog");
    }

});
于 2017-03-03T08:13:54.807 回答
-1

您的项目中存在问题,因为您正在使用已弃用的 actionBarSherlock ...您应该查看Android Support并使用它,因为 supportFragmentManager 可用。它很容易使用,将它添加到你的 build.gradle

compile "com.android.support:support-core-utils:25.2.0"

在使用来自支持的 FragmentActivity 或 Fragment 扩展您的活动和片段之后。也许你将不得不因为使用夏洛克酒吧而做出一些改变

关于您的问题的另一件事是您从片段中调用了 supportFragmentManager ...您应该调用

getChildFragmentManager() // because it will be a nested fragment

希望它有帮助

于 2017-03-07T10:44:01.557 回答