0

我总是得到这个错误。

Attemp To Invoke Virtual Method 'void android.support.v4.widget.SwipeRefreshLayout.setRefreshing(boolean)' on a null object reference

我试图获取一个 Json 文件并显示它,但它总是给我这个错误。有人遇到了这个,但它是一个简单的解决方法,我试图这样做但没有运气。请帮我。

这是我的MainActivity.java

package com.thatmg393.mclauncher;

//Main Imports
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.thatmg393.mclauncher.GetMC;
import com.thatmg393.mclauncher.GetMCUtils;
import com.thatmg393.mclauncher.R;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class MainActivity extends AppCompatActivity {
    
    SwipeRefreshLayout swipeRefresh;
    
    ArrayList<GetMC> arrayList;
    
    ListView lv;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home_layout);
        swipeRefresh = findViewById(R.id.swipeRefresh);
        lv = findViewById(R.id.listView);
        
        Toolbar toolbar=(Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        
        arrayList = new ArrayList<>();
        new fetchData().execute();
        swipeRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
                @Override
                public void onRefresh() {
                    new fetchData().execute();
                }
            });
        
    }
    public void goHome(View v) {
        setContentView(R.layout.home_layout);
    }
    public void goDownload(View v) {
        setContentView(R.layout.download_layout);
    }
    public void goLaunch (View v) {
        Intent launchMCApp = getPackageManager().getLaunchIntentForPackage("com.mojang.minecraftpee");

        if (launchMCApp != null) {
            startActivity(launchMCApp);
        }
        else {
            Toast.makeText(getApplicationContext(), "Please Download Minecraft In The Download Page", Toast.LENGTH_SHORT).show();
        }

    }
    public class fetchData extends AsyncTask<String, String, String> {

        @Override
        public void onPreExecute() {
            super .onPreExecute();
            swipeRefresh.setRefreshing(true);
        }

        @Override
        protected String doInBackground(String... params) {
            arrayList.clear();
            String result = null;
            try {
                URL url = new URL("https://api.jsonbin.io/b/61371510470d33259403b108");
                HttpURLConnection conn = (HttpURLConnection) url.openConnection();
                conn.connect();

                if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
                    InputStreamReader inputStreamReader = new InputStreamReader(conn.getInputStream());
                    BufferedReader reader = new BufferedReader(inputStreamReader);
                    StringBuilder stringBuilder = new StringBuilder();
                    String temp;

                    while ((temp = reader.readLine()) != null) {
                        stringBuilder.append(temp);
                    }
                    result = stringBuilder.toString();
                }else  {
                    result = "error";
                }

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

        @Override
        public void onPostExecute(String s) {
            super .onPostExecute(s);
            swipeRefresh.setRefreshing(false);
            try {
                JSONObject object = new JSONObject(s);
                JSONArray array = object.getJSONArray("data");

                for (int i = 0; i < array.length(); i++) {

                    JSONObject jsonObject = array.getJSONObject(i);
                    String getId = jsonObject.getString("id");
                    String getName_Version = jsonObject.getString("name_version");
                    String getAppSize = jsonObject.getString("app_size");
                    String getReleaseDate = jsonObject.getString("release_date");
                    String getIsAvailable = jsonObject.getString("isAvailable");

                    GetMC McInfo = new GetMC();
                    McInfo.setId(getId);
                    McInfo.setName_Version(getName_Version);
                    McInfo.setAppSize(getAppSize);
                    McInfo.setReleaseDate(getReleaseDate);
                    McInfo.setIsAvailable(getIsAvailable);
                    arrayList.add(McInfo);
                }
            } catch (JSONException  e) {
                e.printStackTrace();
            }

            GetMCUtils adapter = new GetMCUtils(MainActivity.this, arrayList);
            lv.setAdapter(adapter);

        }
    }
}

这是我的download_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:gravity="left">

        <ImageButton
            android:layout_width="wrap_content"
            style="?android:attr/buttonBarButtonStyle"
            android:layout_height="wrap_content"
            android:src="@drawable/back_button"
            android:id="@+id/toHome"
            android:layout_gravity="center"
            android:layout_marginLeft="5dp"
            android:onClick="goHome"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/downloadPage_text"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:layout_gravity="center"
            android:layout_marginLeft="62dp"/>

    </LinearLayout>

    <LinearLayout
        android:orientation="horizontal"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/swipeRefresh"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ListView
                android:id="@+id/listView"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

        </android.support.v4.widget.SwipeRefreshLayout>

    </LinearLayout>

</LinearLayout>

android.support.v4.widget.SwipeRefreshLayout不是父 UI 。

这是我的进口

import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;
import com.thatmg393.mclauncher.GetMC;
import com.thatmg393.mclauncher.GetMCUtils;
import com.thatmg393.mclauncher.R;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

我的 IDE 是AIDE PRO FIX 1

谢谢你。

4

0 回答 0