[ERLEDIGT] Warum bekomme ich hier eine NullPointerException

A

androidcoder

Neues Mitglied
0
Hallo,
ich bin noch Anfänger und hoffe, dass ihr für diese Frage Verständnis habt.
Warum bekomme ich hier eine NullPointerException?
Ich weiß, es ligt an dieser Zeile Code
button = (Button) findViewById(R.id.buttn);
aber ich verstehe nicht, warum es wegen diesem Code zu einer NullPointerException kommt?

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference


Code:
public class MainActivity extends AppCompatActivity {
 
    private EditText userInput;
    private TextView textView;
    private Button button;
 
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
 
        userInput = (EditText) findViewById(R.id.editText);
        textView = (TextView) findViewById(R.id.textView);
        button = (Button) findViewById(R.id.buttn);
 
        textView.setText("");
 
        textView.setMovementMethod(new ScrollingMovementMethod());
        View.OnClickListener ourOnClickListener = new View.OnClickListener(){
          @Override
          public void onClick(View view){
              textView.getText();
              String result = userInput.getText() +"\n";
              textView.append(result);
          }
        };
        button.setOnClickListener(ourOnClickListener);
    }
 
 
}

Code:
    <?xml version="1.0" encoding="utf-8"?>
    <android.support.constraint.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:id="@+id/activity_main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.timbuchalka.buttonclickcounter.MainActivity">
    
        <Button
            android:id="@+id/button"
            android:layout_width="93dp"
            android:layout_height="53dp"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginTop="16dp"
            android:text="Button"
            app:layout_constraintRight_toRightOf="@+id/activity_main"
            app:layout_constraintTop_toTopOf="@+id/activity_main" />
    
        <EditText
            android:id="@+id/editText"
            android:layout_width="0dp"
            android:layout_height="42dp"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:ems="10"
            android:inputType="textPersonName"
            android:text="Name"
            app:layout_constraintHorizontal_bias="0.4"
            app:layout_constraintLeft_toLeftOf="@+id/activity_main"
            app:layout_constraintRight_toLeftOf="@+id/button"
            app:layout_constraintTop_toTopOf="@+id/activity_main" />
    
        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_marginBottom="16dp"
            android:layout_marginEnd="16dp"
            android:layout_marginLeft="16dp"
            android:layout_marginRight="16dp"
            android:layout_marginStart="16dp"
            android:layout_marginTop="16dp"
            android:scrollIndicators="right|end"
            android:scrollbars="vertical"
            android:text="TextView"
            app:layout_constraintBottom_toBottomOf="@+id/activity_main"
            app:layout_constraintLeft_toLeftOf="@+id/activity_main"
            app:layout_constraintRight_toRightOf="@+id/activity_main"
            app:layout_constraintTop_toBottomOf="@+id/editText"
            app:layout_constraintVertical_bias="0.0" />
    </android.support.constraint.ConstraintLayout>


Code:
    <?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"
        android:weightSum="1">
    
        <Button
            android:id="@+id/buttn"
            android:layout_width="209dp"
            android:layout_height="17dp"
            android:layout_weight="0.08"
            android:text="Button" />
    </LinearLayout>
 
Wenn die Id im Layout nicht gefunden wird, liefert die Methode findViewById ein Nullobjekt zurück. Und genau, das ist hier der Fall.

Mit setContentView setzt du das Layout für die Activity, und dort gibt es keine id mit dem Namen @+id/buttn. Das untere Layout wird nicht berücksichtigt.

Wenn du zusätzliche Layouts einbinden willst, muss du die Klasse LayoutInflater benutzen.
 
  • Danke
Reaktionen: androidcoder und swa00
Vielen Dank.
Das hat mir sehr weitergeholfen :)
 

Ähnliche Themen

S
Antworten
3
Aufrufe
636
swa00
swa00
R
Antworten
4
Aufrufe
730
Rapidoman
R
L
Antworten
15
Aufrufe
908
jogimuc
J
Zurück
Oben Unten