Anfänger - Force Close bei erstem Programm

F

Frask3r

Neues Mitglied
0
Hey Leute ich hab heute angefangen mich mal mit Android Programmierung zu beschäftigen.

Ich hab keine Vorkenntnisse was Programmieren betrifft abgesehen von ein paar theoretischen Kenntnissen (ich beherrsche keine Programmiersprache).

Ich arbeite mit dem Android Studio.

Mein Problem ist nun dass meine "erste App" leider garnicht starten will :D
Die App hat 2 Buttons und ein TextView Feld, welches die Anzahl der Klicks mitzählen soll. Die Buttons ändern nach dem ersten mal Drücken die Beschriftung auf "Gedrückt".

Meine MainActiivity sieht so aus:

package com.example.testapp.app;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.view.View;
import android.widget.TextView;

public class MainActivity extends Activity {


int c = 0;
Button button1;
Button button2;
TextView textView1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

this.button1 = new Button(null);
this.button2 = new Button(null);
this.textView1= new Button(null);

}


public void button1OnClick(View v){
this.button1.setText("Gedrückt");
c++;
this.textView1.setText("Klicks: "+c);

}

public void button2OnClick(View v) {
this.button2.setText("Gedrückt");
c++;
this.textView1.setText("Klicks: " +c);

}




@Override
public boolean onCreateOptionsMenu(Menu menu) {

// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.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();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}

}

Die Beiden Methoden button1/2OnClick sind auch mit den Buttons verknüpft (s XML).

activity_main.xml:

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context="com.example.testapp.app.MainActivity">

<TextView
android:text="@string/hello_world"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView1"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:id="@+id/button1"
android:layout_below="@+id/textView1"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:onClick="button1OnClick" />

<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 2"
android:id="@+id/button2"
android:layout_below="@+id/button1"
android:layout_alignParentStart="true"
android:layout_marginTop="27dp"
android:onClick="button2OnClick" />

</RelativeLayout>

Der Compiler läuft ohne Fehler, allerdings lässt sich die App nicht öffnen (sofortiger Force Close).
Der erste Versuch der App bei dem lediglich die Buttons ohne TextView Feld integriert waren lief problemlos, also es ist kein grundsätzliches PRoblem dass mein Handy (HTC One) nicht mit den Apps klarkommt.

Hier noch mein Logcat:

04-15 18:30:06.236 12096-12096/com.example.testapp.app W/dalvikvm threadid=1: thread exiting with uncaught exception (group=0x41f87e18)
04-15 18:30:06.246 12096-12096/com.example.testapp.app E/AndroidRuntime FATAL EXCEPTION: main
Process: com.example.testapp.app, PID: 12096
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp.app/com.example.testapp.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$800(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.ViewConfiguration.get(ViewConfiguration.java)
at android.view.View.<init>(View.java)
at android.view.View.<init>(View.java)
at android.widget.TextView.<init>(TextView.java)
at android.widget.Button.<init>(Button.java)
at android.widget.Button.<init>(Button.java)
at android.widget.Button.<init>(Button.java)
at com.example.testapp.app.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
************at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
************at android.app.ActivityThread.access$800(ActivityThread.java)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
************at android.os.Handler.dispatchMessage(Handler.java)
************at android.os.Looper.loop(Looper.java)
************at android.app.ActivityThread.main(ActivityThread.java)
************at java.lang.reflect.Method.invokeNative(Native Method)
************at java.lang.reflect.Method.invoke(Method.java)
************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
************at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
************at dalvik.system.NativeStart.main(Native Method)
04-15 18:45:54.007 13618-13618/com.example.testapp.app E/AndroidRuntime FATAL EXCEPTION: main
Process: com.example.testapp.app, PID: 13618
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.testapp.app/com.example.testapp.app.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
at android.app.ActivityThread.access$800(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
at android.os.Handler.dispatchMessage(Handler.java)
at android.os.Looper.loop(Looper.java)
at android.app.ActivityThread.main(ActivityThread.java)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.view.ViewConfiguration.get(ViewConfiguration.java)
at android.view.View.<init>(View.java)
at android.view.View.<init>(View.java)
at android.widget.TextView.<init>(TextView.java)
at android.widget.Button.<init>(Button.java)
at android.widget.Button.<init>(Button.java)
at android.widget.Button.<init>(Button.java)
at com.example.testapp.app.MainActivity.onCreate(MainActivity.java:24)
at android.app.Activity.performCreate(Activity.java)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java)
************at android.app.ActivityThread.performLaunchActivity(ActivityThread.java)
************at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java)
************at android.app.ActivityThread.access$800(ActivityThread.java)
************at android.app.ActivityThread$H.handleMessage(ActivityThread.java)
************at android.os.Handler.dispatchMessage(Handler.java)
************at android.os.Looper.loop(Looper.java)
************at android.app.ActivityThread.main(ActivityThread.java)
************at java.lang.reflect.Method.invokeNative(Native Method)
************at java.lang.reflect.Method.invoke(Method.java)
************at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java)
************at com.android.internal.os.ZygoteInit.main(ZygoteInit.java)
************at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
************at dalvik.system.NativeStart.main(Native Method)

Findet jemand vllt. doch nen Fehler im Code?

Ich hätte auch noch ein eine grundsätzliche Frage die sich vllt "nebenbei" klären lässt...
Wenn ich eine Methode einem Button bei "OnClick" zuweise die etwas mit "setText" bearbeitet woher weiß die Methode dann welches Objekt bearbeitet werden soll (bspw. Button1 soll per OnClick textView1 bearbeiten?) Bzw. Wie mach ich ihr das klar?
Ihr seht schon ich stehe gaaaaaaanz am Anfang :rolleyes2:
Ich hoffe mir kann jemand weiterhelfen :)

Greetz

EDIT: API ist v19 bis v19 (4.4.2) falls das relevant sein sollte und auf meinem Gerät läuft auch 4.4.2.
 
Zuletzt bearbeitet:
ohne mir das genau anzugucken.
du bekommst ne NUllPointerException.
Und du machst button1 = new Button(null);

Das muss einem doch ins Auge fallen.

Und das du auf textView1 einen button setzt is auch nicht besser ;)
 
Tatsache, das mit dem Button auf dem TextView ist n vertipper von mir.
Das mit dem null hab ich auch schon gesehen, aber was soll ich da denn stattdessen reinschreiben? Ich kanns nicht leerlassen dann zeigts mir nen Fehler, aber wüsste nicht was ich da reinschreiben soll? :D


Greetz :)
 
Buch, Tutorials, ... Was bringt es dir, wenn ich dir jetzt ein findviewbyid(R.id.button1) an den Kopf werfe? ;)
 
Ah danke wollte nur wissen ob ich auf dem richtigen weg bin, das war ich grade dabei zu versuchen ;)
Die meisten Tutorials ssind leider nicht auf komplette Neulinge ausgelegt sondern gehen schon von Java Kenntnissen aus...gibt nur ganz wenige für mich brauchbare.

EDIT: Ups, sorry habe grade deinen Nullpointerexception Sticky gesehen *schäm*...hab ich mich wohl grade eingereiht :(
 
Zuletzt bearbeitet:

Ähnliche Themen

D
Antworten
17
Aufrufe
419
datNeMo
D
U
  • unerfahrenerAppEntwickler
Antworten
3
Aufrufe
719
swa00
swa00
M
Antworten
3
Aufrufe
183
moin
M
Zurück
Oben Unten