java - 无法将值从一个 Intent 传递到另一个 Intent

标签 java android eclipse android-activity adt

这是我将值传递给名为 choice 的类的主要 Activity 。

@Override
public void onClick(View v) {
// TODO Auto-generated method stub

String x="";
for(int i=0;i<count;i++)
{
    x+=songf[i]+" ";
}
Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
Choice o = new Choice();
o.initializeit(songf,sizef,linkf,indexf,count);
Intent ix=new Intent("com.example.harmony.CHOICE");
startActivity(ix);
}

/*public void doit() 
{
    Choice o = new Choice();
    o.initializeit(songf,sizef,linkf,indexf,count);  
}
*/

这是类(class)选择

public class Choice extends Activity implements OnClickListener{

Button b;
RadioButton rb1,rb2,rb3,rb4 ;
RadioGroup rg;

static String[] songf = new String[19];
static Integer[] indexf = new Integer[19];
static String[] linkf = new String[19];
static double[] sizef = new double[19];
static int count;

public static void initializeit(String[] song, double[] size, String[] link,Integer[] index, int c) {
    // TODO Auto-generated method stub
    songf=song;
    sizef=size;
    indexf=index;
    linkf=link;
    count=c;
    String x="";
    //x+=Integer.toString(count);
    //Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
    //for(int i=0;i<count;i++)
    //{
    //  x+=songf[i]+" ";
//  }
//  Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
}

@Override
 protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.choice);

    b   = (Button) findViewById(R.id.download);
    rg  = (RadioGroup) findViewById(R.id.radioGroup1);
    rb1 = (RadioButton) findViewById(R.id.fcfs);
    rb2 = (RadioButton) findViewById(R.id.sjf);
    rb3 = (RadioButton) findViewById(R.id.priority);
    b.setOnClickListener(this);
    MainActivity o = new MainActivity();
    o.doit();

 }

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(rb1.isChecked())
    {
     for(int i=0;i<count;i++)
     {
        for(int j=0;j<count-i-1;j++)
        {
            if(indexf[j]>indexf[j+1])
            {
                int temp1=indexf[j];
                indexf[j]=indexf[j+1];
                indexf[j+1]=temp1;
                String temp2=linkf[j];
                linkf[j]=linkf[j+1];
                linkf[j+1]=temp2;
                String temp3=songf[j];
                songf[j]=songf[j+1];
                songf[j+1]=temp3;
                double temp4=sizef[j];
                sizef[j]=sizef[j+1];
                sizef[j+1]=temp4;
            }
        }
     }
     String x="";
        for(int i=0;i<count;i++)
        {
            x+=songf[i]+" ";
        }
        Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
    }
    else if(rb2.isChecked())
    {
        for(int i=0;i<count;i++)
         {
            for(int j=0;j<count-i-1;j++)
            {
                if(sizef[j]>sizef[j+1])
                {
                    double temp1=sizef[j];
                    sizef[j]=sizef[j+1];
                    sizef[j+1]=temp1;
                    String temp2=linkf[j];
                    linkf[j]=linkf[j+1];
                    linkf[j+1]=temp2;
                    String temp3=songf[j];
                    songf[j]=songf[j+1];
                    songf[j+1]=temp3;
                    int temp4=indexf[j];
                    indexf[j]=indexf[j+1];
                    indexf[j+1]=temp4;
                }
            }
         }
        String x="";
        for(int i=0;i<count;i++)
        {
            x+=songf[i]+" ";
        }
        x+=Integer.toString(count)+songf[0]+indexf[0];
        Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();
    }
    else if(rb3.isChecked())
    {
        String x="";
        for(int i=0;i<count;i++)
        {
            x+=songf[i]+" ";
        }
        Toast.makeText(getApplicationContext(), x, Toast.LENGTH_SHORT).show();

    }

}


}

在initializeit中初始化的字符串值未初始化并给我空值。我无法理解这个问题。请帮帮我。

最佳答案

您应该这样传递数据:

Intent intent = new Intent(this, Choice.class); 
intent.putExtra("fname", "My first name");
intent.putExtra("lname", "My last name");
startActivity(intent);

并以这种方式检索 Choice.class 中的数据:

Intent intent = getIntent();
String fName = intent.getStringExtra("fname");
String lName = intent.getStringExtra("lname");

使用方法 putExtra("key", value);getExtra("key"); 并且您不应该创建任何 Activity 的实例,除非您想导航到那里。查看更多详情doc .

关于java - 无法将值从一个 Intent 传递到另一个 Intent ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29991950/

相关文章:

java - 无法读取 VR 路径注册表 - Selenium Webdriver

java - 如何在 Android 上创建 50hz 方波并播放

java - 在 JRuby 中加载 GAMS Java API

java - 在 Eclipse 中查找 .class 文件

java - eclipse 编译器编译 javac 不会编译的代码 - 代码看起来是合法的

eclipse - 在 Eclipse git 中使用 native git 而不是 jgit?

java - 如何从android中的两个数组列表制作地理编码

java - 尝试启动 Android Studio 时出现 JDK 错误

JAVA访问json字符串

android - 使用旧密码登录 Google 帐户 - 如何重定向到蓝色 Google 登录页面?