简体   繁体   English

Android Studio“不幸的是我的项目已停止”

[英]Android Studio “Unfortunately my project has stopped”

The project itself does not report any error, but when I run my project with the emulator, it popped up "Unfortunately project has stopped", can anyone look at my project and know what error is? 项目本身不报告任何错误,但是当我使用模拟器运行我的项目时,它弹出“很遗憾项目已经停止”,任何人都可以查看我的项目并知道错误是什么? I got following logcat error: 我得到了以下logcat错误:

Process: edu.drexel.weatherundergroundapi, PID: 1800
java.lang.RuntimeException: Unable to start activity ComponentInfo{edu.drexel.weatherundergroundapi/edu.drexel.weatherundergroundapi.MainActivity}: java.lang.NullPointerException: storage == null
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
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:128)
at edu.drexel.weatherundergroundapi.CustomListAdapter.<init>(CustomListAdapter.java:35)
at edu.drexel.weatherundergroundapi.MainActivity.onCreate(MainActivity.java:25)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
            at android.app.ActivityThread.access$800(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:135)
            at android.app.ActivityThread.main(ActivityThread.java:5221)
            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:899)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

There are three java classes and three xml files: 1. MainActivity.java 2. CustomListAdapter.java 3. MyBgTask.java 4. listView.xml 5. activity_main.xml 有三个java类和三个xml文件:1。MainActivity.java 2. CustomListAdapter.java 3. MyBgTask.java 4. listView.xml 5. activity_main.xml

public class MainActivity extends Activity {


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

        MyBgTask asyncTask = new MyBgTask();

        CustomListAdapter adapter = new CustomListAdapter(this, asyncTask.getTimeArray(), asyncTask.getConditionArray(),
                                    asyncTask.getTempArray(), asyncTask.getHumidityArray(), asyncTask.getUrls());

        ListView list = (ListView) findViewById(R.id.listView);
        list.setAdapter(adapter);

        asyncTask.execute();
    }

    /*public void onListItemClick(ListView lv, View view, int position, int imgid) {
        String Slecteditem= (String)getListAdapter().getItem(position);
        Toast.makeText(this, Slecteditem, Toast.LENGTH_SHORT).show();
    }*/


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
  1. CustomListAdapter.java: CustomListAdapter.java:

     public class CustomListAdapter extends ArrayAdapter<String> { private final Activity context; private final String[] time; private final String[] condition; private final String[] temp; private final String[] humidity; private final String[] url; public CustomListAdapter(Activity context, String[] time, String[] condition, String[] temp, String[] humidity, String[] url) { super(context, R.layout.listview, time); this.context = context; this.time = time; this.condition = condition; this.temp = temp; this.humidity = humidity; this.url = url; } private Bitmap getImageBitmap(String url) { Bitmap bm = null; try { URL aURL = new URL(url); URLConnection conn = aURL.openConnection(); conn.connect(); InputStream is = conn.getInputStream(); BufferedInputStream bis = new BufferedInputStream(is); bm = BitmapFactory.decodeStream(bis); bis.close(); is.close(); } catch (IOException e) { Log.e("Error getting bitmap", e.toString()); } return bm; } public View getView(int position, View view, ViewGroup parent) { LayoutInflater inflater = context.getLayoutInflater(); View rowView = inflater.inflate(R.layout.listview, null, true); TextView timeView = (TextView) rowView.findViewById(R.id.dateTime); TextView conditionView = (TextView) rowView.findViewById(R.id.condition); TextView tempView = (TextView) rowView.findViewById(R.id.temp); TextView humidityView = (TextView) rowView.findViewById(R.id.humidity); ImageView imageView = (ImageView) rowView.findViewById(R.id.icon); timeView.setText(time[position]); conditionView.setText(condition[position]); tempView.setText(temp[position]); humidityView.setText(humidity[position]); imageView.setImageBitmap(getImageBitmap(url[position])); return rowView; } 

    } }

  2. MyBgTask.java MyBgTask.java

     public class MyBgTask extends AsyncTask<Void, Void, Void> { String[] timeArray; String[] conditionArray; String[] tempArray; String[] humidityArray; String[] urls; public String[] getTimeArray() { return timeArray; } public String[] getConditionArray() { return conditionArray; } public String[] getTempArray() { return tempArray; } public String[] getHumidityArray() { return humidityArray; } public String[] getUrls() { return urls; } @Override protected Void doInBackground(Void... params) { try { // Get User Current Location String geoURL = "http://api.wunderground.com/api/56419d324b9bf190/geolookup/q/autoip.json"; URL url = new URL(geoURL); HttpURLConnection request = (HttpURLConnection) url.openConnection(); request.connect(); request.getContent(); JsonParser geo_jp = new JsonParser(); JsonElement geo_root = geo_jp.parse(new InputStreamReader((InputStream) request.getContent())); JsonObject geo_rootobj = geo_root.getAsJsonObject(); String city = geo_rootobj.get("location").getAsJsonObject().get("city").getAsString(); String state = geo_rootobj.get("location").getAsJsonObject().get("state").getAsString(); String zip = geo_rootobj.get("location").getAsJsonObject().get("zip").getAsString(); // Invoke hourly weather forcast with user's current location String forecastURL = "http://api.wunderground.com/api/56419d324b9bf190/conditions/hourly/forecast/q/" + zip + ".json"; URL furl = new URL(forecastURL); HttpURLConnection forecastRequest = (HttpURLConnection) furl.openConnection(); forecastRequest.connect(); forecastRequest.getContent(); JsonParser forecast_jp = new JsonParser(); JsonElement forecast_root = forecast_jp.parse(new InputStreamReader((InputStream) request.getContent())); JsonArray forecast_array = forecast_root.getAsJsonObject().get("hourly_forcast").getAsJsonArray(); for (int hour = 0; hour < forecast_array.size(); hour++) { String month = forecast_array.get(hour).getAsJsonObject(). get("FCTTIME").getAsJsonObject(). get("mon_abbrev").getAsString(); String monthDay = forecast_array.get(hour).getAsJsonObject(). get("FCTTIME").getAsJsonObject(). get("mday_padded").getAsString(); String time = forecast_array.get(hour).getAsJsonObject(). get("FCTTIME").getAsJsonObject(). get("civil").getAsString(); String dateAndtime = month + " " + monthDay + " " + time; String condition = forecast_array.get(hour).getAsJsonObject(). get("condition").getAsString(); String temp = forecast_array.get(hour).getAsJsonObject(). get("temp").getAsJsonObject(). get("english").getAsString(); String humidity = forecast_array.get(hour).getAsJsonObject(). get("humidity").getAsString(); String icon_url = forecast_array.get(hour).getAsJsonObject(). get("icon_url").getAsString(); timeArray[hour] = dateAndtime; conditionArray[hour] = condition; tempArray[hour] = temp; humidityArray[hour] = humidity; urls[hour] = icon_url; } } catch (IOException e) { Log.e("ERROR", e.toString()); } return null; } protected void onPostExecute(Void... params) { } } 
  3. listView.xml listView.xml

     <ImageView android:id="@+id/icon" android:layout_width="60dp" android:layout_height="60dp" android:padding="5dp" /> <LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/dateTime" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceMedium" android:layout_marginLeft="10dp" android:layout_marginTop="5dp" android:padding="2dp" android:textColor="#33CC33" android:text="@string/dateTime" /> <TextView android:id="@+id/condition" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/condition" android:layout_marginLeft="10dp"/> <TextView android:id="@+id/temp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/temp" android:layout_marginLeft="10dp" /> <TextView android:id="@+id/humidity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/humidity" android:layout_marginLeft="10dp" /> </LinearLayout> 

  4. activity_main.xml activity_main.xml中

     <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".main"> <ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/listView" android:clickable="false" android:layout_alignParentLeft="true" android:layout_alignParentStart="true" > </ListView> 

Based on what I can see, the program crashes because time is null at this line: 基于我所看到的,程序崩溃,因为此行的timenull

public CustomListAdapter(Activity context, String[] time, String[] condition, String[] temp, String[] humidity, String[] url){
    super(context, R.layout.listview, time);
    ...
}

This is because, as @codeMagic said in the comments, you are using the AsyncTask class incorrectly. 这是因为,正如@codeMagic在评论中所说,您正在错误地使用AsyncTask类。 The following code in your onCreate method onCreate方法中的以下代码

MyBgTask asyncTask = new MyBgTask();

CustomListAdapter adapter = new CustomListAdapter(this, asyncTask.getTimeArray(), asyncTask.getConditionArray(),
                                asyncTask.getTempArray(), asyncTask.getHumidityArray(), asyncTask.getUrls());

Creates an AsyncTask, but doesn't execute() it. 创建一个AsyncTask,但不execute()它。 When you call getTimeArray() , it returns the value of timeArray , which is a default-initialised null value. 当你调用getTimeArray() ,它返回timeArray的值,这是一个默认初始化的null值。

Broadly, you need to 从广义上讲,你需要

  • Execute the AsyncTask 执行AsyncTask
  • Update the ArrayListAdapter when the task is done 任务完成后更新ArrayListAdapter

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM