Hallo Ihr lieben. Ich habe mal wieder ein Problem, nachdem ich schon seit 2 Stunden im Internet am suchen bin und einfach keine Lösung finde. Kurzer Umriss: es handelt sich um eine BattleActivity.java die in ihrer .xml datei ein Framelayout enthält, indem eine von zwei Fragments angezeigt werden soll. Das Anzeigen läuft und scheinbar auch der Wechsel zwischen beiden. Nun, wenn die BattleActivity startet, soll zuerst BattleTextFragment geladen werden, zusammen mit einem von 3 Strings, der zufällig generiert wird. Wenn ich diese Funktion innerhalb des Fragments ablaufen lasse, funktioniert das auch prima, aber nun möchte ich (es ist ein Kampf Szenario) die zwei Fragments über die BattleActivity laufen lassen, dafür müsste ich nun die TextView innerhalb des BattleTextFragments durch BattleActivity verändern, indem ich eine Methode gerufen hätte, die man innerhalb des Fragments findet. Aber das scheitert und ich weiß nicht weiter.
[CODE lang="java" title="BattleActivity.java"]package com.manticore.monochromia;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
public class BattleActivity extends AppCompatActivity {
private Randomizer rnd = new Randomizer();
private Fragment frag1 = new BattleTextFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battle);
SetFragment(frag1);
Battle();
}
@Override
public void onBackPressed(){
super.onBackPressed();
this.finish();
Intent i = new Intent(this, DebugMenuActivity.class);
startActivity(i);
overridePendingTransition(0,0);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}
private void SetFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit();
}
public void Battle(){
BattleStart();
}
public void BattleStart(){
BattleTextFragment frag1 = (BattleTextFragment) getFragmentManager().findFragmentById(R.id.fragBtlTxt);
// bei dem oberen zeigt mir die Doku immer "Inconvertible types; cannot cast 'android.app.Fragment' to 'com.manticore.monochromia.BattleTextFragment'"
int ranNum = rnd.Randymizer(0, 3);
frag1.UpdateBattleText(ranNum);
}
}[/CODE]
[CODE lang="xml" title="activity_battle.xml"]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:weightSum="2"
androidrientation="horizontal"
android:background="@color/colorPrimary"
tools:context=".BattleActivity">
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/delete"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:scaleType="fitXY"
app:srcCompat="@drawable/mob_shellshock" />
</RelativeLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
androidrientation="vertical"
android:weightSum="6">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_centerInParent="true"
android:layout_toStartOf="@id/slash"
android:layout_marginRight="10dp"
android:text="53"/>
<TextView
android:id="@+id/slash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_centerInParent="true"
android:text="/"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_centerInParent="true"
android:layout_toEndOf="@id/slash"
android:layout_marginLeft="10dp"
android:text="100"/>
</RelativeLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>[/CODE]
[CODE lang="java" title="BattleTextFragment.java"]package com.manticore.monochromia;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class BattleTextFragment extends Fragment implements View.OnClickListener {
private Randomizer rnd = new Randomizer();
private BattleText btlTxt = new BattleText();
ImageView changeFragment;
public TextView battleText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_battle_text, container, false);
battleText = (TextView) view.findViewById(R.id.battleText);
changeFragment = (ImageView) view.findViewById(R.id.changeFragment);
changeFragment.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v){
//implement your things
Fragment battleActionFragment = new BattleActionFragment();
FragmentTransaction transaction= getFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, battleActionFragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void UpdateBattleText(int i){
battleText.setText(btlTxt.btlIntrTxt);
}
}[/CODE]
[CODE lang="xml" title="fragment_battle_text.xml"]<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/fragBtlTxt"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".BattleTextFragment">
<TextView
android:id="@+id/battleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:textAlignment="center"
android:textSize="25sp"
android:text="@string/battleTextFragment" />
<ImageView
android:id="@+id/changeFragment"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
app:srcCompat="@drawable/game_orb"/>
</RelativeLayout>[/CODE]
ich weiß nicht weiter, ich hatte so nen guten Lauf, und jetzt hänge ich mal wieder an einer so blöden Stelle. Ich wette, dass nur irgendwo wieder ein blödes Semicolon fehlt...
ich danke euch schon jetzt für euren Beistand
[CODE lang="java" title="BattleActivity.java"]package com.manticore.monochromia;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.content.Intent;
import android.os.Bundle;
public class BattleActivity extends AppCompatActivity {
private Randomizer rnd = new Randomizer();
private Fragment frag1 = new BattleTextFragment();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_battle);
SetFragment(frag1);
Battle();
}
@Override
public void onBackPressed(){
super.onBackPressed();
this.finish();
Intent i = new Intent(this, DebugMenuActivity.class);
startActivity(i);
overridePendingTransition(0,0);
i.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
}
private void SetFragment(Fragment fragment){
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragmentContainer, fragment);
fragmentTransaction.commit();
}
public void Battle(){
BattleStart();
}
public void BattleStart(){
BattleTextFragment frag1 = (BattleTextFragment) getFragmentManager().findFragmentById(R.id.fragBtlTxt);
// bei dem oberen zeigt mir die Doku immer "Inconvertible types; cannot cast 'android.app.Fragment' to 'com.manticore.monochromia.BattleTextFragment'"
int ranNum = rnd.Randymizer(0, 3);
frag1.UpdateBattleText(ranNum);
}
}[/CODE]
[CODE lang="xml" title="activity_battle.xml"]<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:weightSum="2"
androidrientation="horizontal"
android:background="@color/colorPrimary"
tools:context=".BattleActivity">
<RelativeLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent">
<ImageView
android:id="@+id/delete"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_centerInParent="true"
android:scaleType="fitXY"
app:srcCompat="@drawable/mob_shellshock" />
</RelativeLayout>
<LinearLayout
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent"
androidrientation="vertical"
android:weightSum="6">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_centerInParent="true"
android:layout_toStartOf="@id/slash"
android:layout_marginRight="10dp"
android:text="53"/>
<TextView
android:id="@+id/slash"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_centerInParent="true"
android:text="/"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:layout_centerInParent="true"
android:layout_toEndOf="@id/slash"
android:layout_marginLeft="10dp"
android:text="100"/>
</RelativeLayout>
<FrameLayout
android:id="@+id/fragmentContainer"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="0dp" />
</LinearLayout>
</LinearLayout>[/CODE]
[CODE lang="java" title="BattleTextFragment.java"]package com.manticore.monochromia;
import android.content.Intent;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
public class BattleTextFragment extends Fragment implements View.OnClickListener {
private Randomizer rnd = new Randomizer();
private BattleText btlTxt = new BattleText();
ImageView changeFragment;
public TextView battleText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_battle_text, container, false);
battleText = (TextView) view.findViewById(R.id.battleText);
changeFragment = (ImageView) view.findViewById(R.id.changeFragment);
changeFragment.setOnClickListener(this);
return view;
}
@Override
public void onClick(View v){
//implement your things
Fragment battleActionFragment = new BattleActionFragment();
FragmentTransaction transaction= getFragmentManager().beginTransaction();
transaction.replace(R.id.fragmentContainer, battleActionFragment);
transaction.addToBackStack(null);
transaction.commit();
}
public void UpdateBattleText(int i){
battleText.setText(btlTxt.btlIntrTxt);
}
}[/CODE]
[CODE lang="xml" title="fragment_battle_text.xml"]<?xml version="1.0" encoding="utf-8"?>
<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:id="@+id/fragBtlTxt"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context=".BattleTextFragment">
<TextView
android:id="@+id/battleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_margin="10dp"
android:textAlignment="center"
android:textSize="25sp"
android:text="@string/battleTextFragment" />
<ImageView
android:id="@+id/changeFragment"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="10dp"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
app:srcCompat="@drawable/game_orb"/>
</RelativeLayout>[/CODE]
ich weiß nicht weiter, ich hatte so nen guten Lauf, und jetzt hänge ich mal wieder an einer so blöden Stelle. Ich wette, dass nur irgendwo wieder ein blödes Semicolon fehlt...
ich danke euch schon jetzt für euren Beistand