在我的应用程序中,我动态创建了已输入文本的 EditText。然后onCLick,一个ArrayList获取EditTexts中的所有文本,然后序列化。我在 for 循环中获取 EditTexts 的文本并将它们添加到 ArrayList 中。
我想我知道发生了什么,但不知道如何解决它。我的理论是有一个问题,我在这里制作包含 EditTexts 之一的变量:
etIndex=(EditText)newView.findViewById(i+1000);
我的想法是,我说 newView 有一个问题。这是膨胀视图的名称,但问题是每次点击我再次膨胀布局时它都会被删除。如果这没有意义,请阅读下面包含的 Java 类,它将 100% 有意义。
这是我调用 .getText() 的地方和发生 NullPointerException 的行:
listOptions.add(etIndex.getText().toString());
这是我制作 EditText 的地方:
LayoutInflater inflater = LayoutInflater.from(view.getContext());
View newView = inflater.inflate(R.layout.additem, null);
listItem = (LinearLayout)newView.findViewById(R.id.lladdItem);
listItem.setId(numOfItems);
tvItem = (TextView)newView.findViewById(R.id.tvItem);
tvItem.setText(numOfItems + ".");
etItem = (EditText)newView.findViewById(R.id.etItem);
etItem.setId(numOfItems+1000);
etItem.setHint("List Item " + numOfItems);
如果你想要这是我的整个 Java 文件:
package com.frostbytedev.randomgenie;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Steven on 6/23/13.
*/
public class NewList extends Activity implements View.OnClickListener{
java.util.List<Integer> listOfET = new ArrayList<Integer>();
java.util.List<String> listOptions = new ArrayList<String>();
View newView;
LinearLayout insideScroll, listItem;
ScrollView svItems;
TextView tvItem;
EditText FileName, etItem, etItem1, etIndex;
Button Add, Remove, Save;
int numOfItems = 1, i;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlist);
initialize();
}
private void initialize() {
FileName=(EditText)findViewById(R.id.etTitle);
Save=(Button)findViewById(R.id.bSave);
Add=(Button)findViewById(R.id.bAdd);
Remove=(Button)findViewById(R.id.bRemove);
insideScroll=(LinearLayout)findViewById(R.id.insideScroll);
etItem1=(EditText)findViewById(R.id.etItem1);
Save.setOnClickListener(this);
Add.setOnClickListener(this);
Remove.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.bAdd:
numOfItems += 1;
LayoutInflater inflater = LayoutInflater.from(view.getContext());
View newView = inflater.inflate(R.layout.additem, null);
listItem = (LinearLayout)newView.findViewById(R.id.lladdItem);
listItem.setId(numOfItems);
tvItem = (TextView)newView.findViewById(R.id.tvItem);
tvItem.setText(numOfItems + ".");
etItem = (EditText)newView.findViewById(R.id.etItem);
etItem.setId(numOfItems+1000);
etItem.setHint("List Item " + numOfItems);
insideScroll.addView(newView);
break;
case R.id. bRemove:
if(numOfItems==1){
} else {
View v = findViewById(numOfItems);
((LinearLayout)v.getParent()).removeView(v);
numOfItems -= 1;
}
break;
case R.id.bSave:
IndexList();
try {
SaveList();
} catch (IOException e) {
e.printStackTrace();
}
Intent OpenList = new Intent(getApplicationContext(), ListRandom.class);
OpenList.putExtra("Filename", FileName+".txt");
startActivity(OpenList);
break;
}
}
private void SaveList() throws IOException {
String filename = FileName.getText().toString()+".txt";
FileOutputStream fos;
try {
fos = openFileOutput(filename,Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(listOptions);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
private void IndexList() {
listOptions.add(etItem1.getText().toString());
for(i=1;i<numOfItems;i++){
etIndex=(EditText)newView.findViewById(i+1000);
listOptions.add(etIndex.getText().toString());
}
}
}