public class LoadXml extends Activity {
private final static String TAG="LoadXml";
public static Context context = null;
Document document = null;
NodeList childsNodes = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
InputStream inputStreams = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(button1OnClickListener);
}
OnClickListener button1OnClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
getComplateXml();
}
};
public void getComplateXml(){
Log.d(TAG, "123");
try {
Log.d(TAG, "getComplateXml");
readUserConfig();
// int j = 0;
// for (int i = 0; i < childsNodes.getLength(); i++) {
// Log.d(TAG, "1");
// Node node = (Node) childsNodes.item(i);
// Log.d(TAG, "2");
// ContentResolver contentResolver = this.context
// .getContentResolver();
// Log.d(TAG, "3");
// Uri insertUri = Uri.parse("content://com.huawei.biz.LoadContentProvider/STUDENT");
// Log.d(TAG, "4");
// ContentValues values = new ContentValues();
// values.put("name", node.getNodeName());
// Log.d(TAG, node.getNodeName());
//
// values.put("id", node.getFirstChild().getNodeValue());
// Log.d(TAG, node.getFirstChild().getNodeValue());
//
// values.put("Photo",DataManager.getDrawableList().get(j));
// Log.d(TAG, DataManager.getDrawableList().get(j).toString());
// contentResolver.insert(insertUri, values);
// j++;
// }
} catch (Exception e) {
e.printStackTrace();
}
}
private void readUserConfig() throws Exception{
Log.d(TAG, "readUserConfig");
Log.d(TAG, "1");
factory = DocumentBuilderFactory.newInstance();
Log.d(TAG, "2");
builder = factory.newDocumentBuilder();
Log.d(TAG, "3");
inputStreams = LoadXml.context.getResources().getAssets().open("student.xml");
Log.d(TAG, "4");
document = builder.parse(inputStreams);
Log.d(TAG, "5");
childsNodes = document.getDocumentElement().getChildNodes();
Log.d(TAG, "6");
}
}
private final static String TAG="LoadXml";
public static Context context = null;
Document document = null;
NodeList childsNodes = null;
DocumentBuilderFactory factory = null;
DocumentBuilder builder = null;
InputStream inputStreams = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(button1OnClickListener);
}
OnClickListener button1OnClickListener=new OnClickListener(){
@Override
public void onClick(View v) {
Log.d(TAG, "onClick");
getComplateXml();
}
};
public void getComplateXml(){
Log.d(TAG, "123");
try {
Log.d(TAG, "getComplateXml");
readUserConfig();
// int j = 0;
// for (int i = 0; i < childsNodes.getLength(); i++) {
// Log.d(TAG, "1");
// Node node = (Node) childsNodes.item(i);
// Log.d(TAG, "2");
// ContentResolver contentResolver = this.context
// .getContentResolver();
// Log.d(TAG, "3");
// Uri insertUri = Uri.parse("content://com.huawei.biz.LoadContentProvider/STUDENT");
// Log.d(TAG, "4");
// ContentValues values = new ContentValues();
// values.put("name", node.getNodeName());
// Log.d(TAG, node.getNodeName());
//
// values.put("id", node.getFirstChild().getNodeValue());
// Log.d(TAG, node.getFirstChild().getNodeValue());
//
// values.put("Photo",DataManager.getDrawableList().get(j));
// Log.d(TAG, DataManager.getDrawableList().get(j).toString());
// contentResolver.insert(insertUri, values);
// j++;
// }
} catch (Exception e) {
e.printStackTrace();
}
}
private void readUserConfig() throws Exception{
Log.d(TAG, "readUserConfig");
Log.d(TAG, "1");
factory = DocumentBuilderFactory.newInstance();
Log.d(TAG, "2");
builder = factory.newDocumentBuilder();
Log.d(TAG, "3");
inputStreams = LoadXml.context.getResources().getAssets().open("student.xml");
Log.d(TAG, "4");
document = builder.parse(inputStreams);
Log.d(TAG, "5");
childsNodes = document.getDocumentElement().getChildNodes();
Log.d(TAG, "6");
}
}
inputStreams = LoadXml.context.getResources().getAssets().open("student.xml");
改成
inputStreams = getResources().getAssets().open("student.xml");
inputStreams = LoadXml.getResources().getAssets().open("student.xml");
Log.d(TAG, "4");
document = builder.parse(inputStreams);
Log.d(TAG, "5");
childsNodes = document.getDocumentElement().getChildNodes();报空指针,是因为返回的document对象为null,也即传入的Inputstream参数有问题一楼的正解~~