【问题标题】:NullPointerException on class variables and clear run for function variables类变量上的 NullPointerException 和函数变量的清除运行
【发布时间】:2014-08-22 14:25:16
【问题描述】:

当我运行以下命令时,我得到 NullPointerException

    Caused by: java.lang.NullPointerException
        at android.widget.ArrayAdapter.init(ArrayAdapter.java:310)
        at android.widget.ArrayAdapter.<init>(ArrayAdapter.java:166)
        at com.example.amirtau.helloworld.app.MainActivity$PlaceholderFragment.<init>(MainActivity.java:69) 

(以下代码中提到了第 69 行)

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 PlaceholderFragment())
                .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 static class PlaceholderFragment extends Fragment {


    String[] data = {
            "Mon 6/23 - Sunny - 31/17",
            "Tue 6/24 - Foggy - 21/8",
            "Wed 6/25 - Cloudy - 22/17",
            "Thurs 6/26 - Rainy - 18/11",
            "Fri 6/27 - Foggy - 21/10",
            "Sat 6/28 - TRAPPED IN WEATHERSTATION - 23/18",
            "Sun 6/29 - Sunny - 20/7"
    };
    List<String> weekForecast = new ArrayList<String>(Arrays.asList(data));

    //line 69
    ArrayAdapter<String> forecastAdapter = 
            new ArrayAdapter<String>(
                    getActivity(), // The current context (this activity)
                    R.layout.list_item_forecast, // The name of the layout ID.
                    R.id.list_item_forecast_textview, // The ID of the textview to populate.
                    weekForecast);

    public PlaceholderFragment() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container, false);
        ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast);
        listView.setAdapter(forecastAdapter);
        return rootView;
    }
}

}

但是当我将 : data, weekForecast, forecastAdapter 插入 onCreateView 函数时,一切正常。

请帮助我找出代码行为不同的原因。

【问题讨论】:

  • 如果进入构造函数的任何值是null,你至少调试过吗?

标签: java android nullpointerexception initialization


【解决方案1】:

getActivity() 返回 null 如果 Fragment 尚未附加到 Activity。 ArrayAdapter 正在构造函数中被实例化(此时分配有赋值的成员变量),此时 Fragment 尚未附加到 Activity。

您可以声明 ArrayAdapter 并等待在onCreateViewonActivityCreated 中实例化它

【讨论】:

    【解决方案2】:

    我通常更喜欢在 onAttach 中做类似的事情(即当片段完全附加到视图时):

    @public void onAttach(Activity activity)
    {
       super.onAttach(activity);
    
       //save an activity instance for use later
       this.mActivity = activity;
    
       //instantiate your array adapter and even pass values here
       .............
    }
    

    这可确保在您到达 onCreateView 时,您的数据已准备就绪!您还可以使用您的活动,并且不再像上述问题那样为空。

    我希望这会有所帮助。

    【讨论】:

      猜你喜欢
      • 2015-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-08-12
      • 2019-06-01
      • 1970-01-01
      • 2013-03-22
      • 1970-01-01
      • 2020-09-28
      相关资源
      最近更新 更多