0

Inside one of my java files I have to use this new instance of a TextView in two separate methods, so rather than creating the new instances of the TextView in both methods I thought I would just declare it outside of the methods although still inside the class.

I didn't receive any errors before compiling however when I tried to get to the activity this class is concerned with the app crashes. I fixed this by putting the declaration in both separate methods and the app stopped crashing I was just wondering why and would there be a more eloquent way of doing it as I'm trying to stick to the DRY principle.

Here's the line of code:

final TextView tv_scoreDisplay = (TextView) findViewById(R.id.tv_scoreDisplay);

Logcat:

10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: FATAL EXCEPTION: main 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: Process: com.example.aaron.dartpro, PID: 9474 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.aaron.dartpro/com.example.aaron.dartpro.FiveZeroOne}: java.lang.NullPointerException 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: Caused by: java.lang.NullPointerException 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.Activity.findViewById(Activity.java:1884) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.example.aaron.dartpro.FiveZeroOne.(FiveZeroOne.java:26) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.Class.newInstanceImpl(Native Method) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.Class.newInstance(Class.java:1208) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101) 10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.access$800(ActivityThread.java:135)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:102)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.os.Looper.loop(Looper.java:136)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5001)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:515)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)  10-21 16:05:18.150 9474-9474/com.example.aaron.dartpro E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method) 

COYG
  • 1,538
  • 1
  • 16
  • 31

4 Answers4

1

It was crashing because you need to call findViewByID after setContentView() method si called, if tv_scoreDisplay is a field variable, you cannot declare it final.

Ariel Carbonaro
  • 1,529
  • 1
  • 13
  • 25
1

You can declare the TextView as a member attribute of your activity but you need to initialize it in the onCreateView and you need to first inflate your layout with setContentView(R.layout.your_layout) for instance.

public class YourActivity{
   private TextView tv_scoreDisplay;

   public void onCreate(){
      setContentView(R.layout.your_layout);
      tv_scoreDisplay = (TextView) findViewById(R.id.tv_scoreDisplay);
   }
}
1

findViewById(R.id.tv_scoreDisplay); could be the issue.

You load an activity and then call setContentView() this loads the view now all the R.id will be available from the loaded xml any call prior to setContentView() referring to R.id will crash the app. Also see if that id exists in the same xml you loaded ? These happens during runtime because during compilation syntax etc.. are checked but the allocation is determined only during runtime... there arises situation when we want to get the height of view and it will return 0 this happens because the view is not yet painted on the screen so all these things and factors occur during runtime...

thanks

Ahmad
  • 437
  • 1
  • 4
  • 12
0

My guess is you're getting a null reference error at run time, which probably means you're not getting the view reference at the correct time and storing globally. You want to make sure you view isn't null, which can be ensured by grabbing the view @ onCreate() AFTER setting the content view So..

public myActivity extends Activity{
  View myView; 


  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_layout);
    myView = (View)findViewById(R.id.myview);
  }

  public void fnc1(){//myView is accessible from here
  }
  public void fnc2(){//and from here
  }
 }
em_
  • 2,134
  • 2
  • 24
  • 39