【问题标题】:accessing array with object使用对象访问数组
【发布时间】:2010-10-21 12:21:30
【问题描述】:

嗨,我在 android 中有两个类,在一个类中我写了一个数组,我想在主类中访问它,但错误是给我“强制关闭”这是我的代码

package com.semanticnotion.DAO;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class DAO extends Activity 
{
    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        WordsDAO DAO = new WordsDAO(new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"});


        Button next = (Button) findViewById(R.id.Button01);
        next.setOnClickListener(new View.OnClickListener() {
            public void onClick(View view) {
                Intent myIntent = new Intent(view.getContext(), WordsDAO.class);
                startActivity(myIntent);
            }
        });
    }
}

第二类代码是

package com.semanticnotion.DAO;

public class WordsDAO  
{
    String[] words = new String[] "Arte","Arquitectura","Familia","Moda","Cotilleos","Cine","Libros","Historia","Pintura","Musica","Tendencies","Modernimso","Pop art","Masteialismo","realities","filosofia","moda","fotografia","religion"};


    public  WordsDAO(String[] words ) 
    {
        this.words=words;
    }
}

请任何人告诉你这段代码中的错误是什么

【问题讨论】:

    标签: android-emulator


    【解决方案1】:

    首先:你的第二个类中的构造函数不会被使用。将参数传递给另一个活动的方法是在调用另一个活动的代码中使用Intent.putExtra,并在您的其他活动中使用

    Bundle extras = getIntent().getExtras(); 
    if(extras !=null)
    {
        String value = extras.getString("keyName");
    }
    

    获取onCreate中的数据。

    也就是说,我想问题出在你的第二个类没有提供显式无参数构造函数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-16
      • 2013-09-16
      • 1970-01-01
      • 2017-06-13
      • 1970-01-01
      相关资源
      最近更新 更多