启动前的Android空指针方法

时间:2015-06-23 19:48:43

标签: android nullpointerexception

对于我的项目,我有一个方法,应该在设置选项卡中更改后更新城市。我做的是创建一个方法,changeCity(字符串城市),但是当我启动应用程序时,changeCity总是为null,我尝试将该方法放在其他地方,但随后我的应用程序不断崩溃。这是我的代码:

public class MainActivity extends ActionBarActivity
    implements NavigationDrawerCallbacks,
    WeatherFragment.OnFragmentInteractionListener {

/**
 * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
 */
private NavigationDrawerFragment mNavigationDrawerFragment;
private Toolbar mToolbar;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
    setSupportActionBar(mToolbar);

    mNavigationDrawerFragment = (NavigationDrawerFragment)
            getFragmentManager().findFragmentById(R.id.fragment_drawer);

    // Set up the drawer.
    mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);



}


 @Override
public void onNavigationDrawerItemSelected(int position) {

    FragmentManager fragmentManager1 = getFragmentManager();
    Fragment fragment = new Fragment();
    switch (position) {
        case 0:
            fragment = new RecipeListFragment();

            break;
        case 1:

            fragment = new WeatherFragment();

            //SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
            //String weatherLocation = SP.getString("weatherLocation", "NA");

            //changeCity(weatherLocation + ", NL");
            //changeCity("Breda, NL");

            break;
        case 2:
            fragment = new SettingsFragment();
           // changeCity("Terneuzen, NL");
            break;
    }

    fragmentManager1.beginTransaction().replace(R.id.container, fragment).commit();
    //Toast.makeText(this, "Menu item selected -> " + position, Toast.LENGTH_SHORT).show();
}

public void changeCity(String city){
    WeatherFragment wf = (WeatherFragment)getFragmentManager().findFragmentById(R.id.container);
    wf.changeCity(city);
    new CityPreference(this).setCity(city);
}

当我将方法放在开关案例2中时,这是堆栈跟踪:

06-23 15:52:44.413  21422-21422/nl.shacklez.mijnreceptenofficialv1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: nl.shacklez.mijnreceptenofficialv1, PID: 21422
java.lang.ClassCastException: nl.shacklez.mijnreceptenofficialv1.RecipeListFragment cannot be cast to nl.shacklez.mijnreceptenofficialv1.WeatherFragment
        at nl.shacklez.mijnreceptenofficialv1.MainActivity.changeCity(MainActivity.java:81)
        at nl.shacklez.mijnreceptenofficialv1.MainActivity.onNavigationDrawerItemSelected(MainActivity.java:72)
        at nl.shacklez.mijnreceptenofficialv1.NavigationDrawerFragment.selectItem(NavigationDrawerFragment.java:174)
        at nl.shacklez.mijnreceptenofficialv1.NavigationDrawerFragment.onNavigationDrawerItemSelected(NavigationDrawerFragment.java:104)
        at nl.shacklez.mijnreceptenofficialv1.NavigationDrawerAdapter$1.onClick(NavigationDrawerAdapter.java:47)
        at android.view.View.performClick(View.java:4756)
        at android.view.View$PerformClick.run(View.java:19749)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        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)

当我在oncreate中使用该方法时,这是堆栈跟踪:

06-23 15:54:02.597  23278-23278/nl.shacklez.mijnreceptenofficialv1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: nl.shacklez.mijnreceptenofficialv1, PID: 23278
java.lang.RuntimeException: Unable to start activity ComponentInfo{nl.shacklez.mijnreceptenofficialv1/nl.shacklez.mijnreceptenofficialv1.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void nl.shacklez.mijnreceptenofficialv1.WeatherFragment.changeCity(java.lang.String)' on a null object reference
        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: Attempt to invoke virtual method 'void nl.shacklez.mijnreceptenofficialv1.WeatherFragment.changeCity(java.lang.String)' on a null object reference
        at nl.shacklez.mijnreceptenofficialv1.MainActivity.changeCity(MainActivity.java:82)
        at nl.shacklez.mijnreceptenofficialv1.MainActivity.onCreate(MainActivity.java:43)
        at android.app.Activity.performCreate(Activity.java:5933)

尝试答案后:

06-23 16:24:17.913  23712-23712/nl.shacklez.mijnreceptenofficialv1 E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: nl.shacklez.mijnreceptenofficialv1, PID: 23712
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.app.Activity.getString(int)' on a null object reference
        at nl.shacklez.mijnreceptenofficialv1.WeatherFragment$1$1.run(WeatherFragment.java:111)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        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)

2 个答案:

答案 0 :(得分:0)

findFragmentById(R.id.container)返回了RecipeListFragment。 您的程序试图将RecipeListFragment强制转换为WeatherFragment。

答案 1 :(得分:0)

您的堆栈跟踪显示您正在尝试将RecipeListFragment投射到WeatherFragment。该错误的来源位于您onNavigationDrawerItemSelected代码中,您首先调用changeCity,然后使用fragmentManager1.beginTransaction().replace(R.id.container, fragment).commit();更改片段

解决这种情况的最简单方法是将片段传递给changeCity方法,而不是搜索它。

public void changeCity(WeatherFragment wf, String city){
    wf.changeCity(city);
    new CityPreference(this).setCity(city);
}

然后像这样称呼它:

fragment = new WeatherFragment();
changeCity((WeatherFragment) fragment, "Breda, NL");