gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 11:35:56 28 4
gpt4 key购买 nike

这是我将值传递给名为 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com