【发布时间】:2015-03-14 07:23:20
【问题描述】:
我对 android 开发还很陌生,因此非常感谢任何帮助。
我有一个应用程序,它使用光传感器读取光量并将其显示在屏幕上。我正在android studio上开发它。在标题下,是一个textView,是一个imageView,后面是两个文本视图。无论我尝试哪种布局,当我有一个 imageView 时,应用程序都会崩溃。删除 imageView 后,该应用程序将毫无问题地运行。我的图像在可绘制包中(希望这是正确的术语)并且是 JPEG(根据文档是受支持的文件类型)。
我还知道我的 textView 项目之一设置为等于“Light”,而不是指向字符串。我知道这不是我应该这样做的方式,我仍在开发应用程序。
请帮我弄清楚我做错了什么。感谢所有的帮助和时间!
<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
android:background="#ffffffff">
<TextView android:text="@string/title" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textIsSelectable="false"
android:textSize="@dimen/abc_text_size_headline_material"
android:textColor="#ff2db81a"
android:id="@+id/textView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/light_available"
android:text="Sensor"
android:layout_marginBottom="35dp"
android:layout_above="@+id/light_reading"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/light_reading"
android:text="Light"
android:layout_marginBottom="43dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="300dp"
android:layout_height="300dp"
android:id="@+id/imageView"
android:layout_above="@+id/light_available"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:src="@drawable/homeplant" />
这里是 Java...
public class MainActivity extends ActionBarActivity {
TextView textLightIsAvailable, textLightReading;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textLightIsAvailable = (TextView)findViewById(R.id.light_available);
textLightReading = (TextView)findViewById(R.id.light_reading);
SensorManager mySensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
Sensor LightSensor = mySensorManager.getDefaultSensor(Sensor.TYPE_LIGHT);
if(LightSensor != null){
textLightIsAvailable.setText("You Have a Sensor!");
mySensorManager.registerListener(LightSensorListener, LightSensor, SensorManager.SENSOR_DELAY_NORMAL);
}
else{
textLightIsAvailable.setText("You Don't Have a Sensor!");
}
}
private final SensorEventListener LightSensorListener= new SensorEventListener(){
@Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
@Override
public void onSensorChanged(SensorEvent event) {
if(event.sensor.getType() == Sensor.TYPE_LIGHT){
textLightReading.setText("LIGHT: " + event.values[0]);
}
}
};
更新:2015 年 3 月 10 日:我也尝试了两个不同的虚拟设备,但出现与我的物理设备相同的错误。 logcat 只显示(我取出了时间戳/mypackage 名称,因为它看起来在这里的格式)...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ waiting for debugger to settle...
I/System.out﹕ debugger has settled (1433)
再次感谢。
编辑:2015 年 3 月 12 日
我的 JRE 版本是 - 1.6.0_65-B14-466.1 11M4716 x86_64 Android Studio 版本 - 1.1.0
【问题讨论】:
-
尝试项目 -> 干净,有时资源无法正确构建
-
我已经重新清理/重建了项目。我的 Logcat 没有显示任何内容,除了“等待调试器解决...”几次,然后是“com.yourfuturehero.lightsplantgrow D/dalvikvm:threadid=1:撤消后仍然挂起(sc=1 dc=1)”。我也在 Galaxy S4 上运行它而不是使用虚拟设备。
-
如果你的 imageview 是
centerInParent有什么需要使用layout_above? -
@Gaurav 我使用 GUI 来构建布局,这就是 Android Studio 为我提供的。我认为这是因为我有一个 RelativeLayout 并且我添加了中心部分。但是,即使我只使用其中一种,应用程序在启动时仍然会崩溃。
标签: android android-layout crash imageview runtime-error