Ошибки с виджетами в AndroidStudio
Изза этой ошибки все виджеты смещаются на координаты 0; 0;.
также все виджеты прикреплены между особой.
activity main в текстовой версии
package com.exemple.guessinggame;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.widget.EditText;
import android.widget.Button;
import android.widget.TextView;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
private EditText txtGuess;
private Button btnGuess;
private TextView lblOutput;
private int theNumber;
public void newGame()
{
theNumber = (int)(Math.random() * 10);
}
public void checkGuess()
{
String guessText = lblOutput.getText().toString();
int guess = Integer.parseInt(guessText);
String message = "";
try
{
if ( guess > theNumber )
{
message = guess + " is to high, try again ";
}
else if ( guess < theNumber )
{
message = guess + " is too low, try again ";
}
else
{
message = guess + " is true, u Win ";
newGame();
}
}
catch (Exception e)
{
message = " is shiet ";
}
finally
{
lblOutput.setText(message);
txtGuess.requestFocus();
txtGuess.selectAll();
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtGuess = (EditText) findViewById(R.id.txtGuess);
btnGuess = (Button) findViewById(R.id.btnGuess);
lblOutput = (TextView) findViewById(R.id.lblOutput);
newGame();
btnGuess.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v)
{
checkGuess();
}
});
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
а также content main в текстовой версии
<?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"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<TextView
android:id="@+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="56dp"
android:text="It Is Goha's Program"
android:textColor="#000000"
app:layout_constraintBottom_toTopOf="@+id/lblOutput"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/lblOutput"
android:layout_width="224dp"
android:layout_height="0dp"
android:layout_marginBottom="42dp"
android:text="Enter a number between 1 and 10"
android:textColor="#000000"
app:layout_constraintBottom_toTopOf="@+id/txtGuess"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/textView" />
<Button
android:id="@+id/btnGuess"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:text="Button"
app:layout_constraintStart_toStartOf="@+id/txtGuess"
app:layout_constraintTop_toBottomOf="@+id/txtGuess" />
<EditText
android:id="@+id/txtGuess"
android:layout_width="90dp"
android:layout_height="0dp"
android:layout_marginBottom="135dp"
android:autofillHints=""
android:ems="10"
android:inputType="number"
app:layout_constraintBottom_toTopOf="@+id/textView3"
app:layout_constraintEnd_toEndOf="@+id/textView3"
app:layout_constraintHorizontal_bias="0.888"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/lblOutput" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="336dp"
android:text="And press on button"
android:textColor="#000000"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/txtGuess" />
</androidx.constraintlayout.widget.ConstraintLayout>
При открытии эмулятора, вылазят такие ошибки :
и вот такие :
И еще раз, проблема заключается в том, что хотя виджеты прикреплены по вертикали и горизонтали, они все ровно смещаются на исходную позицию.
Извините за мою непрофессиональность)
Ответы (1 шт):
Автор решения: Alex_Skvortsov
→ Ссылка
У Вас в коде используется файл разметки activity_main.xml setContentView(R.layout.activity_main);.
А в вопросе Вы приложили файл content_main.xml. И именно его, как я понял, Вы редактируете. Вызывайте его из кода и все заработает!

