Frage

Ich versuche, eine benutzerdefinierte Nummernauswahl für Anwendungen zu erstellen, die mindestens API-Level 8 erfordern. Bisher habe ich diesen Code erhalten. Er ist einfach, aber ich weiß nicht, wie ich diesen Fehler beheben kann.

Der Code ist bisher dieser:

Paket com.example.symbol_temp;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends ActionBarActivity {

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

        Integer counter=0;
        Button add,sub;
        final TextView display;

        add = (Button) findViewById(R.id.plus);
        sub = (Button) findViewById(R.id.minus);
        display = (TextView) findViewById(R.id.showtemperature);

        add.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
            counter++;
            display.setText( "" + counter);
            }
        });

        sub.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
            counter--;
            display.setText( "" + counter);
            }
        });

        }

}

und das XML ist das:

<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.example.symbol_temp.MainActivity$PlaceholderFragment" >

    <TextView
        android:id="@+id/showtemperature"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/minus"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_above="@+id/showtemperature"
        android:layout_centerHorizontal="true"
        android:text="+" 
        android:onClick="add"/>

    <Button
        android:id="@+id/plus"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/minus"
        android:layout_below="@+id/showtemperature"
        android:text="-" />

</RelativeLayout>

Erhalten Sie dies in LogCat:

04-14 13:31:59.222: E/AndroidRuntime(359): FATAL EXCEPTION: main
04-14 13:31:59.222: E/AndroidRuntime(359): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.symbol_temp/com.example.symbol_temp.MainActivity}: java.lang.NullPointerException
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.os.Looper.loop(Looper.java:123)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-14 13:31:59.222: E/AndroidRuntime(359):  at java.lang.reflect.Method.invokeNative(Native Method)
04-14 13:31:59.222: E/AndroidRuntime(359):  at java.lang.reflect.Method.invoke(Method.java:521)
04-14 13:31:59.222: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-14 13:31:59.222: E/AndroidRuntime(359):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-14 13:31:59.222: E/AndroidRuntime(359):  at dalvik.system.NativeStart.main(Native Method)
04-14 13:31:59.222: E/AndroidRuntime(359): Caused by: java.lang.NullPointerException
04-14 13:31:59.222: E/AndroidRuntime(359):  at com.example.symbol_temp.MainActivity.onCreate(MainActivity.java:24)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-14 13:31:59.222: E/AndroidRuntime(359):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-14 13:31:59.222: E/AndroidRuntime(359):  ... 11 more

Ich glaube, ich habe ein paar Dinge geändert. Zeile Nummer 24 lautet:

add.setOnClickListener(new View.OnClickListener() {

Vielen Dank für Ihre Hilfe.

War es hilfreich?

Lösung

Aus Ihrem Kommentar und

tools:context="com.example.symbol_temp.MainActivity$PlaceholderFragment"

Ich gehe davon aus, dass die Ansichten dazu gehören fragment_main.xml.

Du bläst das falsche Layout auf.Und Sie suchen die Ansicht im falschen Layout.

Ändere das

setContentView(R.layout.activity_main);

Zu

setContentView(R.layout.fragment_main);

Ändern Sie dies auch

Integer counter=0;

Zu

int counter; // primitive data type
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
     counter =0;

Andere Tipps

Ihre Zählervariable wird in der Inside-Methode deklariert, wonach Sie bei der Versuche, wenn Sie versuchen, Ihre Variablen über die Oncreate-Methode verschieben.

Hinweis:
Diese Antwort wird als Antwort auf coments gepostet von Person, die diese Frage gestellt hat, veröffentlicht.Ich habe das als Antwort auf Kürze gepostet Kummer Kummer Verwenden Sie diese Methode
protected void onSaveInstanceState(Bundle savedInstance) { super.onSaveInstanceState(savedInstance); savedInstance.putInt("myCounter",counter); }.
Dann

generasacodicetagpre.


von savedinstance, das Objekt der Bundle-Klasse ist, können Sie Ihren gespeicherten Wert audern und es anlegen zuweisen.Denken Sie daran, zu prüfen, ob der Wert null ist, denn wenn Sie Ihre App zum ersten Mal ausführen, ist Ihre Zähler möglicherweise nicht instanziiert, und es wird eine Ausnahme werfen.
Wenn Sie auch speichertvalues einholen, stellen Sie sicher, dass Sie sie nicht überschreiben.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top