如何摆脱这种“不可转换”的错误?

时间:2015-02-17 22:43:12

标签: android android-layout android-activity android-fragments android-fragmentactivity

我正在创建一个简单的memegenerator应用程序,用户应在其中输入底部和顶部字段的文本,并且该文本将添加到图片的底部和顶部。由于我想保持应用程序简单,我只使用了Meme的一个固定图像。 我用了两个碎片。一个用于Edittext区域,另一个用于viwtext区域(etxt将附加在图片上)。为了将它们粘合在一起,我创建了一个接口,它也可以在我的mainActivity的java文件中调用。现在问题在于我的主java类中的以下代码行。这里的方法memeGen是这个应用程序的主要大脑。在它里面,我试图通过它的id找到一个片段。但它似乎没有找到那种资源。

   public void memeGen(String top, String bottom) {
        BottomSectionJava bjs=(BottomSectionJava)getSupportFragmentManager().findFragmentById(R.id.fragment2);

    }

其他一切都很好。只有上面的行显示此错误: Error

我的日志文件输出:

C:\Users\Riyana\AndroidStudioProjects\Fragment\app\src\main\java\com\mycompany\fragment\FragmentActivity.java
Error:(20, 94) error: inconvertible types
required: BottomSectionJava
found:    Fragment
Error:Execution failed for task ':app:compileDebugJava'.
> Compilation failed; see the compiler error output for details.

这是出现错误的类的完整代码:

    public class FragmentActivity extends ActionBarActivity implements TopSectionjava.TopSectionListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_fragment);
    }

    @Override
    public void memeGen(String top, String bottom) {
        BottomSectionJava bjs=(BottomSectionJava)getSupportFragmentManager().findFragmentById(R.id.fragment2);

    }


    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_, menu);
        return true;
    }
}

BottomSectionJava:

    public class BottomSectionJava extends Fragment{
    private static TextView memetext_top;
    private static TextView memetext_bottom;
 //
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        //return super.onCreateView(inflater, container, savedInstanceState);
        View view=inflater.inflate(R.layout.bottom_section,container,false);
        memetext_top=(TextView)view.findViewById(R.id.memetext_top);
        memetext_bottom=(TextView)view.findViewById(R.id.memetext_bottom);
        return view;
    }

    public void setTextOnThePic(String top,String bottom){
        memetext_top.setText(top);
        memetext_bottom.setText(bottom);

    }

}

1 个答案:

答案 0 :(得分:2)

BottomSectionJava中的扩展名从android.app.Fragment更改为android.support.v4.app.Fragment

我认为你的进口线下面有:

import android.app.Fragment;

你必须

import android.support.v4.app.Fragment;

现在,您的片段从Fragment包扩展了对象android.app,您必须从Fragment包扩展android.support.v4.app

相关问题