修改类中类的变量

时间:2016-08-29 18:37:21

标签: java android

这是我本来想要做的,但不能。我希望从内部类函数更新urlDatasize的值。该应用程序崩溃,我在{I}尝试从Null Pointer Exception函数访问这些变量的行中提供CreateTextView()

package edu.ahduni.seas.gyapak;

import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

import org.json.JSONException;
import org.json.JSONArray;
import org.json.JSONObject;

import android.util.Log;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    public ArrayList<Data> urlData; //Culprit 1
    public int size; //no. of dirs + files, Culprit 2
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    public void parseJSON() throws JSONException {
        ParseActivity parse = new ParseActivity();
        String url = "RDovQWNhZGVtaWMvU2FuamF5IENoYXVkaGFyeQ==";
        parse.execute(url);
        CreateTextView();
        Log.e("RETURNING JSON","RETURNING");
    }

    public void CreateTextView() {
        TextView text = (TextView) findViewById(R.id.text);
        text.setText(Integer.toString(size));
        Button myButton = new Button(this);
        if(urlData==null) //workaround for crash
            return;
        myButton.setText(urlData.get(0).getName());

        LinearLayout ll = (LinearLayout)findViewById(R.id.textlayout);
        LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
                                                                    LinearLayout.LayoutParams.WRAP_CONTENT);
        ll.addView(myButton, lp);
    }

    class ParseActivity extends AsyncTask<String, Void, ArrayList<Data>> {
        InputStream is = null;
        JSONArray jObj = null;
        String json = "";


        @Override
        public void onPostExecute(ArrayList<Data> data) {
            size = data.size(); //here!
        }

        @Override
        protected ArrayList<Data> doInBackground(String... params) {
            final String BASE_URL = "http://111.93.66.162/json/main.php?f=" + params[0];
            HttpURLConnection urlConnection;

            // Making HTTP request
            try {

                Log.e("OPENING: ", "URL");
                URL url = new URL(BASE_URL);

                urlConnection = (HttpURLConnection) url.openConnection();
                urlConnection.setRequestMethod("GET");
                urlConnection.connect();

                is = urlConnection.getInputStream();

                StringBuffer buffer = new StringBuffer();
                if (is == null) {
                    // Nothing to do.
                    return null;
                }

                BufferedReader in = new BufferedReader(new InputStreamReader(is));
                String line;

                while ((line = in.readLine()) != null) {
                    buffer.append(line + "\n");
                    Log.e("JSON: ", line);
                }
                if (buffer.length() == 0) {
                    // Stream was empty.  No point in parsing.
                    return null;
                }
                is.close();
                json = buffer.toString();

            } catch (Exception e) {
                e.printStackTrace();
            }

            try {
                return getData(json);
            } catch (JSONException e) {
                e.printStackTrace();
            }
            return null;
        }

        public ArrayList<Data> getData(String jsonString) throws JSONException {
            // try parse the string to a JSON object
            final String EXT = "ext";
            final String PATH = "path";
            final String NAME = "name";

            try {
                jObj = new JSONArray(jsonString);
            } catch (JSONException e) {
                e.printStackTrace();
            }

            urlData = new ArrayList<Data>();

            for (int i=0;i<jObj.length();i++) {
                JSONObject obj = jObj.getJSONObject(i);
                String ext = obj.getString(EXT);
                String path = obj.getString(PATH);
                String name = obj.getString(NAME);

                urlData.add(new Data(ext,path,name)); //and here!
            }
            return urlData;
        }
    }
}

1 个答案:

答案 0 :(得分:1)

内部类不是静态的,因此需要实例化封闭类的实例。像这样:

public class abcd {
    static int x;

    public static void main(String[] args) {
        abcd o = new abcd(); // Create instance of abcd called 'o'
        abcd.efgh o2 = o.new efgh(); // Use instance 'o' to create an instance of efgh

        System.out.println(x); // 0
        o2.ok();
        System.out.println(x); // 5 
    }

    class efgh {
        public void ok() {
            x = 5;
        }
    }
}