尝试使用 wowza 在空对象引用 android studio 上调用虚拟方法

attempt to invoke virtual method on a null object reference android studio using wowza

我正在尝试使用 wowza 开始广播。我正在使用一个示例应用程序,当我编译时它可以完美运行。

然而,当我尝试在我自己的项目中使用它时,当我尝试开始广播时出现此错误:

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.caper.teststream, PID: 26004
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.wowza.gocoder.sdk.api.broadcast.WOWZBroadcastConfig.setStreamName(java.lang.String)' on a null object reference
    at com.caper.teststream.WowzaCamera.start(WowzaCamera.java:80)
    at com.caper.teststream.BroadcastActivity.onClick(BroadcastActivity.java:59)
    at android.view.View.performClick(View.java:6891)
    at android.widget.TextView.performClick(TextView.java:12651)
    at android.view.View$PerformClick.run(View.java:26083)
    at android.os.Handler.handleCallback(Handler.java:789)
    at android.os.Handler.dispatchMessage(Handler.java:98)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6938)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)

而且相机在预览中不显示

这是我的代码

public class BroadcastActivity extends AppCompatActivity
    implements  View.OnClickListener {

WOWZCameraView mWowzaPreview;
WowzaCamera mWowza = null;
boolean mIsBroadcastStarted = false;

private boolean mPermissionsGranted = true;
private static int mPermissionCount = 0;
private String[] mRequiredPermissions = new String[]{
        Manifest.permission.CAMERA,
        Manifest.permission.RECORD_AUDIO,
        Manifest.permission.READ_EXTERNAL_STORAGE
};
private static final int PERMISSIONS_REQUEST_WOWZA = 367;

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

    // Don't turn off screen while in this activity
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

    mWowzaPreview =  findViewById(R.id.camera_preview);

    Button broadcastBtn = findViewById(R.id.broadcast_button);
    broadcastBtn.setOnClickListener(this);


}


@Override
public void onClick(View view) {
    Button btn = (Button)view;

    if(!mIsBroadcastStarted)
    {

        // Name of stream is assigned here
        mWowza.start("myStream2");
        mIsBroadcastStarted = true;
        btn.setText("STOP");
    } else {
        mWowza.stop();
        btn.setText("START");
        mIsBroadcastStarted = false;
    }



}

@Override
protected void onResume() {
    super.onResume();

    //Check appropriate permissions
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        mPermissionsGranted = hasPermissions(this, mRequiredPermissions);
        if (!mPermissionsGranted)
            ActivityCompat.requestPermissions(this, mRequiredPermissions, PERMISSIONS_REQUEST_WOWZA);
        else {
            initializeWowzaCamera();
        }
    } else {
        mPermissionsGranted = true;
        initializeWowzaCamera();
    }
}

private boolean isInit = false;

private void initializeWowzaCamera(){
    if (!isInit) {
        mWowza = new WowzaCamera(BroadcastActivity.this, mWowzaPreview);
        isInit = true;

    }
    if (mWowza != null) {
        mWowza.onResume();
        mWowzaPreview.onResume();

    }
}

private static boolean hasPermissions(Context context, String[] permissions) {
    for (String permission : permissions)
        if (context.checkCallingOrSelfPermission(permission) != PackageManager.PERMISSION_GRANTED)
            return false;

    return true;
}


@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case PERMISSIONS_REQUEST_WOWZA: {
            // If request is cancelled, the result arrays are empty.
            // Check the result of each permission granted

            mPermissionCount++;

            for (int grantResult : grantResults) {
                if (grantResult == PackageManager.PERMISSION_DENIED) {
                    mPermissionsGranted = false;
                }
            }

            if (mPermissionsGranted && mPermissionCount == 3) {
                initializeWowzaCamera();
            }


            if (!mPermissionsGranted && mPermissionCount == 3) {
                mPermissionCount = 0;
                finish();
            }

            return;
        }
    }
}


@Override
protected void onPause() {
    if(mWowza!=null)
    mWowza.onPause();
    super.onPause();
}


@Override
protected void onDestroy() {
    mWowza.stop();
    super.onDestroy();
}
}

请帮忙

我认为您在检查是否已授予权限时没有调用 initializeWowzaCamera()。你是什​​么意思?

if (mPermissionsGranted && grantResults.length == 3) {
    initializeWowzaCamera();
}