0

我遇到了一个问题,在调用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);
    }




}

还有我的堆栈跟踪:

    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

4

1 回答 1

1

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

LayoutInflater inf; 

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

并在MenuActivity'onCreate方法中:

this.setListAdapter(new MyAdapter(this));

此外,在您的getView()方法中convertView,通过 super 调用返回而不是视图返回:

return convertView;
于 2012-12-09T14:51:00.740 回答