Reputation: 2216
I am facing this exception .I checked many solutions but can't figure out what's the problem in my code. This 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);
}
}
I have also a class which extends 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);
}
}
This line is excuting the background proces in MainActivity :
new Fetch().execute("Hardware");
I need help in this , Thanks in advance
Log Cat Output
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)
Upvotes: 0
Views: 889
Reputation: 6788
Problem is :
protected void onPostExecute(Hardware h) {
**MainActivity m = new MainActivity();**
m.populateList(h);
}
Activity life cycle is taken care by Android itself. You should not instantiate the Activity like this. Rather pass the activity instance as parameter to the AsyncTask. And access it
onPostExecute()
method
Upvotes: 1