0

I caught the error message

"5-14 12:39:13.104 2518-2518/com.example.fdai3744.neueleereapp E/AndroidRuntime: FATAL EXCEPTION: main Process: com.example.fdai3744.neueleereapp, PID: 2518 java.lang.RuntimeException: Unable to instantiate activity ..."

and here's my Java Code

package com.example.fdai3744.neueleereapp;

import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


    public class MainActivity extends AppCompatActivity {

        public Button button_1 = (Button) findViewById(R.id.button1); //Button
        public TextView text1 = (TextView)findViewById(R.id.text1); // Textview

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

            button_1.setOnClickListener(new View.OnClickListener() { // Here I add the ActionListener for my button

                @Override
                public void onClick(View v) {
                    text1.setText("Button 1 wurde geklickt!");
                }
            });
        }

    }

If I start my App the emulator throws an error message "App has stopped". How should I prevent this error?

Ramón Wilhelm
  • 967
  • 2
  • 18
  • 39

2 Answers2

2

Well, your view hierarchy needs to be alive before your retrieve individual Views from it and the method setContentView() brings it to life(or instantiates it).

How?

setContentView(View) is a method exclusively available for Activity. Internally it calls the setContentView(View) of Window. This method sets the activity content to an explicit view. This view is placed directly into the activity's view hierarchy. Calling this function "locks in" various characteristics of the window that can not, from this point forward, be changed. Hence it is called only once.

So, instead of initializing the Views as instance variables, instantiate them inside onCreate() after setContentView().

Also read: Android: setContentView and LayoutInflater

Community
  • 1
  • 1
Manish Kumar Sharma
  • 12,982
  • 9
  • 58
  • 105
1

caused by

 public Button button_1 = (Button) findViewById(R.id.button1); //Button
    public TextView text1 = (TextView)findViewById(R.id.text1); // Textview

never assign view before setContentView() is called

your modified code

package com.example.fdai3744.neueleereapp;

import android.net.wifi.p2p.WifiP2pManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;


    public class MainActivity extends AppCompatActivity {

        public Button button_1;
        public TextView text1;
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

          button_1 = (Button) findViewById(R.id.button1); //Button
           text1 = (TextView)findViewById(R.id.text1); // Textview

            button_1.setOnClickListener(new View.OnClickListener() { // Here I add the ActionListener for my button

                @Override
                public void onClick(View v) {
                    text1.setText("Button 1 wurde geklickt!");
                }
            });
        }

    }
jagapathi
  • 1,635
  • 1
  • 19
  • 32