【问题标题】:How to get rid of this "Inconvertible" error?如何摆脱这个“不可转换”的错误?
【发布时间】:2015-02-17 22:43:11
【问题描述】:

我正在制作一个简单的 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);

    }

其他一切正常。只有上面的行显示了这个错误:

我的日志文件输出如下:

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

    }

}

【问题讨论】:

  • 为什么要将 Fragment 投射到 BottomSectionJava 对象上? BottomSectionJava 是否扩展了 Fragment?
  • Konrad Krakowiak ,是的,它扩展了片段。如果你这次看到我的帖子。我已经在我的代码中添加了那部分。
  • 好的,我更新了我的帖子
  • 你试过我的解决方案了吗?
  • Konrad Krakowiak,请再给我一点时间!我会回到你身边

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


【解决方案1】:

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

我认为您在下面的导入行中有:

import android.app.Fragment;

你必须拥有

import android.support.v4.app.Fragment;

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

【讨论】:

  • 非常感谢。其实我有两个片段。这就是为什么我必须对两个课程都应用相同的建议。现在我摆脱了主要问题。我的应用程序正在运行。唯一的问题是当我单击按钮添加文本时没有任何反应。它不会在图片的 textView 区域中添加文本。不过我不会打断你那么多。如果你有时间可以看看我的问题为什么按钮点击不起作用?否则没关系。 XX
  • 很高兴能为您提供帮助。
  • 在哪里调用 setTextOnThePic(Sting, String) 方法?没有任何地方可以初始化监听器。
猜你喜欢
  • 2015-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-07-30
  • 1970-01-01
  • 2017-04-04
相关资源
最近更新 更多