简体   繁体   English

将代码移到另一个类会导致应用程序崩溃

[英]Moving code to another class leads to app crash

I'm creating an Android app and everything seems to be working fine. 我正在创建一个Android应用,一切似乎都工作正常。 The problem starts when I try to move some of the code to another file , in order to tide things up. 当我尝试将一些代码移到另一个文件以解决问题时,问题就开始了。

Let's say that the app has a spinner and a button. 假设该应用有一个微调器和一个按钮。 Pressing the button reads the current selected string of the Spinner and presents it with a Toast. 按下按钮将读取微调框的当前选定字符串,并将其显示为祝酒词。 So the code is: 所以代码是:

Spinner spinnerOmadas=(Spinner) findViewById(R.id.spinnerOmadas);
String omada = spinnerOmadas.getSelectedItem().toString();

Toast.makeText(getApplicationContext(), omada,Toast.LENGTH_SHORT).show();

If I keep this code in the onClick function of the button in the main activity, everything works. 如果我将此代码保留在主活动中按钮的onClick函数中,则一切正常。 I want to move it to another file since I will be adding more code to it. 我想将其移动到另一个文件,因为我将向它添加更多代码。

So I create a file buttonCalculation.java with the following code: 因此,我使用以下代码创建了一个文件buttonCalculation.java

package com.test.example;

import android.widget.EditText;
import android.widget.Spinner;
import android.widget.Toast;

public class buttonCalculation extends MainActivity {

    public void calculate(){

        Spinner spinnerOmadas=(Spinner) findViewById(R.id.spinnerOmadas);
        String t1= spinnerOmadas.getSelectedItem().toString();

        Toast.makeText(getApplicationContext(), t1, Toast.LENGTH_SHORT).show();

    }

}

and in the onClick method of the main activity I do this: 在主要活动的onClick方法中,我这样做:

buttonCalculation b1 = new buttonCalculation();
b1.calculate();

There is no error according to Android Studio in my approach, but when I run the app and press the button, it crashes. 按照我的方法,根据Android Studio并没有错误,但是当我运行该应用程序并按下按钮时,它崩溃了。

Is there something that I have to declare to be able to find the objects of the main Activity? 我必须声明一些内容才能找到主Activity的对象吗?

I think the problem is with the function findViewById() as you do not have any layout defined in your buttonCalculation class. 我认为问题出在函数findViewById()因为您没有在buttonCalculation类中定义任何布局。 Therefore you have to pass it as a parameter. 因此,您必须将其作为参数传递。

public class ButtonCalculation { //I don't find any reason to extend MainActivity here

    //also you need a constructor (except if you are using a static method)
    public void ButtonCalculation (){

    }

    public void calculate(ViewGroup root, Context context){

        Spinner spinnerOmadas=(Spinner) root.findViewById(R.id.spinnerOmadas);
        String t1= spinnerOmadas.getSelectedItem().toString();

        Toast.makeText(context, t1, Toast.LENGTH_SHORT).show();

    }

}

And in your MainActivity 在您的MainActivity中

final ViewGroup root= (ViewGroup) ((ViewGroup) this
            .findViewById(android.R.id.content)).getChildAt(0);
ButtonCalculation b1 = new ButtonCalculation ();
b1.calculate(root, getApplicationContext());

First declare the spinner button as a global variable in the MainActivity: 首先在MainActivity中将微调按钮声明为全局变量

protected Spinner spinner;

then in the onCreate() Method of MainActivity initialize it. 然后在MainActivityonCreate()方法中对其进行初始化。

spinnerOmadas=(Spinner) findViewById(R.id.spinnerOmadas);

and then you can use it in your buttonCalculation class as : 然后可以在buttonCalculation类中将其用作:

String t1= spinner.getSelectedItem().toString();

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

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