应用程序在尝试在模拟器上运行时崩溃[Android Studio]

时间:2014-09-29 20:34:52

标签: android

我试图在模拟器中运行应用程序,但每次崩溃时 我得到了由以下原因引起的错误:

  

[" Java.lang.nullPointerException at(android.support.v4.app.backstackrecord.doAddOp(Backstackrecord.java:394)"]

我尝试了所有我知道但我还是坚持了! 我想我错过了一些代码,但我不知道它是什么!

以下是所有logcat Full logcat的链接 这是MainActivity.java类:

public class MainActivity extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        if (savedInstanceState == null)
        {
            getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new ForecastFragment()
                        .Commit());
             }
        }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public ForecastFragment Commit() {
        return null;
    }

            HttpURLConnection urlConnection = null;
            BufferedReader reader = null;

            String forecastJsonStr = null;
    }

这是ForecastFragment.java类:

   public class ForecastFragment extends Fragment {

        public ForecastFragment() {
        }

        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setHasOptionsMenu(true);
        }


        public void onCreateOptionMenu(Menu menu, MenuInflater inflater) {
            inflater.inflate(R.menu.forecastfragment, menu);
        }


        public boolean onOptionItemSelected(MenuItem item) {
            int id = item.getItemId();
            if (id == R.id.action_refresh) {
                FetchWeatherTask weatherTask = new FetchWeatherTask();
                weatherTask.execute("94043");
                return true;
            }
            return super.onOptionsItemSelected(item);
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {

            String[] data = {
                    "Today - Sunny - 88/63",
                    "Tomorrow - foggy - 70/46",
                    "weds - Cloudy - 72/63",
                    "Thurs - Rainy - 64/51",
                    "Fri - Foggy - 70/46",
                    "Sat - Sunny - 76/68"
            };

            List<String> weekForecast = new ArrayList<String>(
                    Arrays.asList(data));


            ArrayAdapter<String> mForecastAdapter =

            new ArrayAdapter<String>(
                    getActivity(),
                    R.layout.list_item_forecast,
                    R.id.list_item_forecast_textview,
                    weekForecast );

            View rootView = inflater.inflate(R.layout.fragment_main, container, false);

            ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
            listView.setAdapter(mForecastAdapter);

            return rootView;
        }

        public ForecastFragment Commit() {
            return null;
        }

        public class FetchWeatherTask extends AsyncTask<String, Void, Void> {
            private final String LOG_TAG = FetchWeatherTask.class.getSimpleName();

            private String getReadableDataString(long time) {
                Date date = new Date(time * 1000);
                SimpleDateFormat format = new SimpleDateFormat("E, MMM d");
                return format.format(date).toString();
            }

            protected String[] doInBackGround(String... params) {

                if (params.length == 0) {
                    return null;
                }

                HttpURLConnection urlConnection = null;
                BufferedReader reader = null;

                String forecastJsonStr = null;

                String format = "json";
                String units = "metric";
                int numDays = 7;


                try {
                    final String FORECAST_BASE_URL =
                            "http://api.openweathermap.org/data/2.5/forecast/daily?";
                    final String QUERY_PARAM = "q";
                    final String FORMAT_param = "MODE";
                    final String UNITS_param = "UNITS";
                    final String DAYS_param = "CNT";

                    Uri builtUri = Uri.parse(FORECAST_BASE_URL).buildUpon()
                            .appendQueryParameter(QUERY_PARAM, params[0])
                            .appendQueryParameter(FORMAT_param, format)
                            .appendQueryParameter(UNITS_param, format)
                            .appendQueryParameter(DAYS_param, Integer.toString(numDays))
                            .build();

                    URL url = new URL(builtUri.toString());
                    Log.v(LOG_TAG, "Built Uri " + builtUri.toString());


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

                    InputStream inputStream = urlConnection.getInputStream();
                    StringBuffer buffer = new StringBuffer();
                    if (inputStream == null) {
                        return null;

                    }

                    reader = new BufferedReader(new InputStreamReader(inputStream));

                    String line;
                    while ((line = reader.readLine()) != null) {

                        buffer.append(line + "\n");
                    }

                    if (buffer.length() == 0) {
                        return null;
                    }


                    forecastJsonStr = buffer.toString();

                    Log.v(LOG_TAG, "Forecast JSON String: " + forecastJsonStr);

                } catch (IOException e) {
                    Log.e(LOG_TAG, "Error", e);
                    return null;

                } finally {
                    if (urlConnection != null) {
                        urlConnection.disconnect();
                    }
                    if (reader != null) {
                        try {
                            reader.close();
                        } catch (final IOException e) {
                            Log.e(LOG_TAG, "Error closing stream", e);
                        }
                    }
                }
                return getWeatherDataFromJson(forecastJsonStr, numDays);

            }



            @Override
            protected Void doInBackground(String... params) {
                return null;
            }
        }

        public String[] getWeatherDataFromJson(String forecastJsonStr, int numDays) {
            return new String[0];
        }
    }

谢谢!

1 个答案:

答案 0 :(得分:1)

您的NullPointerException位于onCreate的MainActivity的第20行。你似乎在那里放错了一些括号:

getSupportFragmentManager().beginTransaction()
            .add(R.id.container, new ForecastFragment()
                    .Commit());

对片段进行Commit()。它必须是

getSupportFragmentManager()
    .beginTransaction()
    .add(R.id.container, new ForecastFragment())
    .commit();

如果这不能解决,您可以调试对象并检查哪一个为空。