【发布时间】:2018-08-28 12:09:28
【问题描述】:
我可以使用我的代码读取文本文件并将其保存到Arraylist。现在我想在其他活动中使用这个Arraylist,但是我在将Arraylist 传递给另一个类时遇到了问题。我故意尝试过,但没有奏效。我将在下面添加我的程序的代码。
Intent intent=new Intent(this, ergebnisse.class);
intent.putExtra("NEFZ", Nlist);
startActivity(intent);
我希望你能帮助我。
我的第一个活动:
public class NEFZ2array extends AppCompatActivity implements Serializable {
public static void main(String[] args) {
FileReader file = null;
try {
file = new FileReader("NEFZ.txt");
} catch (FileNotFoundException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
ArrayList<Double> Nlist = new ArrayList<Double>();
int i=0;
Double d= null;
try {
BufferedReader input = new BufferedReader(file);
String s=null;
while((s=input.readLine())!=null) {
StringTokenizer st = new StringTokenizer(s,",");
while(st.hasMoreTokens()) {
try {
d = Double.parseDouble(st.nextToken());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Nlist.add(i, d);
}
}
input.close();
} catch(Exception e) {
e.printStackTrace();
}
for(double j:Nlist) {
System.out.println(j);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_nefz2array);
}
}
我的接收者活动:
public class ergebnisse extends AppCompatActivity implements Serializable {
public Button button_ausfuerlicher;
public Button button_home;
public void init(){
button_ausfuerlicher = (Button) findViewById(R.id.button_ausfuerlicher);
button_ausfuerlicher.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ergebnisse.this, ausfuehrlicher.class);
startActivity(intent);
}
});
}
public void init2(){
button_home = (Button) findViewById(R.id.button_home);
button_home.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(ergebnisse.this, MainActivity.class);
startActivity(intent);
}
});
}
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ergebnisse);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_SENSOR_LANDSCAPE);
init();
init2();
}
}
【问题讨论】:
-
Android 应用程序的入口点通常是 onCreate 方法。我怀疑您的主要方法(传统的 java 入口点)没有运行。您可以尝试从 onCreate 调用它,但实际上应该重命名它。要接收 Intent(只要您调用 main 并添加它就应该发送)确保接收者覆盖 onReceive
-
“没有工作”是什么意思
-
psvm() 不是 android 活动生命周期的一部分,因此永远不会被调用。
-
您好,Ugur,您的代码不完整,因为您放置/发送了额外的,但您在第二个活动中没有获取/接收它。检查这个:stackoverflow.com/a/5265952/10005752,祝你好运!
-
public static void main(String[] args) {这不是 Android 的工作方式。在继续之前,您似乎需要一些基本的 Android 教程
标签: java android android-intent arraylist