使用ListActivty中使用的适配器的IllegalStateException

时间:2012-12-09 14:43:40

标签: java android android-arrayadapter listactivity

我遇到了一个问题,我在调用ListActivity时,它告诉我系统功能在onCreate之前不起作用。我看到onCreate被调用(根据我的代码),但它给了我IllegalStateException,这是我以前从未处理过的。顺便说一下,我正在使用SherlockListActivity。这是我的代码:

    public class MenuActivity extends SherlockListActivity {
    ListView scroll;
    int time;       

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);         
        scroll = this.getListView();
        this.setListAdapter(new MyAdapter());           
    }


    public View find(int x){
        return findViewById(x);
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        // TODO Auto-generated method stub
        super.onListItemClick(l, v, position, id);
        switch (position){
        case (0):{
            Toast.makeText(getApplicationContext(), "Hello you just chose \"Splash\"", Toast.LENGTH_SHORT).show();
        }break;
        case (1):{
            Toast.makeText(getApplicationContext(), "Its another one here !", Toast.LENGTH_SHORT).show();
        }break;
        case (2):{
            Toast.makeText(getApplicationContext(), String.valueOf(id) +" and "+ position, Toast.LENGTH_SHORT).show();

        }break;
        }


    }

    public void showToast(String toast){
        Toast.makeText(getApplicationContext(), toast, Toast.LENGTH_SHORT).show();
    }



}

class MyAdapter extends ArrayAdapter<String>{       

    LayoutInflater inf = LayoutInflater.from(this.getContext());

    public MyAdapter() {
        super(new MenuActivity() , 0 ,stringArray);
        // TODO Auto-generated constructor stub
    }


    public static String[] stringArray= new String[]{"Splash"   , "Another Example" , "Future Checkable"};


    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        // TODO Auto-generated method stub

        if (convertView == null){
            inf.inflate(R.layout.normal_row, parent , false);
        }
        switch(position){
        case(0):
            convertView = (LinearLayout) inf.inflate(R.layout.row_switch, parent,false);

            break;
        }


        return super.getView(position, convertView, parent);
    }




}

我的Stack Trace:

    12-09 16:19:45.144: E/AndroidRuntime(28754): FATAL EXCEPTION: main
12-09 16:19:45.144: E/AndroidRuntime(28754): java.lang.RuntimeException: Unable to start activity ComponentInfo{seaskyways.testingproject/seaskyways.testingproject.MenuActivity}: java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.os.Handler.dispatchMessage(Handler.java:99)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.os.Looper.loop(Looper.java:137)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.ActivityThread.main(ActivityThread.java:5039)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at java.lang.reflect.Method.invokeNative(Native Method)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at java.lang.reflect.Method.invoke(Method.java:511)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at dalvik.system.NativeStart.main(Native Method)
12-09 16:19:45.144: E/AndroidRuntime(28754): Caused by: java.lang.IllegalStateException: System services not available to Activities before onCreate()
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.Activity.getSystemService(Activity.java:4463)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:128)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at seaskyways.testingproject.MyAdapter.<init>(MenuActivity.java:82)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at seaskyways.testingproject.MenuActivity.onCreate(MenuActivity.java:26)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.Activity.performCreate(Activity.java:5104)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
12-09 16:19:45.144: E/AndroidRuntime(28754):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
12-09 16:19:45.144: E/AndroidRuntime(28754):    ... 11 more

1 个答案:

答案 0 :(得分:1)

不要将新的Activity实例化为适配器的Context,而是在实例化适配器的位置传递this

LayoutInflater inf; 

public MyAdapter(Context context) {
    super(context, 0 ,stringArray);
    inf = LayoutInflater.from(context);
}

以及MenuActivity的{​​{1}}方法:

onCreate

此外,在您的this.setListAdapter(new MyAdapter(this)); 方法中,返回getView()而不是超级调用返回的视图:

convertView