1

Ok I am not sure what I am doing wrong here. After looking up many directions and what not I came up with this snippet but I am not sure why it doesn't work. An explanation and help would be greatly appreciated. LogCat logs included.

activity_camera.xml:

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".CameraActivity" >



    <Button
        android:id="@+id/buttonMenu"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/capture"
        android:layout_alignBottom="@+id/capture"
        android:layout_alignParentLeft="true"
        android:layout_marginLeft="22dp"
        android:text="menu" />

    <Button
        android:id="@+id/capture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="24dp"
        android:onClick="onClick"
        android:text="Take pic" />


</RelativeLayout>

This is the snippet that gives me an error

protected void onCreate(Bundle savedInstanceState) {
        mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE); mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);
        super.onCreate(savedInstanceState);

        RelativeLayout RL = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_camera, null);
        mTextureView = new TextureView(this);
        mTextureView.setSurfaceTextureListener(this);
        RL.addView(mTextureView);
        setContentView(RL);

logcat:

E/AndroidRuntime(  784): FATAL EXCEPTION: main
E/AndroidRuntime(  784): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
E/AndroidRuntime(  784):    at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1723)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1670)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:382)
E/AndroidRuntime(  784):    at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:403)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
E/AndroidRuntime(  784):    at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5012)
E/AndroidRuntime(  784):    at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
E/AndroidRuntime(  784):    at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2189)
E/AndroidRuntime(  784):    at android.view.View.measure(View.java:15848)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1905)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1104)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1284)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1004)
E/AndroidRuntime(  784):    at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5481)
E/AndroidRuntime(  784):    at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
E/AndroidRuntime(  784):    at android.view.Choreographer.doCallbacks(Choreographer.java:562)
E/AndroidRuntime(  784):    at android.view.Choreographer.doFrame(Choreographer.java:532)
E/AndroidRuntime(  784):    at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
E/AndroidRuntime(  784):    at android.os.Handler.handleCallback(Handler.java:730)
E/AndroidRuntime(  784):    at android.os.Handler.dispatchMessage(Handler.java:92)
E/AndroidRuntime(  784):    at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(  784):    at android.app.ActivityThread.main(ActivityThread.java:5103)
E/AndroidRuntime(  784):    at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(  784):    at java.lang.reflect.Method.invoke(Method.java:525)
E/AndroidRuntime(  784):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
E/AndroidRuntime(  784):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
E/AndroidRuntime(  784):    at dalvik.system.NativeStart.main(Native Method)
dsuma
  • 1,000
  • 2
  • 9
  • 30

5 Answers5

1

Clean/Rebuild your project , it does not have any error. If problem still persists delete your R.java and build project again.

Jitender Dev
  • 6,907
  • 2
  • 24
  • 35
  • Whats is requirement exactly, May be i will modify your code and help you somehow. – Jitender Dev Oct 15 '13 at 10:29
  • My TextureView provides me with camera preview, Since I have it defined this way I need my buttons there to take the picture or go to menu which are defined in the xml. – dsuma Oct 15 '13 at 10:32
1

Change you onCreate code to:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_camera);

    mPreferences = getSharedPreferences("CurrentUser", MODE_PRIVATE);

    RelativeLayout RL = (RelativeLayout) getWindow().getDecorView().findViewById(android.R.id.content);
    mTextureView = new TextureView(this);
    mTextureView.setSurfaceTextureListener(this);
    RL.addView(mTextureView);
}

You need to call super.onCreate first. Then setContentView to your layout (which is the id = name of your file, not your relative layout). Then you can find your relative layout inside.

Szymon
  • 42,577
  • 16
  • 96
  • 114
  • Nowhere in my code, all in the android libraries. but im deducing it is this: `RelativeLayout RL = (RelativeLayout) findViewById(R.id.rel_layout);` – dsuma Oct 15 '13 at 10:39
  • Updated my answer, try again. – Szymon Oct 15 '13 at 10:44
  • `android.widget.FrameLayout cannot be cast to android.widget.RelativeLayout` Same thing. WHERE IS THIS FRAMELAYOUT – dsuma Oct 15 '13 at 10:47
  • No idea! Try different answers from this question: http://stackoverflow.com/questions/4486034/get-root-view-from-current-activity – Szymon Oct 15 '13 at 10:48
0

As the comments suggest it would be useful if you posted the xml layout that you are using. But the most likely solution seems that the top level element in the xml layout is NOT a RelativeLayout; thus the type cast (correctly) fails.

You don;t seem to require any functionality of the relative layout so I think you should cast to a more generic class (perhaps FrameLayout?). This would make you code more resilient to changes in the layout format files.

Elemental
  • 7,365
  • 2
  • 28
  • 33
0

Thank you All! So I fixed this this way in my activity_camera.xml i Changed from RelativeLayout to FrameLayout. So far so good. but thanks @Szymon His answer helped mine work.

dsuma
  • 1,000
  • 2
  • 9
  • 30
-1

open your xml file named activity_camera and make sure the layout is RelativeLayout. if not,you should change the layout in the xml file, or change this line RelativeLayout RL = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.activity_camera, null);RelativeLayout change to FrameLayout.

Kay
  • 1
  • 3