我对编码很陌生,尤其是 Java,所以如果我错过了任何基本显而易见的东西,我深表歉意。用外行的话回答会很有帮助。
我正在尝试创建一个单词搜索应用程序。一个非常简单的游戏版本,没什么花哨的。我制作了一个网格视图来容纳字母的图像以及要在哪里进行工作选择,这是最上面的片段。底部片段包含正在搜索的字母。
这是包含网格视图的顶部片段的代码。这适用于独立的应用程序,每个字母都是可选的,并且可以进行多项选择。但是,当尝试将其放入片段i = new ImageView(TopWordsearchFragment.this);
TopWordsearchFragment 时会抛出错误“ImageView(android.content.Context)中的 ImageView 无法应用于(com.example.sebastian.multipleselectiongrid.TopWordSearchFragment”
这是我的第一个问题
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 而不仅仅是 Activity,它工作正常。我检查了 xml 文件,它们完全按照@Kumiho 的建议进行编码。
至于问题 1,我无法解决在片段中使用此特定代码的问题,因此我将其留在了 MainActivity 中(经过适当的编辑)。两者正在像我希望的那样一起工作。现在来实现功能。
再次,非常感谢。