Welcome to the Question2Answer Q&A. There's also a demo if you just want to try it out.
+2 votes
1.3k views
in Q2A Core by

Im trying to make this thing that generates worlds in my application for android but the ListActivity never opens because of the shared preferences. It says there is a storage null pointer exception, but I can't find a way to fix that. Any ideas? (It worked perfectly fine without the shared preferences.)

Code:

       
  1. //List Activity
    import android.app.ListActivity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
     
    public class WorldMenu extends ListActivity{
    String worldString2;
    String worldString;
    static String worldString3;
    String[] worldMenu;
        SharedPreferences worldList;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setListAdapter(new ArrayAdapter<String>(WorldMenu.this, android.R.layout.simple_list_item_1, worldMenu));
    worldMenu = worldString3.split(",");
    worldMenu[worldMenu.length] = "Create World";
    worldString = "Create World,";
    worldList = getSharedPreferences("worldString",0);
    worldString3 = worldList.getString("worldString", "Worlds not found.");
    }
     
    @Override
    protected void onPause() {
    // TODO Auto-generated method stub
    super.onPause();
    worldString2 = worldString3;
    Editor editor = worldList.edit();
    editor.putString("worldStuff",worldString2);
    editor.commit();
    }
     
    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
    // TODO Auto-generated method stub
    super.onListItemClick(l, v, position, id);
     
    if(position == 0){
    Intent openGenerator = new Intent("blablabla.WORLDNAME");
    startActivity(openGenerator);
    }
    }
     
     
    }
  2. package x.x.x;
     
    //Naming Activity
    import android.app.Activity;
    import android.content.Intent;
    import android.content.SharedPreferences;
    import android.content.SharedPreferences.Editor;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.EditText;
     
    public class worldName extends Activity{
    EditText worldType;
    String worldName; 
    Button save;
    SharedPreferences wname;
    String nameworld = "worldName";
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_worldname);
    wname = getSharedPreferences("worldName",0);
    worldType = (EditText) findViewById(R.id.editText1);
    save = (Button) findViewById(R.id.button1);
    save.setOnClickListener(new View.OnClickListener() {
     
    @Override
    public void onClick(View v) {
    // TODO Auto-generated method stub
    worldName = worldType.getText().toString();
    WorldMenu.worldString3 += worldName+",";
    Intent intent = new Intent("com.mtprogramming.blockfight.WORLDGENERATOR");
    startActivity(intent);
    }
    });
    }
     
    }
     

 

Please log in or register to answer this question.

...