我正在开发一个Android应用程序,其中有活动列表。在某个时间启动任何活动。所以想要包括progess栏直到下一个活动启动。
我在进度条上经历了很多问题但是没有得到确切的解决方案。所以,任何人都可以建议我在我的应用程序中集成进度条的最佳方法。
我正在分享我的代码,而logCat无法正常工作。
package com.example.progress;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
private ProgressDialog progress;
Button Button;
@Override
protected void onCreate(Bundle savedInstanceState)
{
progress = new ProgressDialog(MainActivity.this);
Button=(Button)findViewById(R.id.button1);
Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
progress.setMessage("Please Wait Loading...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
new Thread()
{
public void run()
{
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
progress.dismiss();
}
}.start();
break;
}
}
});
}
}
LOgCat Error :::
-----------
01-27 17:23:19.094: D/AndroidRuntime(13666): Shutting down VM
01-27 17:23:19.095: E/AndroidRuntime(13666): FATAL EXCEPTION: main
01-27 17:23:19.095: E/AndroidRuntime(13666): Process: com.example.progress, PID: 13666
01-27 17:23:19.095: E/AndroidRuntime(13666): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.progress/com.example.progress.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2339)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2413)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.ActivityThread.access$800(ActivityThread.java:155)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1317)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.os.Handler.dispatchMessage(Handler.java:102)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.os.Looper.loop(Looper.java:135)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.ActivityThread.main(ActivityThread.java:5343)
01-27 17:23:19.095: E/AndroidRuntime(13666): at java.lang.reflect.Method.invoke(Native Method)
01-27 17:23:19.095: E/AndroidRuntime(13666): at java.lang.reflect.Method.invoke(Method.java:372)
01-27 17:23:19.095: E/AndroidRuntime(13666): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:907)
01-27 17:23:19.095: E/AndroidRuntime(13666): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:702)
01-27 17:23:19.095: E/AndroidRuntime(13666): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
01-27 17:23:19.095: E/AndroidRuntime(13666): at com.example.progress.MainActivity.onCreate(MainActivity.java:22)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.Activity.performCreate(Activity.java:6010)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
01-27 17:23:19.095: E/AndroidRuntime(13666): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2292)
01-27 17:23:19.095: E/AndroidRuntime(13666): ... 10 more
答案 0 :(得分:0)
假设您的活动显示简单列表视图
private ProgressBar mProgBar;
mProgBar= (ProgressBar) findViewById(R.id.progressBarMain);
mProgBar.setVisibility(View.VISIBLE); //before doing your desired operation
/* do your stuff here populate your listview */
mProgBar.setVisibility(View.INVISIBLE); //hide the progressbar
//display your list here
答案 1 :(得分:0)
当您点击Progressbar
转到下一个活动或启动它时,如何启动Button
,然后停止/关闭onPause()
中的栏?
答案 2 :(得分:0)
尝试修改onClick()
,如下所示,看progressBar
正常工作
由于您的progressBar
属于当前Activity
,因此当您启动另一个Activity
时,您无法看到progress bar
Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
progress.setMessage("Please Wait Loading...");
progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
progress.setIndeterminate(true);
progress.show();
new Thread()
{
public void run()
{
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
Intent i = new Intent(MainActivity.this,Second.class);
startActivity(i);
progress.dismiss();
}
}.start();
break;
}
}
});