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
My 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:
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...
CodePudding user response:
In MainActivity.java you should initialize your value into onCreate() method
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();
}
CodePudding user response:
Well, I found my own mistake. The thing what hossein answered earlier was right but the application was still crashing. Actually the problem was coming that I was triggering the methods hitLeft
and hitRight
which were not initialized as public
. That was the mistake and now it is working alright...