找到你要的答案

Q:Android null pointer method before startup

Q:启动前的空指针方法

For my project I have a method that should update the city after changing it in the settings tab. What I did was create a method, changeCity(string city) but when I start the application changeCity is always null, I tried putting the method on other places, but then my app keeps crashing. Here's my code:

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);
}

Here's the stacktrace when i put the method in the switch case 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)

Here's the stacktrace when I use the method in the 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)

After trying the answer:

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)

对于我的项目,我有一个方法,应该更新城市后,改变它在设置选项卡。我所做的是创建一个方法,改变了城市(串城)但当我启动应用程序改变了城市始终是空的,我试图把该方法在其他地方,但我的程序会崩溃。这是我的密码:

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)
answer1: 回答1:

findFragmentById(R.id.container) returned a RecipeListFragment. Your program tried to cast RecipeListFragment to WeatherFragment.

findFragmentById(R.id.container) returned a RecipeListFragment. Your program tried to cast RecipeListFragment to WeatherFragment.

answer2: 回答2:

Your stack trace shows that you are trying to cast RecipeListFragment to WeatherFragment. Source of that error is in your onNavigationDrawerItemSelected code where you are first calling changeCity and then changing fragment with fragmentManager1.beginTransaction().replace(R.id.container, fragment).commit();

Simplest way to resolve this situation would be to pass fragment to your changeCity method instead of searching for it.

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

And then call it like this:

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

你的堆栈跟踪表明你想投到weatherfragment recipelistfragment。那是你的onnavigationdraweritemselected误差源代码,你是第一个打电话改变了城市,然后改变fragmentmanager1片段。begintransaction()。取代(r.id.container,commit()片段);

要解决这个问题最简单的方法就是通过片段你改变了城市法而不是去寻找。

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

然后这样称呼它:

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