java.lang.NullPointerException:尝试再次在空对象引用上调用虚拟方法“ android.view.View android.view.View”

时间:2018-12-20 14:08:15

标签: java android

我是Android制作和应用程序的新手,可以从Oxford Dictionary API获取数据,但是每次我按下搜索按钮时,应用程序崩溃,并且控制台窗口中会显示错误弹出窗口。

这是代码

  

片段

public class Student4 extends Fragment {
String url;
Button getresponse;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.student4, container, false);
    url = dictionaryEntries();
    getresponse=(Button)view.findViewById(R.id.button);
    getresponse.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            MyDictionaryRequest myDictionaryRequest = new 
            MyDictionaryRequest(getActivity());
            myDictionaryRequest.execute(url);
        }
    });
    return view;
}


private String dictionaryEntries() {
    final String language = "en";
    final String word = "Ace";
    final String word_id = word.toLowerCase(); //word id is case sensitive and lowercase is required
    return "https://od-api.oxforddictionaries.com:443/api/v1/entries/" + language + "/" + word_id;
}

}

  

请求类

public class MyDictionaryRequest extends AsyncTask<String,Integer,String> {

final String app_id = "091b8935";
final String app_key = "5decabd8eaef02bb10c7219c07481bb0";
String myUrl;
Context context;
public View view;


MyDictionaryRequest(Context context){
    this.context = context;
}

@Override
protected String doInBackground(String... params) {

    myUrl = params[0];

    try {
        URL url = new URL(myUrl);
        HttpsURLConnection urlConnection = (HttpsURLConnection) url.openConnection();
        urlConnection.setRequestProperty("Accept","application/json");
        urlConnection.setRequestProperty("app_id",app_id);
        urlConnection.setRequestProperty("app_key",app_key);

        // read the output from the server
        BufferedReader reader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
        StringBuilder stringBuilder = new StringBuilder();

        String line = null;
        while ((line = reader.readLine()) != null) {
            stringBuilder.append(line + "\n");
        }

        return stringBuilder.toString();

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

@Override
protected void onPostExecute(String s) {
    super.onPostExecute(s);

    String def;

    try {
        JSONObject jsonObject = new JSONObject(s);
        JSONArray results = jsonObject.getJSONArray("results");

        JSONObject lEnteries = results.getJSONObject(0);
        JSONArray laArray = lEnteries.getJSONArray("lexicalEntries");

        JSONObject entries = laArray.getJSONObject(0);
        JSONArray entriesArray = entries.getJSONArray("entries");

        JSONObject senses = entriesArray.getJSONObject(0);
        JSONArray sensesArray = senses.getJSONArray("senses");

        JSONObject definations = sensesArray.getJSONObject(0);
        JSONArray definationsArray = definations.getJSONArray("definitions");

        def = definationsArray.getString(0);

>> error        TextView t1 = (TextView)view.findViewById(R.id.view);
        t1.setText(def);
        Toast.makeText(context, def, Toast.LENGTH_SHORT).show();



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

}

  

Errorr

   java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.getRootView()' on a null object reference
                  at netscope.mango.educy.MyDictionaryRequest.onPostExecute(MyDictionaryRequest.java:90)
                  at netscope.mango.educy.MyDictionaryRequest.onPostExecute(MyDictionaryRequest.java:23)
                  at android.os.AsyncTask.finish(AsyncTask.java:636)
                  at android.os.AsyncTask.access$500(AsyncTask.java:177)
                  at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:653)
                  at android.os.Handler.dispatchMessage(Handler.java:111)
                  at android.os.Looper.loop(Looper.java:210)
                  at android.app.ActivityThread.main(ActivityThread.java:5839)
                  at java.lang.reflect.Method.invoke(Native Method)
                  at java.lang.reflect.Method.invoke(Method.java:372)
                  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1113)
                  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:879)

在请求类中,当我尝试将响应设置为textview时,出现view = null的问题。请帮我。谢谢。

0 个答案:

没有答案