Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android进度条在获取最大值时启动新活动_Android_Android Progressbar - Fatal编程技术网

Android进度条在获取最大值时启动新活动

Android进度条在获取最大值时启动新活动,android,android-progressbar,Android,Android Progressbar,我创建了一个进度条,希望它在进度条达到100时启动一个新活动。但我的代码似乎不正确。我试图改变它,但我得到了错误,或者它就是不工作。有人能帮我解决我的问题吗,或者给我一些有效的代码?因为我已经尝试了很多方法,但在加载完成后,它不会进入第二个活动 主要活动: package com.example.brandon.territories; import android.content.Context; import android.content.Intent; import android.o

我创建了一个进度条,希望它在进度条达到100时启动一个新活动。但我的代码似乎不正确。我试图改变它,但我得到了错误,或者它就是不工作。有人能帮我解决我的问题吗,或者给我一些有效的代码?因为我已经尝试了很多方法,但在加载完成后,它不会进入第二个活动

主要活动:

package com.example.brandon.territories;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.AttributeSet;
import android.view.Menu;
import android.widget.ImageView;
import android.widget.ProgressBar;

public class MainActivity extends AppCompatActivity {
private ProgressBar progressBar;
private int progressStatus = 0;
private Handler handler = new Handler();
private ImageView ImageView;
private int progress;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    progressBar = (ProgressBar) findViewById(R.id.progressBar);
    ImageView imageView = (ImageView) findViewById(R.id.imageView);

    //Long operation by thread
    new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) {
                progressStatus += 5;
                //Update progress bar with completion of operation
                handler.post(new Runnable() {
                    public void run() {
                        progressBar.setProgress(progressStatus);
                    }
                });
                try {
                    // Sleep for 300 milliseconds.
                    //Just to display the progress slowly
                    Thread.sleep(300);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }).start();
}

class MyProgressBar extends ProgressBar {

    MyProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    public MyProgressBar(Context context) { super(context);

    }

    @Override
    public void setProgress(int progress) {
        super.setProgress(progress);
        if (progress == this.getMax()) {
            //Do stuff when progress is max
            Intent intent = new Intent(MainActivity.this, SecondActivity.class);
            startActivity(intent);
        }
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items
    //to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}
}
package com.example.brandon.territories;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

}

}

最简单的解决方案是删除定制的ProgressBar子类,您不使用它。并在线程中设置进度时检查“已达到最大进度”

代码可以是这样的:

//Long operation by thread
new Thread(new Runnable() {
    public void run() {
        while (progressStatus < progressBar.getMax()) {
            progressStatus += 5;
            //Update progress bar with completion of operation
            handler.post(new Runnable() {
                public void run() {
                    progressBar.setProgress(progressStatus);
                }
            });
            try {
                // Sleep for 300 milliseconds.
                //Just to display the progress slowly
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        handler.post(new Runnable() {
             public void run() {
                  Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                  startActivity(intent);
             }
        });
    }
}).start();
//线程的长操作
新线程(newrunnable()){
公开募捐{
while(progressStatus

希望有帮助

为什么在启动应用程序时使用2次意向筛选。首先请修改您的manifest@RajanBhavsar不,我没有t@IntelliJAmiya这就是问题所在,我不知道在哪里进行更改,即使我这样做了也不会起作用。您甚至没有使用自定义的
MyProgressBar
您希望它如何调用
startActivity(意图);
    <application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:supportsRtl="true"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".Splash"
        android:label="@string/title_activity_splash"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:label="@string/app_name"
        android:theme="@style/AppTheme.NoActionBar" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".SecondActivity"
        android:label="@string/title_activity_second"
        android:theme="@style/AppTheme.NoActionBar" >
    </activity>
</application>

</manifest>
package com.example.brandon.territories;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;


public class SecondActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);

}

}
//Long operation by thread
new Thread(new Runnable() {
    public void run() {
        while (progressStatus < progressBar.getMax()) {
            progressStatus += 5;
            //Update progress bar with completion of operation
            handler.post(new Runnable() {
                public void run() {
                    progressBar.setProgress(progressStatus);
                }
            });
            try {
                // Sleep for 300 milliseconds.
                //Just to display the progress slowly
                Thread.sleep(300);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }
        handler.post(new Runnable() {
             public void run() {
                  Intent intent = new Intent(MainActivity.this, SecondActivity.class);
                  startActivity(intent);
             }
        });
    }
}).start();