【发布时间】: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