应用程序在尝试获取我的相机ID时崩溃

时间:2017-12-17 09:47:17

标签: java android

因此,我对Android相对较新,并尝试构建相机应用程序。但我已经抓住了相机ID。

我有我的公共CameraManager,一个公共String数组和一个公共TextView,我只是试图从我的CameraManager获取CameraID并在我的textview中显示我的第一个ID。应用程序构建没有错误,但一旦我尝试在我的手机上执行它,它只是崩溃。我的错误在哪里?

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

在我的onCreate函数中:

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

并且,根据要求,我手机上的错误日志:

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

行ThrowActivity.java:26

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

1 个答案:

答案 0 :(得分:0)

您需要初始化Activity onCreate方法中的视图。在调用setContentView之前,您无法通过id查找视图。

所以替换这个:

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

使用:

public TextView text;

//....


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

   text = (TextView)findViewById(R.id.text);
   //...
相关问题