クラスを初期化してそのメソッドの 1 つを呼び出そうとすると、エラーが発生します。これを解決するにはどうすればよいですか?

スティーブ・ザ・ジョブレス

カスタム アレイ アダプターを必要とする Android アプリを作成しています。配列アダプタを拡張するこのクラスでは、メディア プレーヤーの初期化で構成されます。このクラスの下に別のメソッドを作成して、アダプターという単語が関連付けられているアクティビティが停止するとすぐにメディア プレーヤーを解放します。アクティビティで停止メソッドを作成し、カスタム配列アダプター クラスを初期化しようとすると、「コンストラクターを () に適用できません」というエラーが表示されます。エラーが発生しましたが、実行中にアプリケーションが強制的に閉じられました! アクティビティ コードは次のとおりです。

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

この問題を解決するには?

アテフ・ハレス

まず第一に、これは とは関係ありません.これはAndroid一般的なプログラミングの質問です!


あなたclass(WordAdapter)のコンストラクタは次のように定義されます:

public WordAdapter(Context context, ArrayList<Word> words, int BgColor)
{
    super(context, 0, words);
    mbgcolor = BgColor;
}

問題は:

1 - あなたはそれを間違って呼んでいます!WordAdapter w= new WordAdapter();

クラスに存在しない空のコンストラクターを呼び出しています! もちろん、これはエラーを表示します

2 - このコンストラクター (空のコンストラクター) が存在する場合でも、コードは間違っていますこれは、使用したクラス(名前を付けたクラス) 以外のクラスの新しいオブジェクトをonStop呼び出しterminate()いるためですonCreateadapter

したがって、これに対する解決策として、adapterオブジェクトActivityを次のようなすべてのメソッドのグローバル変数にします

private WordAdapter adapter;

そして内部それ使用し、内部onCreateでも使用しますonStop

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

Related 関連記事

ホットタグ

アーカイブ