几天来,我一直在严重地解决这个问题。当我调用一个名为 populatespinner() 的函数时出现问题。rowNO、nodeAll 和 msg 是全局声明的字符串。任何帮助将不胜感激,在此先感谢。D
在 populatespinner() 中:-
public void populateSpinner()
{
String nodestring="";
// ArrayList<String> itemsNode = new ArrayList<String>();
try
{
if(count!=1)
{
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
try {
try{
new DownloadWebPageTask().execute("http://www.scom.co.in/Group1ReturnData2.Aspx?row=1");
//if(rowNo.equalsIgnoreCase(""))
rowNo=msg;
// rowNo=OpenHttpConnection1("http://www.scom.co.in/Group1ReturnData2.Aspx?row=1");
if(rowNo.equals("4"))
{
new DownloadWebPageTask().execute("http://www.scom.co.in/Group1ReturnData2.Aspx?row=2");
nodeAll=msg;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
catch (Exception e) {
e.printStackTrace();
}
}
if(rowNo.equals("4"))
for(int i=1;i<=4;i++)
{
String[] nodeIdName=nodeAll.split(",");
String nodeVal=nodeIdName[i-1];
String[] nodeparts=nodeVal.split(" ");
if(i==1)
{
nodestring +=nodeparts[1];//nodeVal.substring(1,1);
}
else
{
{
nodestring ="";//" \" ";
nodestring +=nodeparts[1];
}
items1[i-1] = nodestring;
}
count +=1;
}
catch(Exception e)
{
textView.setText(e.toString());
textView = (TextView) findViewById(R.id.txtCon);
}
}
现在在 DownloadWebPageTask() 类中:-
private class DownloadWebPageTask extends AsyncTask<String, Void, String> {
@Override
protected String doInBackground(String... urls) {
String response = "";
for (String url : urls) {
DefaultHttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
String s = "";
s=OpenHttpConnection1(url);
// while ((s = buffer.readLine()) != null) {
response += s;
//}
// response="http request failed " + execute.getStatusLine();
} catch (Exception e) {
response= e.toString();
e.printStackTrace();
}
}
// msg=response;
return response;
}
@Override
protected void onPostExecute(String result) {
msg=result;
// textView.setText(result);
// textView.setVisibility(View.INVISIBLE);
}
}
并来自 OpenHttpConnection1(url) 方法:-
private String OpenHttpConnection1(String urlString)
throws IOException
{
String responseStr="";
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response==200)//(response == HttpURLConnection.HTTP_OK) {
{
in = httpConn.getInputStream();
BufferedReader buffer = new BufferedReader(new InputStreamReader(in));
String s = "";
while ((s = buffer.readLine()) != null)
{
responseStr += s;
}
}
else
responseStr="http request failed " +'"'+ httpConn.getResponseMessage() +'"' + " ( " + response +" )";
in.close();
httpConn.disconnect();
}
catch (Exception e) {
responseStr= e.toString();//.printStackTrace();
}
msg=responseStr;
return responseStr;
}
以及在 Main 中产生错误的地方:-
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_tabular);
// Button btwn = (Button)findViewById(R.id.btnRec);
// btwn.setBackgroundColor(Color.YELLOW);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
//-------Spinner(DropDown)----------
populateSpinner();
//*/
//====================
// String[] items1 = new String[]{"Node-1", "Node-2", "Node-3","Node-4"};
// itemsNode.add(nodestring);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,items1);
Spinner dropdown = (Spinner)findViewById(R.id.spinner2);
dropdown.setAdapter(adapter);
// final String ttt;
Spinner spinner = (Spinner)findViewById(R.id.spinner2);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
private boolean checked;
@Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// your code here
try{
Spinner mySpinner = (Spinner)findViewById(R.id.spinner2);
Text = mySpinner.getSelectedItem().toString();
// new DownloadWebPageTask().execute("http://www.scom.co.in/Group1ReturnData.Aspx?NodeName="+Text);
// textView = (TextView) findViewById(R.id.txtCon);
// try{
// if(textView.getFreezesText())
// msg= textView.getText().toString();
//=====================================
populateGridwt();
/*
*/
//========================================
//==========================================
// }
//catch(Exception e)
//{
// textView.setText(e.toString());
// textView.setVisibility(View.GONE);
// textView = (TextView) findViewById(R.id.txtCon);
// }
}
catch(NullPointerException e)
{
textView.setText(e.toString());
textView = (TextView) findViewById(R.id.txtCon);
}
}
最后是最新的 logcat :-
11-06 12:35:14.161: W/dalvikvm(8438): threadid=1: thread exiting with uncaught exception (group=0x40c86258)
11-06 12:35:14.172: E/AndroidRuntime(8438): FATAL EXCEPTION: main
11-06 12:35:14.172: E/AndroidRuntime(8438): java.lang.NullPointerException
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:394)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.ArrayAdapter.getView(ArrayAdapter.java:362)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.AbsSpinner.onMeasure(AbsSpinner.java:192)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.Spinner.onMeasure(Spinner.java:285)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.View.measure(View.java:12773)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.TableRow.getColumnsWidths(TableRow.java:310)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.TableLayout.findLargestCells(TableLayout.java:500)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.TableLayout.measureVertical(TableLayout.java:465)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.TableLayout.onMeasure(TableLayout.java:431)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.View.measure(View.java:12773)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1374)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.LinearLayout.measureVertical(LinearLayout.java:663)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.View.measure(View.java:12773)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.View.measure(View.java:12773)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.LinearLayout.measureVertical(LinearLayout.java:815)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.View.measure(View.java:12773)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4788)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
11-06 12:35:14.172: E/AndroidRuntime(8438): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2103)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.View.measure(View.java:12773)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1149)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2583)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.os.Handler.dispatchMessage(Handler.java:99)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.os.Looper.loop(Looper.java:137)
11-06 12:35:14.172: E/AndroidRuntime(8438): at android.app.ActivityThread.main(ActivityThread.java:4503)
11-06 12:35:14.172: E/AndroidRuntime(8438): at java.lang.reflect.Method.invokeNative(Native Method)
11-06 12:35:14.172: E/AndroidRuntime(8438): at java.lang.reflect.Method.invoke(Method.java:511)
11-06 12:35:14.172: E/AndroidRuntime(8438): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
11-06 12:35:14.172: E/AndroidRuntime(8438): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
11-06 12:35:14.172: E/AndroidRuntime(8438): at dalvik.system.NativeStart.main(Native Method)