Hallo Zusammen,
ich versuche gerade meine kleine App auf Fragmente umzustellen, damit ich "Settings" entsprechend einbauen kann.
Jetzt stürzt meine App leider immer beim starten ab und ich vermute einen Fehler in den Layout-Definitionen.
hier die Dateien
activity_main.xml
Code
<?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">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="@layout/content_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
Alles anzeigen
Content_main.xml
Code
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
Alles anzeigen
fragment_main.xml
Code
<?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=".MainFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<org.osmdroid.views.MapView
android:id="@+id/mapView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tilesource="Mapnik" >
</org.osmdroid.views.MapView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="@+id/cmdCenterAuto"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/caricon"
android:visibility="visible" />
<ImageButton
android:id="@+id/cmdPerson"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/person"
android:visibility="visible" />
<ImageButton
android:id="@+id/cmdOverview"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/cardoverview"
android:visibility="visible" />
<ImageButton
android:id="@+id/cmdCarLocation"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_menu_mylocation"
android:visibility="visible" />
<ImageButton
android:id="@+id/cmdSettings"
android:layout_width="70dp"
android:layout_height="70dp"
android:src="@drawable/ic_menu_offline"
android:visibility="visible" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Alles anzeigen
MainActivity
Code
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
@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);
}
}
Alles anzeigen
Vielleicht sieht ja jemand von Euch mein Problem oder gibt es eine gute Webseite, wo man das ganze vernünftig nachvollziehen
kann. Ich habe mir letztendlich am Ende eine Beispiel-App mit dem Android Studio angelegt und danach meine Applikation
versucht umzubauen.
Ich vermute den Layoutfehler, weil ich im Editor die Warnmeldung bekomme:
Code
This view is not contained vertical: at runtime it will jump to the top unless you add a vertical constraint
Viele Grüße
R.