我对编码非常陌生,尤其是Java,所以如果我错过了任何从根本上显而易见的东西,我很抱歉。用外行人的话回答是有帮助的。
我正在尝试创建一个单词搜索应用程序。一个非常简单的游戏版本,没什么稀奇的。我已经做了一个网格视图,以容纳图像的字母和工作选择的地方,这是顶部的片段。下面的片段包含正在搜索的字母。
这是包含网格视图的顶部片段的代码。这在一个独立的应用程序中工作,每个字母都是可选择的,多个选择是可以做出的。但是,当试图将其放入片段时,(com.example.sebastian.multipleselectiongrid.TopWordSearchFragment“TopWordsearchFragment抛出" ImageView (android.content.Context) in ImageView”错误。
这是我的第一个问题
package com.example.sebastian.multipleselectiongrid;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.ActionMode;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Checkable;
import android.widget.FrameLayout;
import android.widget.GridView;
import android.widget.ImageView;
public class TopWordsearchFragment extends Fragment {
GridView mGrid;
TopSectionListener activityCommander;
public interface TopSectionListener{
public void searchWordsearchWords();
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try{
activityCommander = (TopSectionListener)activity;
}catch(ClassCastException e){
throw new ClassCastException(activity.toString());
}
}
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.grid_1, container, false);
mGrid = (GridView) view.findViewById(R.id.myGrid);
mGrid.setAdapter(new ImageAdapter());
mGrid.setChoiceMode(GridView.CHOICE_MODE_MULTIPLE_MODAL);
mGrid.setMultiChoiceModeListener(new MultiChoiceModeListener());
return view;
}
public class ImageAdapter extends BaseAdapter {
public View getView(int position, View convertView, ViewGroup parent) {
CheckableLayout l;
ImageView i;
if (convertView == null) {
i = new ImageView(TopWordsearchFragment.this);
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setLayoutParams(new GridView.LayoutParams(85, 85));
l = new CheckableLayout(TopWordsearchFragment.this);
l.setLayoutParams(new GridView.LayoutParams(
GridView.LayoutParams.WRAP_CONTENT,
GridView.LayoutParams.WRAP_CONTENT));
l.addView(i);
} else {
l = (CheckableLayout) convertView;
i = (ImageView) l.getChildAt(0);
}
i.setImageResource(mThumbIds[position]);
return l;
}
public final int getCount() {
return mThumbIds.length;
}
public final Object getItem(int position) {
return null;
}
public final long getItemId(int position) {
return 0;
}
private Integer[] mThumbIds = {
R.drawable.letter_j, R.drawable.letter_s, R.drawable.letter_o, R.drawable.letter_l, R.drawable.letter_u, R.drawable.letter_t, R.drawable.letter_i, R.drawable.letter_s,
R.drawable.letter_s, R.drawable.letter_u, R.drawable.letter_n, R.drawable.letter_a, R.drawable.letter_r, R.drawable.letter_u, R.drawable.letter_u, R.drawable.letter_a,
R.drawable.letter_n, R.drawable.letter_e, R.drawable.letter_p, R.drawable.letter_t, R.drawable.letter_u, R.drawable.letter_n, R.drawable.letter_e, R.drawable.letter_t,
R.drawable.letter_s, R.drawable.letter_o, R.drawable.letter_n, R.drawable.letter_i, R.drawable.letter_e, R.drawable.letter_i, R.drawable.letter_s, R.drawable.letter_u,
R.drawable.letter_r, R.drawable.letter_c, R.drawable.letter_e, R.drawable.letter_v, R.drawable.letter_t, R.drawable.letter_r, R.drawable.letter_e, R.drawable.letter_r,
R.drawable.letter_a, R.drawable.letter_h, R.drawable.letter_t, R.drawable.letter_r, R.drawable.letter_a, R.drawable.letter_e, R.drawable.letter_s, R.drawable.letter_n,
R.drawable.letter_m, R.drawable.letter_m, R.drawable.letter_e, R.drawable.letter_r, R.drawable.letter_c, R.drawable.letter_u, R.drawable.letter_r, R.drawable.letter_y
};
}
public class CheckableLayout extends FrameLayout implements Checkable {
private boolean mChecked;
public CheckableLayout(Context context) {
super(context);
}
@SuppressWarnings("deprecation")
public void setChecked(boolean checked) {
mChecked = checked;
setBackgroundDrawable(checked ? getResources().getDrawable(
R.drawable.blue) : null);
}
public boolean isChecked() {
return mChecked;
}
public void toggle() {
setChecked(!mChecked);
}
}
public class MultiChoiceModeListener implements
GridView.MultiChoiceModeListener {
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
return true;
}
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
return true;
}
public void onDestroyActionMode(ActionMode mode) {
}
public void onItemCheckedStateChanged(ActionMode mode, int position,
long id, boolean checked) {
}
}
}我的第二个问题是碎片不会出现。BottomSectionFragment不显示任何编码错误,并在运行时抛出一个错误(我将在下面显示)。这是在注释掉上面的TopSectionFragment并从activity_main.xml文件中删除该片段之后。
这是MainActivity.Java文件
package com.example.sebastian.multipleselectiongrid;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}下面是BottomSectionFragment.Java文件
package com.example.sebastian.multipleselectiongrid;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import android.app.Activity;
public class BottomSectionFragment extends Fragment {
TextView word1;
TextView word2;
TextView word3;
TextView word4;
TextView word5;
TextView word6;
TextView word7;
TextView word8;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_section_fragment, container, false);
word1 = (TextView) view.findViewById(R.id.word1);
word2 = (TextView) view.findViewById(R.id.word2);
word3 = (TextView) view.findViewById(R.id.word3);
word4 = (TextView) view.findViewById(R.id.word4);
word5 = (TextView) view.findViewById(R.id.word5);
word6 = (TextView) view.findViewById(R.id.word6);
word7 = (TextView) view.findViewById(R.id.word7);
word8 = (TextView) view.findViewById(R.id.word8);
return view;
}
}当我一起运行这段代码时,我会收到错误FATAL EXCEPTION: main java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.sebastian.multipleselectiongrid/com.example.sebastian.multipleselectiongrid.MainActivity}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment。
并抛出三个原因:Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment
Caused by: android.app.Fragment$InstantiationException: Trying to instantiate a class com.example.sebastian.multipleselectiongrid.BottomSectionFragment that is not a Fragment
Caused by: java.lang.ClassCastException并指出每三种情况下的线setContentView(R.layout.activity_main);。
就像我说的,我对编码很陌生,答案可能是显而易见的,或者我用错误的方式来解决这个问题。因此,任何有帮助的信息或建议将不胜感激。
编辑:谢谢你的答案。我设法修复了问题2。在我的MainActivity类中,如果我扩展了ActionBarActivity,而不仅仅是活动,它就能正常工作。我检查了xml文件,它们是按照@Kumiho建议的方式编码的。
至于问题1,我无法解决在片段中使用这个特定代码的问题,所以我将它留在了MainActivity (带有适当的编辑)中。正如我所希望的那样,两个人正在一起工作。现在要实现功能。
再次,非常感谢。
发布于 2015-04-23 21:01:41
container_for_your_fragment需要在viewgroup_activity_layout.xml中声明,并且需要是一个视图组。
另一种使片段可见的方法是在活动的布局中声明它:
...
<fragment android:name="com.example.my.FragmentClass"
android:id="@+id/fragment_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
... (further parameters if needed)
/>
...进一步的信息,你会发现这里。
祝好运。
发布于 2015-04-23 20:56:38
第一个问题
在构造函数i = new ImageView(TopWordsearchFragment.this.getActivity());中使用片段的父活动
第二问题
Binary XML file line #8: Error inflating class fragment指出了XML文件中的语法问题。你应该把这个文件的内容加到你的问题上。
https://stackoverflow.com/questions/29833957
复制相似问题