简体   繁体   中英

Android findViewById always returns null

Currently, I am trying to build an streaming app using Android NDK and Live555. But my current problem is much more confusing. I've created a CustomPreviewClass to show the actual camera picture. But unfortunatly, when I try to access the view the method findViewById() always returns null. Why? Does anybody have an idea?

Here's my Activity:

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

    preview = (CameraPreview) findViewById(R.id.surfaceView1); // returns null
    if (preview == null) Log.d("Test", "Preview is null");

    final Button buttonStart = (Button) findViewById(R.id.buttonStart);
    buttonStart.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            try {
                startRecording();
            } catch (IOException e) {
                e.printStackTrace();
            }
            buttonStart.setEnabled(false);
        }
    });

    final Button buttonStop = (Button) findViewById(R.id.buttonStop);
    buttonStop.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            stopRecording();
            buttonStart.setEnabled(true);
        }
    });
}

Just a side note: the findViewById() for the buttons works fine...

The activity_main.xml:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="de.douglasmedia.LiveCam.MainActivity" >

<de.douglasmedia.LiveCam.views.CameraPreview
    android:id="@+id/surfaceView1"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
</de.douglasmedia.LiveCam.views.CameraPreview>

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <Button
        android:id="@+id/buttonStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Start" >
    </Button>

    <Button
        android:id="@+id/buttonStop"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Stop" >
    </Button>
</LinearLayout>

The CameraPreview:

...
public CameraPreview(Context context, AttributeSet attrs) {
    this(context);
}

public CameraPreview(Context context) {
    super(context);
    SurfaceHolder holder = getHolder();
    holder.addCallback(this);
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    synchronized (surfaceCreationSync) {
        surfaceCreatedHolder = holder;
        surfaceCreationSync.notifyAll();
    }
}
...

Thanks for your help!

I've solved this problem for my own. The problem was, that the I missed the constructor with 3 arguments in the CameraPreview class. So, I added this constructor and everything works fine...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM