在 android studio 中更改图像视图的坐标时出错

Getting error in changing co-ordinates of imageview in android studio

我是 android 的初学者。我想在水平线性布局中滑动滑动条,但是当我 运行 代码时,应用程序结束时给出错误 FallingBall keeps stopping

我的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>

我的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);
}
}

注意:leftButrightBut 是两个图像按钮,我已将图像复制到 drawable 文件夹中,因此如果您在系统上尝试使用此代码,请务必调整它,否则它将向您显示未找到图片...

在 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();
}

好吧,我发现了自己的错误。 hossein 之前回答的事情是正确的,但应用程序仍然崩溃。实际上问题来了,我正在触发未初始化为 public 的方法 hitLefthitRight。那是个错误,现在它工作正常...