当我尝试初始化一个 class 以调用其方法之一时,出现错误,如何解决?
When I try to initialize a class to call one of its method, an error comes up, how to resolve this?
我正在制作一个需要自定义数组适配器的 android 应用程序。在此 class 中,扩展数组适配器由媒体播放器初始化组成。我在此 class 下创建了另一种方法,以便在与单词适配器相关联的 activity 停止后立即释放媒体播放器。当我在 activity 中创建停止方法并尝试初始化自定义数组适配器 class 时出现错误,提示 "Constructor cannot be applied to ()",或者当我尝试 WordAdapter w=null 并调用该方法时我不想出错,但在运行时应用程序强制关闭!
这是 activity 代码:
public class ColorsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// Create a list of words
ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word("red", "weṭeṭṭi",R.drawable.color_red,R.raw.color_red));
words.add(new Word("mustard yellow", "chiwiiṭә",R.drawable.color_mustard_yellow,R.raw.color_mustard_yellow));
words.add(new Word("dusty yellow", "ṭopiisә",R.drawable.color_dusty_yellow,R.raw.color_dusty_yellow));
words.add(new Word("green", "chokokki",R.drawable.color_green,R.raw.color_green));
words.add(new Word("brown", "ṭakaakki",R.drawable.color_brown,R.raw.color_brown));
words.add(new Word("gray", "ṭopoppi",R.drawable.color_gray,R.raw.color_gray));
words.add(new Word("black", "kululli",R.drawable.color_black,R.raw.color_black));
words.add(new Word("white", "kelelli",R.drawable.color_white,R.raw.color_white));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
WordAdapter adapter = new WordAdapter(this, words,R.color.category_colors);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
}
@Override
protected void onStop()
{
WordAdapter w= new WordAdapter();
w.terminate();
super.onStop();
// TODO: Implement this method
}
}
自定义适配器代码:
public class WordAdapter extends ArrayAdapter<Word> {
int b;
MediaPlayer mp;
/**
* Create a new {@link WordAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param words is the list of {@link Word}s to be displayed.
*/
private int mbgcolor;
public WordAdapter(Context context, ArrayList<Word> words, int BgColor)
{
super(context, 0, words);
mbgcolor = BgColor;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null)
{
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID miwok_text_view.
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
// Get the Miwok translation from the currentWord object and set this text on
// the Miwok TextView.
miwokTextView.setText(currentWord.getMiwokTranslation());
// Find the TextView in the list_item.xml layout with the ID default_text_view.
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
defaultTextView.setText(currentWord.getDefaultTranslation());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
if (currentWord.hasImage())
{
ImageView iconView = (ImageView) listItemView.findViewById(R.id.miwok_image_view);
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
iconView.setImageResource(currentWord.getImageResourceId());
iconView.setVisibility(View.VISIBLE);
}
else
{
ImageView iconView = (ImageView) listItemView.findViewById(R.id.miwok_image_view);
iconView.setVisibility(View.GONE);
}
View text_container=listItemView.findViewById(R.id.text_container);
int bgc=ContextCompat.getColor(getContext(), mbgcolor);
text_container.setBackgroundColor(bgc);
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
int song= currentWord.getSongResourceId();
mp=MediaPlayer.create(getContext(),song);
text_container.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
mp.start();
}
});
return listItemView;
}
public void terminate()
{
mp.release();
}
}
如何解决这个问题?
您每次都在 onStop 中实例化一个新的适配器,而不是使用现有的适配器。
ColorsActivity extends AppCompatActivity {
private WordAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// Create a list of words
ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word("red", "weṭeṭṭi",R.drawable.color_red,R.raw.color_red));
words.add(new Word("mustard yellow", "chiwiiṭә",R.drawable.color_mustard_yellow,R.raw.color_mustard_yellow));
words.add(new Word("dusty yellow", "ṭopiisә",R.drawable.color_dusty_yellow,R.raw.color_dusty_yellow));
words.add(new Word("green", "chokokki",R.drawable.color_green,R.raw.color_green));
words.add(new Word("brown", "ṭakaakki",R.drawable.color_brown,R.raw.color_brown));
words.add(new Word("gray", "ṭopoppi",R.drawable.color_gray,R.raw.color_gray));
words.add(new Word("black", "kululli",R.drawable.color_black,R.raw.color_black));
words.add(new Word("white", "kelelli",R.drawable.color_white,R.raw.color_white));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
mAdapter = new WordAdapter(this, words,R.color.category_colors);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(mAdapter);
}
@Override
protected void onStop()
{
mAdapter.terminate();
super.onStop();
// TODO: Implement this method
}
}
首先,这与Android
无关,这是一个通用的编程问题!
你的class(WordAdapter)
的构造函数是这样定义的:
public WordAdapter(Context context, ArrayList<Word> words, int BgColor)
{
super(context, 0, words);
mbgcolor = BgColor;
}
问题是:
1- 你说错了! WordAdapter w= new WordAdapter();
您正在调用 class 中不存在的空构造函数!所以这当然会告诉你错误
2- 即使这个构造函数(空构造函数)存在,你的代码仍然是错误的,因为在 onStop
中你调用了 terminate()
class 的新对象,而不是您在 onCreate
中使用的对象(您将其命名为 adapter
)。
因此,作为此解决方案,使 adapter
对象成为所有 Activity
方法的全局变量,如下所示:
private WordAdapter adapter;
和在onCreate
和onStop
中使用
我正在制作一个需要自定义数组适配器的 android 应用程序。在此 class 中,扩展数组适配器由媒体播放器初始化组成。我在此 class 下创建了另一种方法,以便在与单词适配器相关联的 activity 停止后立即释放媒体播放器。当我在 activity 中创建停止方法并尝试初始化自定义数组适配器 class 时出现错误,提示 "Constructor cannot be applied to ()",或者当我尝试 WordAdapter w=null 并调用该方法时我不想出错,但在运行时应用程序强制关闭! 这是 activity 代码:
public class ColorsActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// Create a list of words
ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word("red", "weṭeṭṭi",R.drawable.color_red,R.raw.color_red));
words.add(new Word("mustard yellow", "chiwiiṭә",R.drawable.color_mustard_yellow,R.raw.color_mustard_yellow));
words.add(new Word("dusty yellow", "ṭopiisә",R.drawable.color_dusty_yellow,R.raw.color_dusty_yellow));
words.add(new Word("green", "chokokki",R.drawable.color_green,R.raw.color_green));
words.add(new Word("brown", "ṭakaakki",R.drawable.color_brown,R.raw.color_brown));
words.add(new Word("gray", "ṭopoppi",R.drawable.color_gray,R.raw.color_gray));
words.add(new Word("black", "kululli",R.drawable.color_black,R.raw.color_black));
words.add(new Word("white", "kelelli",R.drawable.color_white,R.raw.color_white));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
WordAdapter adapter = new WordAdapter(this, words,R.color.category_colors);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(adapter);
}
@Override
protected void onStop()
{
WordAdapter w= new WordAdapter();
w.terminate();
super.onStop();
// TODO: Implement this method
}
}
自定义适配器代码:
public class WordAdapter extends ArrayAdapter<Word> {
int b;
MediaPlayer mp;
/**
* Create a new {@link WordAdapter} object.
*
* @param context is the current context (i.e. Activity) that the adapter is being created in.
* @param words is the list of {@link Word}s to be displayed.
*/
private int mbgcolor;
public WordAdapter(Context context, ArrayList<Word> words, int BgColor)
{
super(context, 0, words);
mbgcolor = BgColor;
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
// Check if an existing view is being reused, otherwise inflate the view
View listItemView = convertView;
if (listItemView == null)
{
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
// Get the {@link Word} object located at this position in the list
Word currentWord = getItem(position);
// Find the TextView in the list_item.xml layout with the ID miwok_text_view.
TextView miwokTextView = (TextView) listItemView.findViewById(R.id.miwok_text_view);
// Get the Miwok translation from the currentWord object and set this text on
// the Miwok TextView.
miwokTextView.setText(currentWord.getMiwokTranslation());
// Find the TextView in the list_item.xml layout with the ID default_text_view.
TextView defaultTextView = (TextView) listItemView.findViewById(R.id.default_text_view);
// Get the default translation from the currentWord object and set this text on
// the default TextView.
defaultTextView.setText(currentWord.getDefaultTranslation());
// Find the ImageView in the list_item.xml layout with the ID list_item_icon
if (currentWord.hasImage())
{
ImageView iconView = (ImageView) listItemView.findViewById(R.id.miwok_image_view);
// Get the image resource ID from the current AndroidFlavor object and
// set the image to iconView
iconView.setImageResource(currentWord.getImageResourceId());
iconView.setVisibility(View.VISIBLE);
}
else
{
ImageView iconView = (ImageView) listItemView.findViewById(R.id.miwok_image_view);
iconView.setVisibility(View.GONE);
}
View text_container=listItemView.findViewById(R.id.text_container);
int bgc=ContextCompat.getColor(getContext(), mbgcolor);
text_container.setBackgroundColor(bgc);
// Return the whole list item layout (containing 2 TextViews) so that it can be shown in
// the ListView.
int song= currentWord.getSongResourceId();
mp=MediaPlayer.create(getContext(),song);
text_container.setOnClickListener(new OnClickListener(){
public void onClick(View v)
{
mp.start();
}
});
return listItemView;
}
public void terminate()
{
mp.release();
}
}
如何解决这个问题?
您每次都在 onStop 中实例化一个新的适配器,而不是使用现有的适配器。
ColorsActivity extends AppCompatActivity {
private WordAdapter mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.word_list);
// Create a list of words
ArrayList<Word> words = new ArrayList<Word>();
words.add(new Word("red", "weṭeṭṭi",R.drawable.color_red,R.raw.color_red));
words.add(new Word("mustard yellow", "chiwiiṭә",R.drawable.color_mustard_yellow,R.raw.color_mustard_yellow));
words.add(new Word("dusty yellow", "ṭopiisә",R.drawable.color_dusty_yellow,R.raw.color_dusty_yellow));
words.add(new Word("green", "chokokki",R.drawable.color_green,R.raw.color_green));
words.add(new Word("brown", "ṭakaakki",R.drawable.color_brown,R.raw.color_brown));
words.add(new Word("gray", "ṭopoppi",R.drawable.color_gray,R.raw.color_gray));
words.add(new Word("black", "kululli",R.drawable.color_black,R.raw.color_black));
words.add(new Word("white", "kelelli",R.drawable.color_white,R.raw.color_white));
// Create an {@link WordAdapter}, whose data source is a list of {@link Word}s. The
// adapter knows how to create list items for each item in the list.
mAdapter = new WordAdapter(this, words,R.color.category_colors);
// Find the {@link ListView} object in the view hierarchy of the {@link Activity}.
// There should be a {@link ListView} with the view ID called list, which is declared in the
// word_list.xml layout file.
ListView listView = (ListView) findViewById(R.id.list);
// Make the {@link ListView} use the {@link WordAdapter} we created above, so that the
// {@link ListView} will display list items for each {@link Word} in the list.
listView.setAdapter(mAdapter);
}
@Override
protected void onStop()
{
mAdapter.terminate();
super.onStop();
// TODO: Implement this method
}
}
首先,这与Android
无关,这是一个通用的编程问题!
你的class(WordAdapter)
的构造函数是这样定义的:
public WordAdapter(Context context, ArrayList<Word> words, int BgColor)
{
super(context, 0, words);
mbgcolor = BgColor;
}
问题是:
1- 你说错了! WordAdapter w= new WordAdapter();
您正在调用 class 中不存在的空构造函数!所以这当然会告诉你错误
2- 即使这个构造函数(空构造函数)存在,你的代码仍然是错误的,因为在 onStop
中你调用了 terminate()
class 的新对象,而不是您在 onCreate
中使用的对象(您将其命名为 adapter
)。
因此,作为此解决方案,使 adapter
对象成为所有 Activity
方法的全局变量,如下所示:
private WordAdapter adapter;
和在onCreate
和onStop