私は数日間、この問題にひどく固執してきました。私が 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;
}
メインでエラーが発生する場所:-
@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)