GuruBhai
GuruBhai

Reputation: 31

Splash screen with Horizontal Progress bar

i have created a simple splash screen with the horizontal progress bar. till my knowledge the code is correct. Still it is showing some errors in Logcat. I have looked form more options in others tutorials but none of them have provided me the solution which i was looking for. So if any body can help me solving my error that would be greatful. Here i am presenting my codes to you with the other files also.

MainActivity()

public class MainActivity extends Activity {

ProgressBar progressBar;
int progressStatus = 0;
TextView textView1, textView2;
Handler handler = new Handler();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    textView2 = (TextView) findViewById(R.id.load_per);
    new Thread(new Runnable() {
        public void run() {
            while (progressStatus < 100) 
            {
                progressStatus += 1;
                handler.post(new Runnable()
                {
                    public void run() 
                    {
                        progressBar.setProgress(progressStatus);
                        textView2.setText(progressStatus + "%");
                    }
                });
                try 
                {
                    Thread.sleep(200);
                } 
                catch (InterruptedException e) 
                {
                    e.printStackTrace();
                }
            }
            if (progressStatus==100) 
            {
                Intent i = new Intent(MainActivity.this, LoginActivity.class);
                startActivity(i);
            } 
        }
    }).start();
}}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background1"
tools:context="${packageName}.${activityClass}" >

<ProgressBar
    android:id="@+id/progressBar1"
    style="?android:attr/progressBarStyleHorizontal"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:max="100"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true" />

<ImageView
    android:id="@+id/imageView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:contentDescription="@string/contentDescription"
    android:src="@drawable/logo" />

<TextView
    android:id="@+id/Title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:text="@string/EventManag"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:textColor="#FFFFFF"
    android:textSize="30sp" />

<TextView
    android:id="@+id/load_per"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/progressBar1"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffff"
    android:text=""
    android:textAppearance="?android:attr/textAppearanceSmall" />

LogCat

05-29 17:26:09.807: E/AndroidRuntime(1022): FATAL EXCEPTION: main
05-29 17:26:09.807: E/AndroidRuntime(1022): java.lang.NullPointerException
05-29 17:26:09.807: E/AndroidRuntime(1022):     at com.example.temp9.MainActivity$1$1.run(MainActivity.java:31)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at android.os.Handler.handleCallback(Handler.java:605)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at android.os.Handler.dispatchMessage(Handler.java:92)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at android.os.Looper.loop(Looper.java:137)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at android.app.ActivityThread.main(ActivityThread.java:4340)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at java.lang.reflect.Method.invokeNative(Native Method)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at java.lang.reflect.Method.invoke(Method.java:511)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-29 17:26:09.807: E/AndroidRuntime(1022):     at dalvik.system.NativeStart.main(Native Method)
05-29 17:26:18.747: I/Process(1022): Sending signal. PID: 1022 SIG: 9

Upvotes: 2

Views: 6100

Answers (1)

Spring Breaker
Spring Breaker

Reputation: 8251

Your ProgressBar is not initialized.Initialize it before using it.

progressBar=(ProgressBar)findViewById(R.id.progressBar1);

If a View is not initialized with proper corresponding id from the xml file, it will throw NullPointerException.

Upvotes: 1

Related Questions