0

我正面临这个异常。我检查了许多解决方案,但无法弄清楚我的代码中有什么问题。这个 MainActivity.java :

public class MainActivity extends ListActivity {


private Button button , button1;
private ListView lv;
final String[] listitems = new String[2];
ArrayAdapter<String> adapter_list = null;


@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    final Context context = this;

    button = (Button) findViewById(R.id.button1);
    button1 = (Button) findViewById(R.id.button2);
    lv  =(ListView)findViewById(R.id.android_listView1);

    button.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {

            new Fetch().execute("Hardware");

            //populateList(listitems);
        }                   

    });

    button1.setOnClickListener(new OnClickListener() {
        @Override
        public void onClick(View v) {               
        }                   
    });

}

public void populateList(Hardware h)
{
    listitems[0] = h.device;
    listitems[1] = h.manufacturer;
    adapter_list=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, listitems);
    lv.setAdapter(adapter_list);
}

}

我还有一个扩展 AsyncTask 的类:

public class Fetch extends AsyncTask<String, Void, Hardware>  {
@Override
protected Hardware doInBackground(String... str) {


        Hardware h = new Hardware();
        h.manufacturer = "Hello";
        h.device = "Hello";

        return h;       
}   
 protected void onPostExecute(Hardware h) {
     MainActivity m = new MainActivity();
     m.populateList(h);
 }

}

此行正在执行 MainActivity 中的后台进程:

new Fetch().execute("Hardware");

我需要帮助,在此先感谢

日志猫输出

06-28 09:49:47.878: W/IInputConnectionWrapper(6925): showStatusIcon on inactive InputConnection
06-28 09:49:49.777: D/AndroidRuntime(6925): Shutting down VM
06-28 09:49:49.777: W/dalvikvm(6925): threadid=1: thread exiting with uncaught exception (group=0x4001d7d0)
06-28 09:49:49.788: E/AndroidRuntime(6925): FATAL EXCEPTION: main
06-28 09:49:49.788: E/AndroidRuntime(6925): java.lang.IllegalStateException: System services not available to Activities before onCreate()
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.app.Activity.getSystemService(Activity.java:3526)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.widget.ArrayAdapter.init(ArrayAdapter.java:271)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:125)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at com.example.first.MainActivity.populateList(MainActivity.java:87)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at com.example.first.Fetch.onPostExecute(Fetch.java:38)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at com.example.first.Fetch.onPostExecute(Fetch.java:1)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.os.AsyncTask.finish(AsyncTask.java:417)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.os.Looper.loop(Looper.java:123)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at android.app.ActivityThread.main(ActivityThread.java:4627)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at java.lang.reflect.Method.invokeNative(Native Method)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at java.lang.reflect.Method.invoke(Method.java:521)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:871)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
06-28 09:49:49.788: E/AndroidRuntime(6925):     at dalvik.system.NativeStart.main(Native Method)
4

1 回答 1

1

问题是:

protected void onPostExecute(Hardware h) {
     **MainActivity m = new MainActivity();**
     m.populateList(h);
 }

Activity 生命周期由 Android 自己负责。您不应该像这样实例化 Activity。而是将活动实例作为参数传递给 AsyncTask。并访问它

onPostExecute()方法

于 2013-06-28T10:07:51.860 回答