Reputation: 327
I use loading before load completed listview
This code worked :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listContent = (ListView) findViewById(R.id.listContent);
adapter = new AdapterNote(G.tasks);
listContent.setAdapter(adapter);
Commands.readData();
adapter.notifyDataSetChanged();
}
But after changing to the below code , application crashed and force closed :
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startDialog();
}
ProgressDialog pd;
private void startDialog() {
pd = ProgressDialog.show(NoteActivity.this, "title", "loading");
new Thread(new Runnable() {
@Override
public void run() {
final ListView listContent = (ListView) findViewById(R.id.listContent);
adapter = new AdapterNote(G.tasks);
listContent.setAdapter(adapter);
Commands.readData();
adapter.notifyDataSetChanged();
handler.sendEmptyMessage(0);
}
}).start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
pd.dismiss();
}
};
In log cat these errors occurred :
03-08 00:27:09.734: E/AndroidRuntime(334): FATAL EXCEPTION: main
03-08 00:27:09.734: E/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.NoteProject.download.app/com.NoteProject.download.app.NoteNoteProjectActivity}: java.lang.NullPointerException
03-08 00:27:09.734: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.os.Handler.dispatchMessage(Handler.java:99)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.os.Looper.loop(Looper.java:123)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.app.ActivityThread.main(ActivityThread.java:4627)
03-08 00:27:09.734: E/AndroidRuntime(334): at java.lang.reflect.Method.invokeNative(Native Method)
03-08 00:27:09.734: E/AndroidRuntime(334): at java.lang.reflect.Method.invoke(Method.java:521)
03-08 00:27:09.734: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
03-08 00:27:09.734: E/AndroidRuntime(334): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
03-08 00:27:09.734: E/AndroidRuntime(334): at dalvik.system.NativeStart.main(Native Method)
03-08 00:27:09.734: E/AndroidRuntime(334): Caused by: java.lang.NullPointerException
03-08 00:27:09.734: E/AndroidRuntime(334): at com.NoteProject.download.app.NoteNoteProjectActivity$2.onScroll(NoteNoteProjectActivity.java:79)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.widget.AbsListView.invokeOnItemScrollListener(AbsListView.java:675)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.widget.AbsListView.setOnScrollListener(AbsListView.java:664)
03-08 00:27:09.734: E/AndroidRuntime(334): at com.NoteProject.download.app.NoteNoteProjectActivity.onCreate(NoteNoteProjectActivity.java:69)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
03-08 00:27:09.734: E/AndroidRuntime(334): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
03-08 00:27:09.734: E/AndroidRuntime(334): ... 11 more
How to fix it ?
In commands.readdata()
, return listview from web
Thx
Upvotes: 0
Views: 398
Reputation: 442
You can use Async task to load listview in background. Code written below works for me fine.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ListView listContent = (ListView) findViewById(R.id.listContent);
new AsyncTask<Void, Void, Void>()
{
@Override
protected void onPreExecute()
{
pd = ProgressDialog.show((NoteActivity.this, "title", "loading", true, false);
}// End of onPreExecute method
@Override
protected Void doInBackground(Void... params)
{
//reading data for listview
Commands.readData();
return null;
}// End of doInBackground method
@Override
protected void onPostExecute(Void result)
{
pd.dismiss();
//setting adapter here
adapter = new AdapterNote(G.tasks);
listContent.setAdapter(adapter);}//End of onPostExecute method
}.execute((Void[]) null);
}
Upvotes: 0
Reputation: 197
adapter.notifyDataSetChanged(); must be run at UI thread, this will works:
private void startDialog() {
pd = ProgressDialog.show(NoteActivity.this, "title", "loading");
final ListView listContent = (ListView) findViewById(R.id.listContent);
adapter = new AdapterNote(G.tasks);
listContent.setAdapter(adapter);
new Thread(new Runnable() {
@Override
public void run() {
Commands.readData();
handler.sendEmptyMessage(0);
}
}).start();
}
Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
adapter.notifyDataSetChanged();
pd.dismiss();
}
};
Upvotes: 1
Reputation: 590
Updatinh the liatview is a UI operation and it needs to be performed on the main (UI) thread.
Upvotes: 1