【问题标题】:Possible to have a FragmentPagerAdapter inside Fragment which is a part of FragmentActivity?可能在 Fragment 内部有一个 FragmentPagerAdapter,它是 FragmentActivity 的一部分?
【发布时间】:2014-02-20 14:05:37
【问题描述】:

我正在开发一个 android 应用程序,其中一个片段活动中有许多片段。我现在有一个新片段,其中调用了片段页面适配器。但是,这会引发空指针异常。可以这样做吗?

在我的片段中 onPostExecute (AsyncTask) :-

DailyForecastPageAdapter adapter = new DailyForecastPageAdapter(Integer.parseInt(forecastDaysNum),getActivity().getChildFragmentManager(), forecastWeather);

我面临问题的天气片段。

public class WeatherFrag extends Fragment{


private TextView cityText;
private TextView condDescr;
private TextView temp;
//private TextView press;
//private TextView windSpeed;
//private TextView windDeg;
private TextView unitTemp;

//private TextView hum;
private ImageView imgView;

private static String forecastDaysNum = "5";
private ViewPager pager;

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

    View rootView = inflater.inflate(R.layout.weather_main, container, false);
    String city = "London, UK";
    String lang = "en";

    cityText = (TextView) getActivity().findViewById(R.id.cityText);
    temp = (TextView) getActivity().findViewById(R.id.temp);
    unitTemp = (TextView) getActivity().findViewById(R.id.unittemp);
    //unitTemp.setText("°C");
    condDescr = (TextView) getActivity().findViewById(R.id.skydesc);

    pager = (ViewPager) getActivity().findViewById(R.id.pager);
    imgView = (ImageView) getActivity().findViewById(R.id.condIcon);
    JSONWeatherTask task = new JSONWeatherTask();
    task.execute(new String[]{city,lang});

    JSONForecastWeatherTask task1 = new JSONForecastWeatherTask();
    task1.execute(new String[]{city,lang, forecastDaysNum});
    return rootView;
}

私有类 JSONWeatherTask 扩展 AsyncTask {

    @Override
    protected Weather doInBackground(String... params) {
        Weather weather = new Weather();
        String data = ( (new WeatherHttpClient()).getWeatherData(params[0], params[1]));

        try {
            weather = JSONWeatherParser.getWeather(data);
            System.out.println("Weather ["+weather+"]");
            // Let's retrieve the icon
            weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));

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

}


@Override
protected void onPostExecute(Weather weather) {         
        super.onPostExecute(weather);
        Log.d("Err","Inside Post Exe");
        if (weather.iconData != null && weather.iconData.length > 0) {
            Log.d("Err","Inside Post Exe-if");
            Bitmap img = BitmapFactory.decodeByteArray(weather.iconData, 0, weather.iconData.length); 
            imgView.setImageBitmap(img);

        }

        Log.d("Err","Inside On Post Exe-Out");
        cityText.setText(weather.location.getCity() + "," + weather.location.getCountry());
        temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)));
        condDescr.setText(weather.currentCondition.getCondition() + "(" + weather.currentCondition.getDescr() + ")");

        /*

        temp.setText("" + Math.round((weather.temperature.getTemp() - 275.15)) + "°C");
        hum.setText("" + weather.currentCondition.getHumidity() + "%");
        press.setText("" + weather.currentCondition.getPressure() + " hPa");
        windSpeed.setText("" + weather.wind.getSpeed() + " mps");
        windDeg.setText("" + weather.wind.getDeg() + "°");
        */  
    }

}

私有类 JSONForecastWeatherTask 扩展 AsyncTask {

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

        String data = ( (new WeatherHttpClient()).getForecastWeatherData(params[0], params[1], params[2]));
        WeatherForecast forecast = new WeatherForecast();
        try {
            forecast = JSONWeatherParser.getForecastWeather(data);
            System.out.println("Weather ["+forecast+"]");

            //weather.iconData = ( (new WeatherHttpClient()).getImage(weather.currentCondition.getIcon()));

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

}





@Override
    protected void onPostExecute(WeatherForecast forecastWeather) {         
        super.onPostExecute(forecastWeather);
        Log.d("Err","Reached onpostexe");
        DailyForecastPageAdapter adapter = new DailyForecastPageAdapter(Integer.parseInt(forecastDaysNum),getChildFragmentManager(), forecastWeather);
        Log.d("Err","error before");
        pager.setAdapter(adapter);
    }



  }
}

LogCat

02-20 20:25:51.847: E/AndroidRuntime(5241): FATAL EXCEPTION: main
02-20 20:25:51.847: E/AndroidRuntime(5241): Process: com.akisoft.slidingmenu, PID: 5241
02-20 20:25:51.847: E/AndroidRuntime(5241): java.lang.NullPointerException
02-20 20:25:51.847: E/AndroidRuntime(5241):     at com.akisoft.weather.main.WeatherFrag$JSONWeatherTask.onPostExecute(WeatherFrag.java:94)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at com.akisoft.weather.main.WeatherFrag$JSONWeatherTask.onPostExecute(WeatherFrag.java:1)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at android.os.AsyncTask.finish(AsyncTask.java:632)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at android.os.AsyncTask.access$600(AsyncTask.java:177)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at android.os.Looper.loop(Looper.java:136)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invokeNative(Native Method)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at java.lang.reflect.Method.invoke(Method.java:515)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-20 20:25:51.847: E/AndroidRuntime(5241):     at dalvik.system.NativeStart.main(Native Method)

谁能帮我解决/理解我为什么要获得 NPE?我真的只需要它作为一个片段。

【问题讨论】:

  • 能分享一下 NPE 的堆栈跟踪吗?
  • @Supreethks:已添加 LogCat!
  • 应用程序在 WeatherFrag 片段的第 94 行崩溃。如果我的理解是正确的,您是在 FragmentActivity 中添加一个带有 Fragments 的 View 寻呼机,而不是在 Fragment 中嵌套(Pager inside a Fragment),对吗?如果您正在嵌套,您可以使用 childFragmentManager 而不是 FragmentManager
  • 我现在正在使用 childFragmentManager 但它仍然无法正常工作。我在这里包含片段的代码。

标签: android android-asynctask fragment fragmentpageradapter


【解决方案1】:

我想出了解决办法。问题是由于我在引用所有 textViews 时使用的 getActivity() 和 getChildFragmentManager() 足以创建 FragmentPagerAdapter 的对象。

我没有使用 getActivity(),而是使用了我的视图,即 rootView(在我的例子中),这解决了问题。 :)

【讨论】:

    【解决方案2】:

    将以下代码移至:

    JSONWeatherTask task = new JSONWeatherTask();
    task.execute(new String[]{city,lang});
    
    JSONForecastWeatherTask task1 = new JSONForecastWeatherTask();
    task1.execute(new String[]{city,lang, forecastDaysNum});
    

    onActivityCreatedonAtach

    【讨论】:

      猜你喜欢
      • 2012-10-08
      • 1970-01-01
      • 2022-01-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多