accessing array with object

 ̄綄美尐妖づ 提交于 2019-12-12 01:23:51

问题


hi i have two classes in android and in one class i have write an array and i want to access it in the main class but the error is give me that "force closed" here is my code

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

and the second class code is

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;
    }
}

please any one tell what well be the error in this code thaks


回答1:


First of all: the constructor in your second class would not be used. The way to pass parameters to another activity is to use Intent.putExtra in the code calling the other activity and in your other activity use

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

to get the data in onCreate.

That said, I guess the problem arises from your second class not providing an explicit parameterless constructor.



来源:https://stackoverflow.com/questions/3987349/accessing-array-with-object

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!