2

I write a simple programm, but there is problem. Here is the code:

package player.org;

import android.app.Activity;
import android.content.Intent;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.AlphaAnimation;
import android.view.animation.Animation;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

public class Player extends Activity {
    /** Called when the activity is first created. */
    private MediaPlayer media;
    private ProgressBar progress;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.tabs);

        progress=(ProgressBar) findViewById(R.id.progress);

       progress.setVisibility(ProgressBar.INVISIBLE);

And here is the tabs.xml

<?xml version="1.0" encoding="utf-8"?>
    <AbsoluteLayout 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     xmlns:android="http://schemas.android.com/apk/res/android"
     android:background="#FFFFFF">
     <ProgressBar
        android:id="@+id/progress"
        android:layout_width="250dp"
        android:layout_height="8dp"
        style="@android:style/Widget.ProgressBar.Horizontal"
        android:layout_x="35dp"
        android:layout_y="320dp"/>
</AbsoluteLayout>

But when I run program, it's force closed because of Null pointer exception. Can anyone help?

Here is the LogCat:

08-07 08:24:02.516: ERROR/AndroidRuntime(334): FATAL EXCEPTION: main
08-07 08:24:02.516: ERROR/AndroidRuntime(334): java.lang.RuntimeException: Unable to start activity ComponentInfo{player.org/player.org.Player}: java.lang.NullPointerException
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.os.Handler.dispatchMessage(Handler.java:99)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.os.Looper.loop(Looper.java:123)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.app.ActivityThread.main(ActivityThread.java:3683)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at java.lang.reflect.Method.invokeNative(Native Method)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at java.lang.reflect.Method.invoke(Method.java:507)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at dalvik.system.NativeStart.main(Native Method)
08-07 08:24:02.516: ERROR/AndroidRuntime(334): Caused by: java.lang.NullPointerException
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at player.org.Player.onCreate(Player.java:26)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-07 08:24:02.516: ERROR/AndroidRuntime(334):     ... 11 more
08-07 08:24:02.536: WARN/ActivityManager(67):   Force finishing activity player.org/.Player
08-07 08:24:03.056: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{4054b008 player.org/.Player}
08-07 08:24:03.167: INFO/ActivityManager(67): Displayed com.android.launcher/com.android.launcher2.Launcher: +37s552ms
08-07 08:24:13.233: WARN/ActivityManager(67): Activity destroy timeout for HistoryRecord{4054b008 player.org/.Player}
pqn
  • 1,522
  • 3
  • 22
  • 33
Vahag Vardanyan
  • 301
  • 6
  • 13
  • 2
    please show stacktrace (from logcat) – MByD Aug 07 '11 at 14:02
  • Launch your application in the emulator, and take a look at the LogCat *(In Eclipse : Window > Show View > Other ... > Android > LogCat)*. When your application crashes, a stacktrace should be displayed in LogCat -- this should help you find out the cause of that crash. – Pascal MARTIN Aug 07 '11 at 14:02
  • I look at Log Chat, here little part of it – Vahag Vardanyan Aug 07 '11 at 14:06
  • 08-07 07:48:43.024: ERROR/AndroidRuntime(896): FATAL EXCEPTION: main 08-07 07:48:43.024: ERROR/AndroidRuntime(896): java.lang.RuntimeException: Unable to start activity ComponentInfo{player.org/player.org.Player}: java.lang.NullPointerException 08-07 07:48:43.024: ERROR/AndroidRuntime(896): Caused by: java.lang.NullPointerException 08-07 07:48:43.024: ERROR/AndroidRuntime(896): at player.org.Player.onCreate(Player.java:27) 08-07 07:48:43.024: ERROR/AndroidRuntime(896): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) – Vahag Vardanyan Aug 07 '11 at 14:06
  • 2
    "at player.org.Player.onCreate(Player.java:27" -- you haven't shown us that code. – Earl Aug 07 '11 at 14:13
  • It's easiest if you just edit your original post to include the entire logcat. That way the formatting is preserved. Also, your stack trace indicates that the error is on line 27 of your Player class. You have only posted 26 lines of that class. The error is on the next line, so please post the rest of your code so that we can help you. – theisenp Aug 07 '11 at 14:16
  • Please show us your complete code that you have implemented. – Lalit Poptani Aug 07 '11 at 14:20
  • Here is shown the all code. I just delete one comment line before posting quetion) – Vahag Vardanyan Aug 07 '11 at 14:21
  • Please post the rest of the code, where you use `media`. –  Aug 07 '11 at 14:27
  • "Here is shown the all code." -- Um, no. No it isn't. – Earl Aug 07 '11 at 14:28
  • I commented all other lines, and left only these,but app still force closeing. – Vahag Vardanyan Aug 07 '11 at 14:31
  • It's confusing because you named the class `Player`. –  Aug 07 '11 at 14:31
  • I think the problem at ProgressBar – Vahag Vardanyan Aug 07 '11 at 14:31
  • Look... you're not showing us the code, so do this: Run it. Let it fail. Double click the line in the logcat that says "at player.org.Player.onCreate" and it will take you to the line that's generating the error. Something on that line will be uninitialized. If you want help, you're going to have to show us that line and the surrounding lines of code. Understand that findViewbyId() can return a null... perhaps "progress" isn't being found. Progress certainly isn't being made here.... – Earl Aug 07 '11 at 14:34
  • 5
    I think that line is `progress.setVisibility(ProgressBar.INVISIBLE)`, so the problem is that `progress=(ProgressBar) findViewById(R.id.progress)` is not working, try Project->Clean and then run it again. –  Aug 07 '11 at 14:36
  • Yes the problem is that findViewById return null I add line progress=(ProgressBar) findViewById(R.id.progress); if(progress!=null) and the program works fine. but Why dinfViewById returns null, how van I fix it? progress.setVisibility(ProgressBar.INVISIBLE); – Vahag Vardanyan Aug 07 '11 at 15:05
  • I found that in R.id there is no id for Progressbar, but can't figure how to fix it? – Vahag Vardanyan Aug 07 '11 at 15:21
  • the advice of Alex Ady is helpful! thanks all for help) – Vahag Vardanyan Aug 07 '11 at 15:23

0 Answers0