0

So I'm relatively new to Android and trying to build a Camera App. But I'm allready stuck with catching the Camera IDs.

I have my public CameraManager, a public String array and a public TextView and I was just trying to get the CameraIDs from my CameraManager and display the first of my IDs in my textview. And the App builds without errors but as soon as I try to execute it on my phone, it just crashes. Where is my mistake?

public CameraManager mCamera;
public String[] cameraIDs;
public TextView text = (TextView)findViewById(R.id.text);

And in my onCreate function:

try {
    cameraIDs = mCamera.getCameraIdlist();
    text.setText(cameraIDs[0]);
} catch (CameraAccessExeption e) {
    text.setText("Sorry, couldn't find camera device.");
}

And, as requested, the error log from my phone:

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.test/com.example.test.ThrowActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2509)
at android.app.ActivityThread.access$1000(ActivityThread.java:153)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1373)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:5527)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:739)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:629)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.Window$Callback android.view.Window.getCallback()' on a null object reference
at android.support.v7.app.AppCompatDelegateImplBase.<init>(AppCompatDelegateImplBase.java:117)
at android.support.v7.app.AppCompatDelegateImplV9.<init>(AppCompatDelegateImplV9.java:149)
at android.support.v7.app.AppCompatDelegateImplV11.<init>(AppCompatDelegateImplV11.java:29)
at android.support.v7.app.AppCompatDelegateImplV14.<init>(AppCompatDelegateImplV14.java:54)
at android.support.v7.app.AppCompatDelegateImplV23.<init>(AppCompatDelegateImplV23.java:31)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:200)
at android.support.v7.app.AppCompatDelegate.create(AppCompatDelegate.java:183)
at android.support.v7.app.AppCompatActivity.getDelegate(AppCompatActivity.java:519)
at android.support.v7.app.AppCompatActivity.findViewById(AppCompatActivity.java:190)
at com.example.test.ThrowActivity.<init>(ThrowActivity.java:26)
at java.lang.Class.newInstance(Native Method)
at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2350)
... 9 more

line ThrowActivity.java:26

public TextView text = (TextView)findViewById(R.id.text);
donfuxx
  • 11,277
  • 6
  • 44
  • 76
Markus Vogel
  • 65
  • 1
  • 8

1 Answers1

0

you need to initialise views inside the Activity onCreate method. Before setContentView is called you cannot look up views by id.

So replace this:

public TextView text = (TextView)findViewById(R.id.text);

with:

public TextView text;

//....


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

   text = (TextView)findViewById(R.id.text);
   //...
donfuxx
  • 11,277
  • 6
  • 44
  • 76