无法访问android中的视图
Posted
技术标签:
【中文标题】无法访问android中的视图【英文标题】:Unable to access views in android 【发布时间】:2014-03-19 20:16:00 【问题描述】:我正在使用 metaio sdk。我试图在按下按钮时简单地切换两个图像视图的可见性,但它不起作用。
我的布局:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/ma_layout"
android:layout_
android:layout_
android:background="#00000000" >
-----
<ImageView
android:id="@+id/zoomIcon"
android:layout_
android:layout_
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_
android:layout_
android:layout_alignParentRight="true"
android:background="@drawable/scrollerbg"
android:visibility="invisible"/>
<ImageView
android:id="@+id/scroller"
android:layout_
android:layout_
android:layout_alignParentRight="true"
android:background="@drawable/scroller"
android:visibility="invisible"/>
---
</RelativeLayout>
我的代码:
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();
我从几个小时开始就尝试了多种方式,但它们都不起作用。请告诉我如何从 xml 访问视图。此代码以前可以使用,但现在无法使用。 Android 编程看起来真的很令人沮丧。
提前致谢
【问题讨论】:
按钮在哪里。你能把整个代码贴出来吗 嗨,我添加了按钮 发布整个代码。我看不到将膨胀的 xml 添加到活动的位置 添加了你好代码..请看..谢谢 【参考方案1】:你必须使用 setContent(R.layout.your_layout);在 onCreate 里面
像这样
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】:我猜错误在'R', 您需要做的就是导入您的包。R
例子
...
import com.yourpackage.yourappname.R; //Import this
@Override
public void onCreate(Bundle savedInstanceState)
MetaioCloudPlugin.startJunaio(null, getApplicationContext());
super.onCreate(savedInstanceState);
setContentView(R.layout.mainactivity);
...
那么它应该可以正常工作:D
【讨论】:
嗨,我已经有了那个导入语句..我使用的是 metaio sdk 而不是云插件..谢谢。还有更多建议 错误到底在哪一行?你可以通过logcat看到.. :D 任何地方都没有错误。 logcat 不显示任何内容。可见性切换不起作用 哦,然后贴出 main java 的每个代码以及您要具体隐藏哪两个图像? 抱歉回复晚了...无论如何都可以删除 mGuiView 并仅使用 findviewbyid 进行实验...【参考方案3】:我之前遇到过同样的问题。我设法通过清除缓存、数据并从设备上卸载应用程序来解决它。在我将应用程序重新发布到测试设备后它正在工作。也许你可以试试。
【讨论】:
以上是关于无法访问android中的视图的主要内容,如果未能解决你的问题,请参考以下文章
无法访问表视图单元格中 Struct 中的值。阅读下面截图中描述的代码
从木偶视图重新加载部分车把将无法访问为父视图中的部分模板元素定义的 ui 对象