主要活动中我的android代码中的错误

时间:2017-03-21 02:40:40

标签: java android android-asynctask

import android.os.AsyncTask;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;

import android.widget.TextView;

import java.io.BufferedReader;

import java.io.IOException;

import java.io.InputStream;

import java.io.InputStreamReader;

import java.net.HttpURLConnection;

import java.net.MalformedURLException;

import java.net.URL;

import java .lang.String;


import static android.support.v7.appcompat.R.id.text;


public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TextView text = (TextView) findViewById(R.id.tv);
        Button btn = (Button) findViewById(R.id.btn_hit);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                new Jsontask().execute("https://jsonparsingdemo-cec5b.firebaseapp.com/jsonData/moviesDemoItem.txt");
            }
        });
    }
        public class Jsontask extends AsyncTask<String,String,String>{
            @Override
            protected String doInBackground(String... params) {
                HttpURLConnection http=null;
                BufferedReader reader = null;

                try {
                    URL url=new URL(params[0]);
                    http=(HttpURLConnection)url.openConnection();
                    http.connect();
                    InputStream inp=http.getInputStream();
                    reader=new BufferedReader(new InputStreamReader(inp));


                    String line=" ";
                    StringBuffer read=new StringBuffer();
                    while((line=reader.readLine())!=null)
                        read.append(line);
                    return reader.toString();

                } catch(MalformedURLException e1){
                    e1.printStackTrace();



                } catch (IOException e) {
                    e.printStackTrace();
                }
                finally {
                    if (http != null)

                        http.disconnect();
                    try {
                        if(reader!=null)
                            reader.close();
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }

             return null;
            }

            @Override
           protected void onPostExecute(String result){
                super.onPostExecute(result);
                text.setText(result);
            }
        }
        }

2 个答案:

答案 0 :(得分:0)

我认为问题在于这一行:

import static android.support.v7.appcompat.R.id.text;

当你在这里设置文字时:

text.setText(result);

此文本未引用TextView文本。所以,如果它正确引用它,请检查一下。

答案 1 :(得分:0)

for (int j = 1; j <= numbers[i]/2; j+=2) {
    if (numbers[i] % j == 0) {
        total += (long) j;
    }
}

“text”是局部变量,因此asynctask无法找到它

请在string HexValue = "0x0FC"; int OtherValue = Convert.ToInt(HexValue);

之外定义