简体   繁体   English

在 android 工作室中更改 imageview 的坐标时出错

[英]Getting error in changing co-ordinates of imageview in android studio

I am a beginner to android. I want to slide a slidebar in a horizontal linear layout but when I run the code, the application ends giving error FallingBall keeps stopping我是 android 的初学者。我想在水平线性布局中滑动滑动条,但是当我运行代码时,应用程序结束时给出错误FallingBall keeps stopping

My xml:我的xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">


<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="0dp"
    android:layout_height="0dp"
    android:orientation="vertical"
    android:weightSum="100"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="72"
        android:orientation="horizontal" >

        <TextView
            android:id="@+id/textView2"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:text="Hello" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="8"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/slideBar"
            android:layout_width="100dp"
            android:layout_height="match_parent"
            app:srcCompat="@android:drawable/bottom_bar" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="20"
        android:orientation="horizontal">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <ImageButton
                android:id="@+id/leftBut"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@android:color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintStart_toStartOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/left_but"
                android:onClick="hitLeft"/>

            <ImageButton
                android:id="@+id/rightBut"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:background="@android:color/white"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintEnd_toEndOf="parent"
                app:layout_constraintTop_toTopOf="parent"
                app:srcCompat="@drawable/right_but"
                android:onClick="hitRight"/>
        </androidx.constraintlayout.widget.ConstraintLayout>

    </LinearLayout>
</LinearLayout>

</androidx.constraintlayout.widget.ConstraintLayout>

My java file is like this:我的 java 文件是这样的:

    package com.example.fallingball;

import androidx.appcompat.app.AppCompatActivity;

import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

ImageView s = findViewById(R.id.slideBar);
float x = s.getX();
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
}


void hitLeft(View view){
    x--;
    s.setX(30);
}

void hitRight(View view){
    x++;
    s.setX(40);
}
}

Note: leftBut and rightBut are two Image button and I have copied the images to the drawable folder so if you are trying this code on your system, make sure you adjust this else it will show you Image not found or so...注意: leftButrightBut是两个图像按钮,我已将图像复制到 drawable 文件夹中,因此如果您在系统上尝试此代码,请确保调整此代码,否则它会显示图像未找到等...

In MainActivity.java you should initialize your value into onCreate() method在 MainActivity.java 你应该将你的值初始化为 onCreate() 方法

ImageView s;
float x;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
    s = findViewById(R.id.slideBar);
    x = s.getX();
}

Well, I found my own mistake.好吧,我发现了自己的错误。 The thing what hossein answered earlier was right but the application was still crashing. hossein 之前回答的事情是正确的,但应用程序仍然崩溃。 Actually the problem was coming that I was triggering the methods hitLeft and hitRight which were not initialized as public .实际上问题来了,我正在触发未初始化为publichitLefthitRight方法。 That was the mistake and now it is working alright...那是个错误,现在它工作正常......

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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