【问题标题】:How can i declare a variable outside of a method and then initialize them inside of OnCreate()?如何在方法之外声明一个变量,然后在 OnCreate() 中初始化它们?
【发布时间】:2017-02-17 06:34:12
【问题描述】:

我的问题和.getExtras()' on a null object reference的问题一样

但我不知道怎么做 codeMagic 说:“你需要在方法之外声明它们并在 onCreate() 内部初始化它们。或者将你需要的值传递给必要的函数”

对不起,我对编程世界很陌生。 `package ejemlo1.listaejemlo;

import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;


public class Lista extends Activity {

    private ListView lista;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lista);

        String elem1 = getIntent().getExtras().getString("Nombre");
        String elem2 = getIntent().getExtras().getString("Sexo");

        Espacios[] datos = new Espacios[] {
                new Espacios(elem1, elem2),
                new Espacios("Nombre2", "Sexo2")};

        lista = (ListView)findViewById(R.id.Interesar);

        Adapta adaptador = new Adapta(this, datos);
        lista.setAdapter(adaptador);

    }
    class Adapta extends ArrayAdapter<Espacios> {

        String elem1 = getIntent().getExtras().getString("Nombre");
        String elem2 = getIntent().getExtras().getString("Sexo");

        Espacios[] datos = new Espacios[] {
                new Espacios(elem1, elem2),
                new Espacios("Nombre2", "Sexo2")};

        public Adapta(Context context, Espacios[] datos) {
            super(context, android.R.layout.simple_list_item_2, datos);
        }

        public View getView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater = LayoutInflater.from(getContext());
            View item = inflater.inflate(android.R.layout.simple_list_item_2, null);

            TextView texto1 = (TextView) item.findViewById(android.R.id.text1);

            texto1.setText(datos[position].getNombre());

            TextView texto2 = (TextView) item.findViewById(android.R.id.text2);
            texto2.setText(datos[position].getSexo());



            return(item);


        }

    }


}
`

【问题讨论】:

  • 请在您的问题中添加到目前为止的代码示例。这可以让人们给出具体的建议。
  • 有我的代码,我解决了原来的问题,但我必须复制变量“datos”“elem1”“elem2”,现在我想知道是否存在消除第一个的方法.谢谢!

标签: object android-intent null bundle


【解决方案1】:

将变量 elem1elem2 声明为类成员,就像对 lista 变量所做的一样。这允许elem1elem2 可以访问您类中的所有方法。完成后,在 onCreate() 方法中将 getExtras 中的值分配给它。

例如

public class Lista extends Activity {

    private ListView lista;
    //Here we are declaring our elem variables outside the onCreate() method
    //This means that your "Adapta" class can use them
    private String elem1;
    private String elem2;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.lista);

        //And here we are assigning them the values from getExtras()
        elem1 = getIntent().getExtras().getString("Nombre");
        elem2 = getIntent().getExtras().getString("Sexo");

        Espacios[] datos = new Espacios[] {
            new Espacios(elem1, elem2),
            new Espacios("Nombre2", "Sexo2")
        };

        lista = (ListView)findViewById(R.id.Interesar);

        Adapta adaptador = new Adapta(this, datos);
        lista.setAdapter(adaptador);
    }

    class Adapta extends ArrayAdapter<Espacios> {

        //Notice we have removed elem1 and elem2 from the adapter class
        Espacios[] datos = new Espacios[] {
            new Espacios(elem1, elem2),
            new Espacios("Nombre2", "Sexo2")
        };

        public Adapta(Context context, Espacios[] datos) {
            super(context, android.R.layout.simple_list_item_2, datos);
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View item = inflater.inflate(android.R.layout.simple_list_item_2, null);

            TextView texto1 = (TextView) item.findViewById(android.R.id.text1);

            texto1.setText(datos[position].getNombre());

            TextView texto2 = (TextView) item.findViewById(android.R.id.text2);
            texto2.setText(datos[position].getSexo());

            return(item);
        }

    }

}

我在上面的例子中放了几个cmets,我希望这能让事情更清楚一点。

【讨论】:

    猜你喜欢
    • 2016-12-08
    • 2014-10-27
    • 1970-01-01
    • 1970-01-01
    • 2021-10-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-08
    相关资源
    最近更新 更多