Android Studio-java.lang.NullPointerException:存储== null

时间:2019-08-20 17:50:44

标签: java android-studio

我正尝试将数据从mysql php加载到android studio listview,我没有错误,但是当我运行它时,我的应用程序始终显示“已停止”。我是编程新手。

这是我主要活动中的代码,这是单击后应自动显示加载的数据的地方

public class project_sites extends AppCompatActivity {
ListView lvsite;
ArrayAdapter<String> adapter;
String address = "localhost/projects.php";
BufferedInputStream is;
String line=null;
String result=null;
String[] data;
List<ProductList> mproductlist;
private Productlistadapter pla;

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

    lvsite = (ListView) findViewById(R.id.listview);
   // mproductlist = new ArrayList<>();
    getData();
    pla = new Productlistadapter(this,data);

    StrictMode.setThreadPolicy((new 
 StrictMode.ThreadPolicy.Builder().permitNetwork().build()));
    //adapter = new ArrayAdapter<String>(this, 
 android.R.layout.simple_list_item_1,data);
    lvsite.setAdapter(pla);
}
private void getData()
{
    try
    { URL url = new URL(address);
        HttpURLConnection con= (HttpURLConnection) url.openConnection();
        con.setRequestMethod("GET");
        is=new BufferedInputStream(con.getInputStream());
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    try{
        BufferedReader br = new BufferedReader(new InputStreamReader(is));
        StringBuilder sb = new StringBuilder();

        while ((line=br.readLine()) != null)
        {
            sb.append(line+"\n");
        } is.close();
        result = sb.toString();
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    try {
        JSONArray jsa = new JSONArray(result);
        JSONObject jso = null;

        data = new String[jsa.length()];
        for (int i = 0; i < jsa.length(); i++) {
            jso = jsa.getJSONObject(i);
            data[i] = jso.getString("project_site");
        }
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
}
}

//这是我的productlist适配器文件

import java.util.List;

public class Productlistadapter extends ArrayAdapter<String> {

private Activity mContent;
private String[] listproject;

public Productlistadapter(Activity context, String[] listproject ) {
    super(context,  R.layout.listview_items,listproject);
    this.mContent = context;
    this.listproject=listproject;
}
@NonNull
@Override
public View getView(int position, @Nullable View convertView,@NonNull 
ViewGroup parent) {
    View r = convertView;
   ViewHolder viewHolder=null;
   if(r==null){
       LayoutInflater layoutInflater=mContent.getLayoutInflater();
       r=layoutInflater.inflate(R.layout.listview_items,null, true);
       viewHolder=new ViewHolder(r);
       r.setTag(viewHolder);
   }
   else
   {
       viewHolder=(ViewHolder)r.getTag();
   }
   viewHolder.listprojects.setText(listproject[position]);
    return r;
}
class ViewHolder
  {
    TextView listprojects;
    ViewHolder(View v){
        listprojects =  (TextView)v.findViewById(R.id.listproject);
    }
}
}

这是我的Logcat错误

E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.managerapp, PID: 9671
java.lang.RuntimeException: Unable to start activity 
ComponentInfo{com.example.managerapp/
com.example.managerapp.project_sites}: 

java.lang.NullPointerException: storage == null
    at android.app.ActivityThread.performLaunchActivity
       (ActivityThread.java:2416)
    at android.app.ActivityThread.handleLaunchActivity
 (ActivityThread.java:2476)
    at android.app.ActivityThread.-wrap11(ActivityThread.java)
   at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
    at android.os.Handler.dispatchMessage(Handler.java:102)
    at android.os.Looper.loop(Looper.java:148)
    at android.app.ActivityThread.main(ActivityThread.java:5417)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
       (ZygoteInit.java:726)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
 Caused by: java.lang.NullPointerException: storage == null
    at java.util.Arrays$ArrayList.<init>(Arrays.java:38)
    at java.util.Arrays.asList(Arrays.java:155)
    at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:137)
    at com.example.managerapp.Productlistadapter.<init> 
 (Productlistadapter.java:24)
    at 
 com.example.managerapp.project_sites.onCreate(project_sites.java:53)
    at android.app.Activity.performCreate(Activity.java:6237)
    at 
android.app.Instrumentation.callActivityOnCreate
      (Instrumentation.java:1107)
    at 
android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2369)
    at 
android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476) 
    at android.app.ActivityThread.-wrap11(ActivityThread.java) 
    at 
android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:148) 
    at android.app.ActivityThread.main(ActivityThread.java:5417) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run
       (ZygoteInit.java:726) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
Application terminated.

0 个答案:

没有答案