简体   繁体   English

Android Studio:应用并非以已实现的匿名Java类开头

[英]Android Studio: App doesn't start with implemented Anonymous Java Class

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 ..." “ 5-14 12:39:13.104 2518-2518 / com.example.fdai3744.neueleereapp E / AndroidRuntime:致命例外:main进程:com.example.fdai3744.neueleereapp,PID:2518 java.lang.RuntimeException:无法实例化活动...”

and here's my Java Code 这是我的Java代码

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? 我应该如何防止该错误?

Well, your view hierarchy needs to be alive before your retrieve individual View s from it and the method setContentView() brings it to life(or instantiates it). 好吧,在从视图中检索单个View之前,您的视图层次结构必须处于活动状态,并且setContentView()方法使它setContentView() (或实例化)。

How? 怎么样?

setContentView(View) is a method exclusively available for Activity. setContentView(View)是专用于Activity的方法。 Internally it calls the setContentView(View) of Window. 在内部,它调用Window的setContentView(View) 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() . 因此,与其将View初始化为实例变量,不如在setContentView()之后在onCreate()实例化它们。

Also read: Android: setContentView and LayoutInflater 另请阅读: Android:setContentView和LayoutInflater

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 在调用setContentView()之前不要分配视图

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!");
                }
            });
        }

    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM