App startet nicht...Android studio

Y

YouKnowNow

Neues Mitglied
0
Hey Leute ich hab ne kleine App geschrieben aber irgendwie starten sie aber sobald ich auf ein Button drücke setht da ...reagiert nicht Hier der Code:
Lektionen. java hier stürtzt es ab:
package com.youknownow.lateinfelixuebersetzunen;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class Lektionen extends AppCompatActivity {

Button btn1 ;
Button btn2 ;
Button btn3 ;
Button btn4 ;
Button btn5 ;
Button btn6 ;
Button btn7 ;
Button btn8 ;

@override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_lektionen);

btn1 = (Button)findViewById(R.id.L2_10);
btn2 = (Button)findViewById(R.id.L11_20);
btn3 = (Button)findViewById(R.id.L21_30);
btn4 = (Button)findViewById(R.id.L31_40);
btn5 = (Button)findViewById(R.id.L41_50);
btn6 = (Button)findViewById(R.id.L51_60);
btn7 = (Button)findViewById(R.id.L61_70);
btn8 = (Button)findViewById(R.id.L75);

btn1.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View a) {
startActivity(new Intent(Lektionen.this,Lektion0.class));
}
});

btn2.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View b) {
startActivity(new Intent(Lektionen.this,Lektion1.class));
}
});

btn3.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View c) {
startActivity(new Intent(Lektionen.this,Lektion2.class));
}
});

btn4.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View d) {
startActivity(new Intent(Lektionen.this,Lektion3.class));
}
});

btn5.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View e) {
startActivity(new Intent(Lektionen.this,Lektion4.class));
}
});

btn6.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View f) {
startActivity(new Intent(Lektionen.this,Lektion5.class));
}
});

btn7.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View h) {
startActivity(new Intent(Lektionen.this,Lektion6.class));
}
});

btn8.setOnClickListener(new View.OnClickListener() {
@override
public void onClick(View i) {
startActivity(new Intent(Lektionen.this,Lektion7.class));
}
});
}
}
aktivity_lektionen.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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.youknownow.lateinfelixuebersetzunen.Lektionen">

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 2-10"
android:id="@+id/L2_10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 11-20"
android:id="@+id/L11_20"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 21-30"
android:id="@+id/L21_30"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="150dp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 31-40"
android:id="@+id/L31_40"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 41-50"
android:id="@+id/L41_50"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="250dp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 51-60"
android:id="@+id/L51_60"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="300dp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 61-70"
android:id="@+id/L61_70"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="350dp" />

<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Lektion 75"
android:id="@+id/L75"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="400dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Lektionen:"
android:id="@+id/TV"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="@string/D"
android:id="@+id/TVD"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
 
Und wie sehen deine Lection1 bis Lection7 Klassen aus?
Und wie sieht deine AndroidManifest.xml aus?
 
@Nonsens danke das du geantwortest hast hier die manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youknownow.lateinfelixuebersetzunen">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@Style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Lektionen">
</activity>

</application>

</manifest>
[doublepost=1476376988,1476376894][/doublepost]@Nonsens und hier ne lektion
Lektion1.java
package com.youknownow.lateinfelixuebersetzunen;

/**
* Created by abbas on 13.10.2016.
*/
public class Lektion1 {
}

und die xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">

<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/textView2"
android:layout_gravity="center_horizontal" /></ScrollView>
</LinearLayout>
 
Hi,
nun ist es mir klar wo es klemmt.

Eine Android APP besteht mindestens aus einer Activity oder mehreren Activity's.

Die APP in Android sind "Module" für die Android umgeben. Und damit Android weis dass deinen APP "Module" hat, muss man die im Manifest anmelden.
Hier ein gekürzter Auszug aus einem Manifest.

Code:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    .........
    <application
      
        .....
        <activity
            android:name=".Launcher"
            .....
        </activity>

        <!-- Listen for the "edit" action -->
        <activity
            android:name=".WidgetAuswahl"
            ......
        </activity>

        <!-- BroadcastReceiver -->
        <receiver android:name=".TaskerReceiver">
            ......
        </receiver>

        <activity
            android:name=".Impressum"
            android:label="@string/title_activity_impressum">
    </activity>
    
    <activity
        android:name=".Anleitung"
        android:label="@string/title_activity_anleitung">
    </activity>

        <activity
            android:name=".Profile"
            android:label="@string/title_activity_profil">

        </activity>
    </application>

</manifest>


Du siehst also, dass jede Activity im Manifest eintragen werden musst.
Deine LektionX Klassen brauchen also die Erweiterung extends Activity

Code:
public class Lektion1 extends Activity {

on Create(...){..} usw.

}

Grundsätzlich was zum Thema Activity. Wenn du etwas auf dem Bildschirm anzeigen möchtest brauchst du ein Activity und die layout.xml dazu wo drin steht was wie angezeigt werden soll.

Wenn du nun verschiedene Seiten mit anderem Layout anzeigen möchtest, etwa ein Auswahl Scrren, dann brauchst du dafür ein Activity und das entsprechende Layout. Stell dir einfach vor, dass jedes Activity eine Karte eines Kartenstapels ist. Neues Layout = neue Karte = neue Activity.
Wenn du nun mit startActivity(); eine Eigene oder Fremde Activity startest, wird die aufrufende Activity gestoppt und und um beim Beispiel mit den karten zu bleiben eine neue Karte (Activity) oben draufgelegt. Drückst du nun den Back Button, wird die Oberste Karte (Activity) vom Stapel entfernt und deine aufrufende Activity kommt wieder zum Vorschein.

Es ist vollkommen egal ob du mit startActivity eine eigene Klasse oder eine Fremde Klasse aufrufst. Jede Activity ist im Grunde ein eigenes Programm. Die Applikation definition ist nur das PACKET.

Also nochmal, für jede Screen definition (XML) braucht du eine Klasse die von Activity abgeleitet wurde (extends Activity) Und jede Activity muss im Manifest definiert werden.

das ist der Kopf meiner Klasse WidgetAuswahl:

Code:
public class WidgetAuswahl extends Activity
implements OnItemClickListener
{
 
und hier der Eintrag im Manifest:
<activity
    android:name=".WidgetAuswahl"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name">
 ........
</activity>

Stell dir einfach mal vor du würdest auf 10 kleinen Karten 10 verschiedenen Layouts für 10 verschiedene Seiten deiner App als Vorlage gestalten. Dann hättest du 10 Karten und das wären dann 10 Activity's, 10 layouts, 10 Screens, 10 Seiten deiner App.

Nun, ich bin nicht gut im Erklären und hoffe, dass das Prinzip nun etwas klarer wurde :crying:




Grüße
 
Zuletzt bearbeitet von einem Moderator:
markus.tullius schrieb:
Nais, falsche Begründung, aber richtige Lösung. ;)

Es gibt auch Apps ohne Activity. Man kann auch eine App bauen, die nur aus ein Service besteht. ;)

Processes and Threads | Android Developers

Das weis ich doch auch, aber er fängt erst damit an, da muss man schon auch einfach bleiben. Natürlich kann man das Ziel mit 2 Activity's und mehrere Layouts lösen. Ich würde es mit nur einer Activity lösen :biggrin: und eine WebView einbauen und die Lektionen als html Seiten einblenden. :biggrin:


Grüße
 
Zuletzt bearbeitet von einem Moderator:
@Nonsens Leider das gleiche Problem wie vorhin App wird beendet So sieht aber jetzt meine Manifestdatei aus :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.youknownow.lateinfelixuebersetzunen">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@Style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Lektionen">
</activity>


</application>
<activity
android:name=".Lektion0">
</activity>
<activity
android:name=".Lektion1">
</activity>
<activity
android:name=".Lektion2">
</activity>
<activity
android:name=".Lektion3">
</activity>
<activity
android:name=".Lektion4">
</activity>
<activity
android:name=".Lektion5">
</activity>
<activity
android:name=".Lektion6">
</activity>
<activity
android:name=".Lektion7">
</activity>
</manifest>
[doublepost=1476382251,1476382169][/doublepost]@YouKnowNow das ist auch das einzigste was ich geändert hab...
 
Das kann so nicht funktionieren, du hast für die neuen Activities außerhalb des Containers <Application> stehen
</application>
<activity
android:name=".Lektion0">

Alle deine Activity definitionen müssen innerhalb des Application tag sein

Code:
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="[USER=91274]@Style[/USER]/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Lektionen">
</activity>
<activity
android:name=".Lektion0">
</activity>
<activity
android:name=".Lektion1">
</activity>
<activity
android:name=".Lektion2">
</activity>
<activity
android:name=".Lektion3">
</activity>
<activity
android:name=".Lektion4">
</activity>
<activity
android:name=".Lektion5">
</activity>
<activity
android:name=".Lektion6">
</activity>
<activity
android:name=".Lektion7">
</activity>
</application>               
</manifest>

Hast du deine Lektion-Klassen mit extends Activity erweitert?
Deinen Lektion-Klassen brauchen auf jeden Fall die Funktion onCreate(...) und in dieser

setContentView(R.layout.deinlayout); // entsprechen anpassen

sonst wird nichts angezeigt.
 
Zuletzt bearbeitet von einem Moderator:
  • Danke
Reaktionen: markus.tullius

Ähnliche Themen

D
Antworten
23
Aufrufe
2.552
Data2006
D
SaniMatthias
Antworten
19
Aufrufe
960
swa00
swa00
5
Antworten
0
Aufrufe
1.150
586920
5
Zurück
Oben Unten