Unable to access views in android

2019-09-01 08:34发布

问题:

I am using metaio sdk. I am trying to simply toggle the visibility of two imageviews when pressing a button but it is not working.

My layout:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/ma_layout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#00000000" >
-----

<ImageView
    android:id="@+id/zoomIcon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_marginTop="4dp"
    android:layout_marginRight="4dp"
    android:background="@drawable/zooming"
    android:onClick="seeZoom" />


    <ImageView
        android:id="@+id/scrollerBg"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/scrollerbg"
        android:visibility="invisible"/>

    <ImageView
        android:id="@+id/scroller"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:background="@drawable/scroller"  
        android:visibility="invisible"/>
---
</RelativeLayout>

My code:

import android.view.MotionEvent;
import java.util.List;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.MeasureSpec;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;

import com.metaio.sdk.jni.IGeometry;
import com.metaio.sdk.jni.IMetaioSDKAndroid;
import com.metaio.sdk.jni.Vector3d;

import android.os.Environment;
import com.metaio.sdk.ARViewActivity;
import com.metaio.sdk.jni.EPLAYBACK_STATUS;
import com.metaio.sdk.jni.IMetaioSDKCallback;
import com.metaio.sdk.jni.MovieTextureStatus;
import com.metaio.sdk.jni.Rotation;
import com.metaio.sdk.MetaioDebug;



import com.company.abc.R;

public class MainActivity extends ARViewActivity 
{

    public RelativeLayout mGUIView;
    //public ImageView imgView1;
    //public ImageView imgView2;
    Camera camera;
    private IGeometry tdp1, tdp2, tdp3, tdp4, tdp5, tdp6, tdp7, sal1, sal2;
    boolean isTorchOn=false;
    Parameters camParams;
    public ImageView basePng;
    public ImageView zoomIcon;
    ImageView scroller;
    public LayoutParams scrollerParams;
    ImageView scrollerBg;
    public LayoutParams scrollerBgParams;
    boolean afc;
    int counter;
    int displayWidthbyTwo;
    int displayHeightbyTwo;
    int scrollerW;
    int scrollerH;
    int scrollerBgW;
    int scrollerBgH;
    int ZoomValue=0;
    int maxZoomLevel;
    int maxZoombyfour;
    boolean zoomSupported;
    boolean isZoomBarVisible=false;



    private MetaioSDKCallbackHandler mCallbackHandler;

    @Override
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);



        MetaioDebug.enableLogging(true);

        mCallbackHandler = new MetaioSDKCallbackHandler();

        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);
        displayWidthbyTwo=metrics.widthPixels/2;
        displayHeightbyTwo=metrics.heightPixels/2;

        PackageManager PM= this.getPackageManager();
        afc = PM.hasSystemFeature(PackageManager.FEATURE_CAMERA_AUTOFOCUS);

        setContentView(R.layout.mainactivity);

        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
            mGUIView = (RelativeLayout) getLayoutInflater().inflate(R.layout.mainactivity, null);


        zoomIcon = (ImageView) mGUIView.findViewById(R.id.zoomIcon);

        scroller = (ImageView) mGUIView.findViewById(R.id.scroller);
        scrollerBg = (ImageView) mGUIView.findViewById(R.id.scrollerBg);

        scroller.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        scrollerW = scroller.getMeasuredWidth()/2;
        scrollerH = scroller.getMeasuredHeight()/2;
        scrollerParams = (LayoutParams) scroller.getLayoutParams();
        scrollerParams.topMargin = displayHeightbyTwo-150-scrollerH;
        scroller.setLayoutParams(scrollerParams);


        scrollerBg.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
        scrollerBgW = scrollerBg.getMeasuredWidth()/2;
        scrollerBgH = scrollerBg.getMeasuredHeight()/2;
        scrollerBgParams = (LayoutParams) scrollerBg.getLayoutParams();
        scrollerBgParams.topMargin = displayHeightbyTwo-scrollerBgH;
        scrollerBg.setLayoutParams(scrollerBgParams);



        scroller.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                LayoutParams layoutParams = (LayoutParams) scroller.getLayoutParams();
                switch(event.getAction())
                {
                case MotionEvent.ACTION_MOVE:
                    //Get y coord of the touch point relative to screen
                    int y_cord = (int) event.getRawY();

                    //Restrict scroller bewtween center of screen +- 150
                    if(y_cord>(displayHeightbyTwo+150))
                        y_cord = displayHeightbyTwo+150;
                    if(y_cord<(displayHeightbyTwo-150))
                        y_cord = displayHeightbyTwo-150;

                    //Set zoom levels at various steps
                    if(y_cord<=displayHeightbyTwo-90){
                        camParams.setZoom(0);                   
                        Log.i("Zooming:","0");
                        ZoomValue=0;}
                    if(y_cord<=displayHeightbyTwo-30 && y_cord>displayHeightbyTwo-90){
                        camParams.setZoom(maxZoombyfour);
                        Log.i("Zooming:","1");
                        ZoomValue=1;}
                    if(y_cord<=displayHeightbyTwo+30 && y_cord>displayHeightbyTwo-30){
                        camParams.setZoom(maxZoombyfour*2);
                        Log.i("Zooming:","2");
                        ZoomValue=2;}
                    if(y_cord<=displayHeightbyTwo+90 && y_cord>displayHeightbyTwo+30){
                        camParams.setZoom(maxZoombyfour*3);
                        Log.i("Zooming:","3");
                        ZoomValue=3;}
                    if(y_cord>displayHeightbyTwo+90){
                        camParams.setZoom(maxZoomLevel);
                        Log.i("Zooming:","4");
                        ZoomValue=4;}
                    camera.setParameters(camParams);
                    scrollerParams.topMargin = y_cord-scrollerH;
                    scroller.setLayoutParams(scrollerParams);
                    break;

                default:
                    break;
                }
                return true;
            }
        });


    }

    @Override
    protected int getGUILayout() 
    {
        // TODO: return 0 in case of no GUI overlay
        return R.layout.mainactivity; 
    }


    @Override
    protected void onStart() 
    {
        super.onStart();
        // hide GUI until SDK is ready
        //if (!mRendererInitialized)
            //mGUIView.setVisibility(View.GONE);
        // add GUI layout
        addContentView(mGUIView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));


    }   

    @Override
    protected void loadContents() 
    {
        // Load desired tracking data for planar marker tracking
        String Path = "storage/sdcard1/AS/tdp/";
        //final String trackingConfigFile = AssetsManager.getAssetPath("Tracking.xml");
        boolean result = metaioSDK.setTrackingConfiguration(Path+"Tracking.xml"); 

        String movie1Path = Path + "movie1.3gp";
        tdp1 = metaioSDK.createGeometryFromMovie(movie1Path, false) ;
        tdp1.setCoordinateSystemID(1);
        tdp1.setScale(new Vector3d(4.0f,4.0f,4.0f));
        tdp1.startMovieTexture(true); // loop = true;

    }


    @Override
    protected void onGeometryTouched(IGeometry geometry) {
        // TODO Auto-generated method stub
    }


    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);
    }



    @Override
    public void onSurfaceChanged(int width, int height) {
        //always call the super implementation first
        super.onSurfaceChanged(width, height);
        camera=IMetaioSDKAndroid.getCamera(this);
        camParams = camera.getParameters();
        if(afc){
            List<String> focusModes = camParams.getSupportedFocusModes();
            if(focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE))
                camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
            else if (focusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
                camParams.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
            camera.setParameters(camParams);
        }

        if(camParams.isZoomSupported()){
            this.runOnUiThread(new Runnable(){
                            public void run(){
                                zoomIcon.setVisibility(View.VISIBLE);
                            }
                        });
            maxZoomLevel = camParams.getMaxZoom();
            maxZoombyfour = Math.round(maxZoomLevel/4);
            zoomSupported=true;
         }

    }

    @Override
    protected IMetaioSDKCallback getMetaioSDKCallbackHandler() 
    {
        return mCallbackHandler;
    }

    final class MetaioSDKCallbackHandler extends IMetaioSDKCallback 
    {

    }

    public void showTorch(View v) {
        if(isTorchOn){
            IMetaioSDKAndroid.stopTorch(this);
            isTorchOn=false;
        }
        else{
            IMetaioSDKAndroid.startTorch(this);
            isTorchOn=true;
        }

    }

    public void seeZoom(View v) {
        if(isZoomBarVisible){
            scroller.setVisibility(0);
            //scrollerBg.setVisibility(View.INVISIBLE);
            isZoomBarVisible=false;
            Log.i("ss","ss");
        }
        else{
            scroller.setVisibility(1);
            isZoomBarVisible=true;
            Log.i("ss","ss");
        }

    }
    public void seeSettings(View v) {
        Intent intent = new Intent(this, Settings.class);
        startActivity(intent);
        //finish();
    }

    public void seeCatalog(View v) {
        Intent intent = new Intent(this, CatalogueActivity.class);
        startActivity(intent);
        //finish();
    }

}

I am trying in multiple ways since hours but none of them work . plesae tell me how to access the views from xml. This code used to work previously but it is not working now. Android programming really seems frustrating.

Thanks in advance

回答1:

You must use setContent(R.layout.your_layout); inside onCreate

Like this

public void onCreate(Bundle savedInstanceState) 
{
        super.onCreate(savedInstanceState);
        //mGUIView = (RelativeLayout) getLayoutInflater().inflate(R.layout.scan, null);
        setContent(R.layout.your_layout);
        scroller = (ImageView) mGUIView.findViewById(R.id.scroller);
        scrollerBg = (ImageView) mGUIView.findViewById(R.id.scrollerBg);
}


回答2:

I guess the error is at 'R', All you need to do is import your package.R

Example

...    
import com.yourpackage.yourappname.R; //Import this

@Override
public void onCreate(Bundle savedInstanceState) {
    MetaioCloudPlugin.startJunaio(null, getApplicationContext());
    super.onCreate(savedInstanceState);
    setContentView(R.layout.mainactivity);
...

Then it should work fine :D



回答3:

I encountered the same issue before. I managed to solve it by clearing the cache, data and uninstall the app from the device. It is working after I re-publish the app to the testing device. Maybe you could try it.