Android Android-App Crash - Unable to start activity ComponentInfo

pascal.s

Neues Mitglied
Hallo,

bin momentan dabei meine erste eigene App zu programmieren und komme bei einem Problem einfach nicht weiter:

Ich habe in meiner MainActivity verschiedene Methoden erstellt, die Buttons erstellen sollen (Daten aus einer Datenbank ausgelesen --> Buttons dazu erstellt). Diese habe ich auch schon erfolgreich geschafft zu erstellen, doch musste dazu immer zuerst auf einen anderen Button klicken, damit ich eben die erste dazu nötige Methode (getData()) starte.

Jetzt will ich aber, dass diese Methode eben schon beim Öffnen der App gestartet wird und so direkt die Buttons erzeugt werden.

Wichtig zu wissen wäre vielleicht noch, dass ich über einen PagerAdapter zwei Fragmente in einer Aktivität implementiert habe, also damit ich eben durch ein Wischen auf die andere Seite komme.

Dabei habe ich immer einen Error bekommen, egal, was ich probiert habe. (Statisch oder nicht statisch, mit oder ohne View, aus onCreate in MainActivity oder FragmentProfile.java aufgerufen...)

Hier onCreate und getData (also die relevanten Methoden) aus der MainActivity:

Code:
ViewPager viewpager;
    TextView name_test;
    dbhelper dbhelper;
    Context context = this;
    SQLiteDatabase db;

    private ArrayList<Button> profile_button;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        viewpager = (ViewPager) findViewById(R.id.pager);
        PagerAdapter padap = new PagerAdapter(getSupportFragmentManager());
        viewpager.setAdapter(padap);
        View view = findViewById(R.id.fragment_Profile);
        getData(view);
    }

    public void getData(View view) {
        final MainActivity main = new MainActivity();

        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        RelativeLayout.LayoutParams paramsII = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 3);

        Button create_button = new Button(this);

        ImageView line = new ImageView(this);
        this.create_newProfile_Button(create_button, 100, params, paramsII, 100, 80, 0, 0, 18, "NEW PROFILE", line);


        try {
            this.dbhelper = new dbhelper(this.context);
            this.db = this.dbhelper.getReadableDatabase();

            Cursor cursor;

            this.db.rawQuery("SELECT * from profiles", null);
            cursor = this.db.rawQuery("SELECT * from profiles", null);

            cursor.moveToLast();
            Integer maxid = cursor.getInt(0);


            String title[] = new String[maxid];
            Integer ringtone[] = new Integer[maxid];
            Integer media[] = new Integer[maxid];
            Integer alarm[] = new Integer[maxid];
            String vibration[] = new String[maxid];
            Integer brightness[] = new Integer[maxid];
            String autobright[] = new String[maxid];
            String wlan[] = new String[maxid];
            String bluetooth[] = new String[maxid];
            String psm[] = new String[maxid];

            Button pb[] = new Button[maxid];
            ImageView underline[] = new ImageView[maxid];

            cursor.moveToFirst();

            for (int n = 0; n < maxid; n++) {
                title[n] = cursor.getString(1);
                ringtone[n] = cursor.getInt(2);
                media[n] = cursor.getInt(3);
                alarm[n] = cursor.getInt(4);
                vibration[n] = cursor.getString(5);
                brightness[n] = cursor.getInt(6);
                autobright[n] = cursor.getString(7);
                wlan[n] = cursor.getString(8);
                bluetooth[n] = cursor.getString(9);
                psm[n] = cursor.getString(10);

                this.createButton(pb[n], n, params, paramsII, 100, 80, 140 * (n + 1), 0, 18,
                        title[n], ringtone[n], media[n], alarm[n], vibration[n], brightness[n],
                        autobright[n], wlan[n], bluetooth[n], psm[n],
                        underline[n]);

                System.out.println(n + ". Button hinzugefügt");

                cursor.moveToNext();
            }
        } catch (Exception e) {
            Log.e("DATABASE ERROR", "NO DATA");
        }
    }

Hier der Code vom PagerAdapter:

Code:
public class PagerAdapter extends FragmentPagerAdapter {

    public PagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        switch (arg0) {
            case 0:
                return new FragmentProfile();
            case 1:
                return new FragmentRules();
            default:
                break;
        }
        return null;
    }

    @Override
    public int getCount() {
        return 2;
    }
}

Vom FragmentProfile.java (also der eine Teil der zwei Aktivitäten, die durch den PagerAdapter verbunden sind)

Code:
public class FragmentProfile extends Fragment {
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_profile, container, false);
    }
}

Und zu guter letzt noch die Error Meldung beim aktuellen Code (wie gesagt, hab schon verschiedenes probiert und auch unterschiedliche Error-Meldungen gehabt):

Code:
24675-24675/de.ps2406.apc.autoprofile E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: de.ps2406.apc.autoprofile, PID: 24675
    java.lang.RuntimeException: Unable to start activity ComponentInfo{de.ps2406.apc.autoprofile/de.ps2406.apc.autoprofile.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2411)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)
     Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
            at de.ps2406.apc.autoprofile.MainActivity.create_newProfile_Button(MainActivity.java:170)
            at de.ps2406.apc.autoprofile.MainActivity.getData(MainActivity.java:82)
            at de.ps2406.apc.autoprofile.MainActivity.onCreate(MainActivity.java:70)
            at android.app.Activity.performCreate(Activity.java:5958)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1129)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2364)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2474)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1359)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:155)
            at android.app.ActivityThread.main(ActivityThread.java:5696)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1028)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)


Wäre super, wenn ihr mir weiterhelfen könntet :)

Danke schonmal im Vorraus!

Pascal
 

truesoul

Top Contributor
Die Fehlermeldung wird in der newProfile_Button Methode geworfen.

Ich würde dir auf jedenfall empfehlen dir noch ein zwei Tutorials oder noch besser https://developer.android.com/training/index.html zu lesen

Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
            at de.ps2406.apc.autoprofile.MainActivity.create_newProfile_Button(MainActivity.java:170)
            at de.ps2406.apc.autoprofile.MainActivity.getData(MainActivity.java:82)
            at de.ps2406.apc.autoprofile.MainActivity.onCreate(MainActivity.java:70)

Sowas wie MainActivity main = new MainActivity() gehört sich in Android nicht.


Grüße
 
Zuletzt bearbeitet:

pascal.s

Neues Mitglied
Die Fehlermeldung wird in der newProfile_Button Methode geworfen.

Ich würde dir auf jedenfall empfehlen dir noch ein zwei Tutorials oder noch besser https://developer.android.com/training/index.html zu lesen

Code:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View)' on a null object reference
            at de.ps2406.apc.autoprofile.MainActivity.create_newProfile_Button(MainActivity.java:170)
            at de.ps2406.apc.autoprofile.MainActivity.getData(MainActivity.java:82)
            at de.ps2406.apc.autoprofile.MainActivity.onCreate(MainActivity.java:70)

Sowas wie MainActivity main = new MainActivity() gehört sich in Android nicht.


Grüße


Erstmal danke für die Antwort!

Kannst du mir eventuell noch genauer sagen, was mit dieser Methode nicht stimmt? :)

Mit der MainActivity main, das habe ich nur gemacht, weil in irgendnem Forum bei nem anderem Fall stand, dass das helfen solle...

Und auch danke für den Tutorial Tipp! :)
 
Ähnliche Java Themen
  Titel Forum Antworten Datum
C Android - OpenGL Crash Android & Cross-Platform Mobile Apps 2
J Benachrichtigung Freigabe ab Android 14 Android & Cross-Platform Mobile Apps 1
J Android Benachrichtigung zum Zeitpunkt ers Android & Cross-Platform Mobile Apps 15
J Das Beispiel von Android erzeugt Fehler Android & Cross-Platform Mobile Apps 8
J Zeitdifferenzen unter Android 7 (API < 26) berechnen Android & Cross-Platform Mobile Apps 4
W Netzwerk Verbindungen Java Android Android & Cross-Platform Mobile Apps 107
Z Android IntelliJ Android & Cross-Platform Mobile Apps 2
M Repository bei Room-Database in Android Studio (Java) Android & Cross-Platform Mobile Apps 2
Android App auf das eigene Handy bekommen Android & Cross-Platform Mobile Apps 3
Alex IV Android App erstellen Android & Cross-Platform Mobile Apps 3
OnDemand CrossPlatform Kotlin iOs/Android Datenverbrauch Android & Cross-Platform Mobile Apps 2
W In Android Studio Integer an andere activities übergeben Android & Cross-Platform Mobile Apps 2
wladp Android Studio Room Database Android & Cross-Platform Mobile Apps 1
N "Schöne" Datatable in Android und setzen von Parametern von Textview im Code Android & Cross-Platform Mobile Apps 5
N Android game programmieren Android & Cross-Platform Mobile Apps 5
T Android Studio: Einen Button in einer For Schleife verwenden Android & Cross-Platform Mobile Apps 2
K BLE Komunikation mit Android studio und esp32 Android & Cross-Platform Mobile Apps 5
G Android UDP Kommunikation Android & Cross-Platform Mobile Apps 1
M Paper DB wird in Android Studio nicht erkannt Android & Cross-Platform Mobile Apps 7
J Android zugrif auf Thread nach Handy drehen. Android & Cross-Platform Mobile Apps 10
T Android Android Augmented Faces in Java. Neue Landmarks erstellen Android & Cross-Platform Mobile Apps 1
K Android Android In-App-Purchase lädt nicht Android & Cross-Platform Mobile Apps 0
Besset Android http request an interne ip adresse funktioniert nicht Android & Cross-Platform Mobile Apps 8
J Is Android Development Head First Outdated? Android & Cross-Platform Mobile Apps 3
J Android Android Datenbankverbindung zum Raspberry Pi Android & Cross-Platform Mobile Apps 1
lolcore Android Studio -Download Documentation for Android SDK Android & Cross-Platform Mobile Apps 0
S Sinnvollste weg eine SQLite DB mit Android auslesen Android & Cross-Platform Mobile Apps 7
W Problem mit Android Studio Android & Cross-Platform Mobile Apps 0
W App Abo Android Android & Cross-Platform Mobile Apps 10
OSchriever Android Android MediaPlayer bei Anruf stoppen/pausieren Android & Cross-Platform Mobile Apps 2
OSchriever Auf onClick-Listener reagieren und Parameter übergeben (Android Studio) Android & Cross-Platform Mobile Apps 4
W removeNetwork Android App mit Spendenaktion fürs Tierheim! Android & Cross-Platform Mobile Apps 1
T Android R.string.test+i Problem Android & Cross-Platform Mobile Apps 2
P undefinierbarer Fehler Android Android & Cross-Platform Mobile Apps 8
T Android ArrayList sortieren mit 2 Werten ohne thencomparing , Wie? Android & Cross-Platform Mobile Apps 10
W Variable überschreiben (Android Studio) Android & Cross-Platform Mobile Apps 2
ruutaiokwu Android Selbst entwickelter SMTP-Client läuft auf PC, nicht aber auf Android Android & Cross-Platform Mobile Apps 9
ruutaiokwu Android Warum muss man bei Android Studio immer 2x auf "Run" klicken damit die App auf dem Gerät startet Android & Cross-Platform Mobile Apps 8
ruutaiokwu Android Wo das 'android.useAndroidX' property hinzufügen? Android & Cross-Platform Mobile Apps 8
ruutaiokwu Android In einem Android-“Spinner”-Element GLEICHZEITIG Bild (links) UND Text (rechts) anzeigen Android & Cross-Platform Mobile Apps 0
P Login und Registrierung Android Anzeige Android & Cross-Platform Mobile Apps 7
S Von JavaFx zu Android Android & Cross-Platform Mobile Apps 12
K Android to Pi | Websocket Problem Android & Cross-Platform Mobile Apps 3
ruutaiokwu Wie fügt man bei Android Studio .jar-Libraries zu einem Android-Java-Projekt hinzu? Android & Cross-Platform Mobile Apps 33
M Komponenten positionieren in Android Studio 3.6.3 Android & Cross-Platform Mobile Apps 1
M Android Studio - Property-Fenster einblenden Android & Cross-Platform Mobile Apps 1
M Android Studio - App auf dem Smartphone testen Android & Cross-Platform Mobile Apps 7
M Barrierefreie Appentwicklung für Android - Suche Codebeispiele Android & Cross-Platform Mobile Apps 8
M Android Studio - Configuration fehlt Android & Cross-Platform Mobile Apps 20
M Wo kann ich das Android SDK herunterladen / wie kann ich es installieren Android & Cross-Platform Mobile Apps 3
M Unsupported class file major version 57 - Fehlermeldung bei Android Studio Android & Cross-Platform Mobile Apps 27
ruutaiokwu Android Studio (SDK) ANDROID_SDK_ROOT-Variable? Android & Cross-Platform Mobile Apps 5
O Web API in Android (JAVA) einbinden Android & Cross-Platform Mobile Apps 3
J Android Studio macht seltsame Sachen Android & Cross-Platform Mobile Apps 2
J Android 9.1 aber android Studio findet API22 Android & Cross-Platform Mobile Apps 0
Dimax Web-Seite in native app convertieren mit Android Studio Android & Cross-Platform Mobile Apps 8
A Android Studio: while-Schleife beginnt nicht Android & Cross-Platform Mobile Apps 5
lolcore android studio: fehler bei laden des emulators Android & Cross-Platform Mobile Apps 10
J Android App - Browser öffnen und Text eingeben/Button click auslösen Android & Cross-Platform Mobile Apps 10
A Android-Studio: 2. Layout nach kurzer Zeit aufzeigen Android & Cross-Platform Mobile Apps 2
A jpg wird im Android Studio nicht akzeptiert Android & Cross-Platform Mobile Apps 3
J Android Studio - ArrayList - Selected Item ermitteln Android & Cross-Platform Mobile Apps 13
T Android SDK-Manager startet nicht in Eclipse Android & Cross-Platform Mobile Apps 5
T Bringen mir die Java-Basics irgendetwas für die Android-Programmierung Android & Cross-Platform Mobile Apps 4
J Was soll das bedeuten ? does not require android.permission.BIND_JOB_SERVICE permission Android & Cross-Platform Mobile Apps 7
A Android Studio: ImageView verpixelt Android & Cross-Platform Mobile Apps 2
J intend Service im Android Studio Android & Cross-Platform Mobile Apps 4
L Android Android Development eventuell mit Flutter Android & Cross-Platform Mobile Apps 1
S Android Layout - welchen Typ? Android & Cross-Platform Mobile Apps 3
T Fehler Android Studio: java.net.MalformedURLException: no protocol: http%3A%2F%2Fwww.mal ..... Android & Cross-Platform Mobile Apps 2
Arif Android Android Studio: Fehler beim Einbinden fremder Bibliothek? Android & Cross-Platform Mobile Apps 2
L Android Android Contacts DB auslesen Android & Cross-Platform Mobile Apps 1
A Android Studio - App mit Nearby Android & Cross-Platform Mobile Apps 1
L Android content URI Datei einlesen Android & Cross-Platform Mobile Apps 9
N Android Game Background Service Android & Cross-Platform Mobile Apps 11
Jackii Android Android Studio Error im Testlauf ohne zu programmieren Android & Cross-Platform Mobile Apps 9
B Android Probleme mit Android Studio Android & Cross-Platform Mobile Apps 6
Excess Android Service läuft nicht in Sandby weiter Android & Cross-Platform Mobile Apps 2
B Android Projekt für Android und IOS erstellen? Android & Cross-Platform Mobile Apps 5
J App funktioniert auf Android 5, auf 6 nicht Android & Cross-Platform Mobile Apps 2
J Android Snake Android & Cross-Platform Mobile Apps 15
J Android TaschenRechner Android & Cross-Platform Mobile Apps 22
I Das Problem mit der Tastatur... android:windowSoftInputMode="adjustPan" Android & Cross-Platform Mobile Apps 1
E Wie erhalte ich Zugriff auf das Microfon? (Android Studio) Android & Cross-Platform Mobile Apps 9
C Android Programmierung speziell oder einfach Java Buch kaufen? Android & Cross-Platform Mobile Apps 3
B Android Kein Zugriff auf Telefonspeicher (Android 6) Android & Cross-Platform Mobile Apps 1
T Android Equalizer für Android Android & Cross-Platform Mobile Apps 3
L Android Android Studio - Exportierte APK funktioniert nicht Android & Cross-Platform Mobile Apps 6
L Android Methode funktioniert nicht unter Android Android & Cross-Platform Mobile Apps 3
A Beginnen mit Serverkommunikatsion in Android Studio Android & Cross-Platform Mobile Apps 6
E Android Studio Android & Cross-Platform Mobile Apps 15
L Android Android Studio Setup killt Explorer Android & Cross-Platform Mobile Apps 3
K Android Videos rendern Android & Cross-Platform Mobile Apps 1
J Variable in strings.xml (Android Studio) Android & Cross-Platform Mobile Apps 0
B Android Android Studio lässt PC abstürzen Android & Cross-Platform Mobile Apps 3
B Android App Fehler Android & Cross-Platform Mobile Apps 21
J android Spinner funktioniert nicht Android & Cross-Platform Mobile Apps 14
G Android Push Notification Android & Cross-Platform Mobile Apps 2
Light Lux Fehlermeldung unter Android Studio Android & Cross-Platform Mobile Apps 1
D Android Android Apps direkt vom Handy aus programmieren? Android & Cross-Platform Mobile Apps 2

Ähnliche Java Themen

Neue Themen


Oben