android:setAdapter()未解析。该怎么办?

时间:2017-08-15 22:04:22

标签: java android android-studio

package com.example.ramya.trailmix;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
    }

    String options[] = {"SCHOOL","SHOPPING","RESTAURANT","CUSTOM"};

    ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.activity_menu,options);
    ListView listView = (ListView) findViewById(R.id.MenuList);
    listView.setAdapter(adapter);

}

setAdapter()尚未解决。当我尝试导入android.app.Activityandroid.view.Menu时,该语句将被忽略。我是Android工作室的新手。我该怎么解决这个问题?我的目标是在此活动的视图中将选项[]显示为ListView

1 个答案:

答案 0 :(得分:0)

将所有内容移至onCreate()

public class MenuActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_menu);
        String options[] = {"SCHOOL","SHOPPING","RESTAURANT","CUSTOM"};

        ArrayAdapter adapter = new ArrayAdapter<String>(this,R.layout.activity_menu,options);
        ListView listView = (ListView) findViewById(R.id.MenuList);
        listView.setAdapter(adapter);
    }
}