java.lang.NullPointerException error help me [closed] - java

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
it's my logCat.
04-09 09:11:18.178: E/AndroidRuntime(829): FATAL EXCEPTION: main
04-09 09:11:18.178: E/AndroidRuntime(829): java.lang.NullPointerException
04-09 09:11:18.178: E/AndroidRuntime(829): at com.example.androidresim.Activitygiris.onLoadScene(Activitygiris.java:84)
04-09 09:11:18.178: E/AndroidRuntime(829): at org.anddev.andengine.ui.activity.BaseGameActivity.doResume(BaseGameActivity.java:158)
04-09 09:11:18.178: E/AndroidRuntime(829): at org.anddev.andengine.ui.activity.BaseGameActivity.onWindowFocusChanged(BaseGameActivity.java:82)
04-09 09:11:18.178: E/AndroidRuntime(829): at com.android.internal.policy.impl.PhoneWindow$DecorView.onWindowFocusChanged(PhoneWindow.java:2462)
04-09 09:11:18.178: E/AndroidRuntime(829): at android.view.View.dispatchWindowFocusChanged(View.java:7578)
04-09 09:11:18.178: E/AndroidRuntime(829): at android.view.ViewGroup.dispatchWindowFocusChanged(ViewGroup.java:962)
04-09 09:11:18.178: E/AndroidRuntime(829): at android.view.ViewRootImpl$ViewRootHandler.handleMessage(ViewRootImpl.java:3115)
04-09 09:11:18.178: E/AndroidRuntime(829): at android.os.Handler.dispatchMessage(Handler.java:99)
04-09 09:11:18.178: E/AndroidRuntime(829): at android.os.Looper.loop(Looper.java:137)
04-09 09:11:18.178: E/AndroidRuntime(829): at android.app.ActivityThread.main(ActivityThread.java:5103)
04-09 09:11:18.178: E/AndroidRuntime(829): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 09:11:18.178: E/AndroidRuntime(829): at java.lang.reflect.Method.invoke(Method.java:525)
04-09 09:11:18.178: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
04-09 09:11:18.178: E/AndroidRuntime(829): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
04-09 09:11:18.178: E/AndroidRuntime(829): at dalvik.system.NativeStart.main(Native Method)
and it's my class.
public class Activitygiris extends BaseGameActivity {
private static final int CAMERA_WIDTH=800;
private static final int CAMERA_HEIGHT=480;
private Camera camera;
private Engine engine;
Scene sahne;
private Texture texSaha,texOyuncu1,texOyuncu2,texRedWins,texBlueWins;
private TextureRegion texRegSaha,texRegOyuncu1,texRegOyuncu2,texRegRedWins,texRegBlueWins;
private Sprite spriteSaha,spriteOyuncu1,spriteOyuncu2,spriteRedWins,spriteBlueWins;
private TimerHandler timerBeklet;
#Override
public Engine onLoadEngine() {
// TODO Auto-generated method stub
camera= new Camera(0,0,CAMERA_WIDTH,CAMERA_HEIGHT);
final EngineOptions engineoptions = new EngineOptions(true,ScreenOrientation.LANDSCAPE,new FillResolutionPolicy(),camera);
engineoptions.getTouchOptions().setRunOnUpdateThread(true);
engine = new Engine(engineoptions);
return engine;
}
#Override
public void onLoadResources() {
// TODO Auto-generated method stub
texSaha=new Texture(1024,512,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texOyuncu1 = new Texture(128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texOyuncu2 = new Texture(128,128,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texRedWins = new Texture(64,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texBlueWins = new Texture(64,256,TextureOptions.BILINEAR_PREMULTIPLYALPHA);
texRegSaha = TextureRegionFactory.createFromAsset(texSaha, this, "gfx/Arkaplan.jpg",0,0);
texRegOyuncu1 = TextureRegionFactory.createFromAsset(texOyuncu1, this, "gfx/kol1.png",0,0);
texRegOyuncu2 = TextureRegionFactory.createFromAsset(texOyuncu2, this, "gfx/kol2.png",0,0);
texRegBlueWins = TextureRegionFactory.createFromAsset(texBlueWins, this, "gfx/bluewins.png",0,0);
texRegRedWins = TextureRegionFactory.createFromAsset(texRedWins, this, "gfx/redwins.png",0,0);
Texture [] textures = {texSaha,texOyuncu1,texOyuncu2,texBlueWins,texRedWins };
mEngine.getTextureManager().loadTextures(textures);
}
#Override
public Scene onLoadScene() {
// TODO Auto-generated method stub
this.engine.registerUpdateHandler(new FPSLogger());
this.sahne=new Scene();
spriteSaha = new Sprite(0, 0, texRegSaha);
spriteOyuncu1 = new Sprite(600, CAMERA_HEIGHT/2-64, texRegOyuncu1);
spriteOyuncu2 = new Sprite(50, CAMERA_HEIGHT/2-64, texRegOyuncu2);
spriteRedWins = new Sprite(CAMERA_HEIGHT-128,CAMERA_HEIGHT/2-128,texRegRedWins);
spriteRedWins = new Sprite(CAMERA_HEIGHT+32,CAMERA_HEIGHT/2-128,texRegBlueWins.clone());
spriteBlueWins.setVisible(false);
mEngine.registerUpdateHandler(timerBeklet = new TimerHandler(3, false,new ITimerCallback() {
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
// TODO Auto-generated method stub
spriteRedWins.setRotation(180);
mEngine.registerUpdateHandler(timerBeklet= new TimerHandler(3,false, new ITimerCallback() {
#Override
public void onTimePassed(TimerHandler pTimerHandler) {
// TODO Auto-generated method stub
spriteBlueWins.setVisible(true);
spriteRedWins.setVisible(false);
}
}));
}
}));
this.sahne.attachChild(spriteSaha);
this.sahne.attachChild(spriteOyuncu1);
this.sahne.attachChild(spriteOyuncu2);
this.sahne.attachChild(spriteBlueWins);
this.sahne.attachChild(spriteRedWins);
return this.sahne;
}
#Override
public void onLoadComplete() {
// TODO Auto-generated method stub
}
}
Help me, please..

You get a nullPointerException because the reference variable spriteBlueWin is referencing null.
You are not initializing the spriteBlueWins, you should initialize it as you did with the others.
As pointed by #Dirk Lachowski, you probably copied and pasted the spriteRedWins and forgot to replace it to spriteBlueWins:
spriteSaha = new Sprite(0, 0, texRegSaha);
spriteOyuncu1 = new Sprite(600, CAMERA_HEIGHT/2-64, texRegOyuncu1);
spriteOyuncu2 = new Sprite(50, CAMERA_HEIGHT/2-64, texRegOyuncu2);
spriteRedWins = new Sprite(CAMERA_HEIGHT-128,CAMERA_HEIGHT/2-128,texRegRedWins);
spriteRedWins = new Sprite(CAMERA_HEIGHT+32,CAMERA_HEIGHT/2-128,texRegBlueWins.clone());
The last line should be:
spriteBlueWins = new Sprite(CAMERA_HEIGHT+32,CAMERA_HEIGHT/2-128,texRegBlueWins.clone());

Related

Android : saving value for app language

I want language change feature in my app which can provide user feature to change application language from settings but at start i want default application to open so i created dummy project to test it where i am accessing Langugage preference from a class which is defining factor for language Here its code(Clearly I know I am not using sharedPreference the right way and i know the error was because of that so please help in setting shared preference in this app):
public class LanguagePreferences extends Activity {
public int language;
int check;
SharedPreferences settings = getSharedPreferences("mysettings",
Context.MODE_PRIVATE);
public LanguagePreferences()
{
language=settings.getInt("language", 0);
}
public void changeLang()
{
check=settings.getInt("language",0);
if(check==0)
{
settings.edit().putInt("language", 1);
settings.edit().comment();
}
else
{
settings.edit().putInt("language", 1);
settings.edit().comment();
}
}
}
and a button to change application Language in settings Here its Code:
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.change);
btnLang=(Button)findViewById(R.id.change);
btnLang.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LanguagePreferences l= new LanguagePreferences();
l.changeLang();
}
});
}
and main Activity where i am changing text value if language is different code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = (TextView) findViewById(R.id.text);
Button btnLang = (Button) findViewById(R.id.buttonlang);
SharedPreferences settings = getSharedPreferences("mysettings",
Context.MODE_PRIVATE);
btnLang.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent i = new Intent(MainActivity.this, Change.class);
startActivity(i);
}
});
LanguagePreferences l = new LanguagePreferences();
if (l.language == 0) {
tv.setText("Germany");
} else {
tv.setText("English");
}
}
I am using shared preferences to save data but at start activity closes and not responding error shows. Any Help is appreciated.
Error Log:
09-11 17:44:26.200: D/AndroidRuntime(13952): Shutting down VM
09-11 17:44:26.200: W/dalvikvm(13952): threadid=1: thread exiting with uncaught exception (group=0x414112a0)
09-11 17:44:26.208: E/AndroidRuntime(13952): FATAL EXCEPTION: main
09-11 17:44:26.208: E/AndroidRuntime(13952): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dummy/com.example.dummy.MainActivity}: java.lang.NullPointerException
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2097)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2122)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.access$600(ActivityThread.java:140)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1228)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.os.Handler.dispatchMessage(Handler.java:99)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.os.Looper.loop(Looper.java:137)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.main(ActivityThread.java:4895)
09-11 17:44:26.208: E/AndroidRuntime(13952): at java.lang.reflect.Method.invoke(Method.java:511)
09-11 17:44:26.208: E/AndroidRuntime(13952): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:994)
09-11 17:44:26.208: E/AndroidRuntime(13952): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:761)
09-11 17:44:26.208: E/AndroidRuntime(13952): at dalvik.system.NativeStart.main(Native Method)
09-11 17:44:26.208: E/AndroidRuntime(13952): Caused by: java.lang.NullPointerException
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.content.ContextWrapper.getApplicationContext(ContextWrapper.java:101)
09-11 17:44:26.208: E/AndroidRuntime(13952): at com.example.dummy.LanguagePreferences.<init>(LanguagePreferences.java:18)
09-11 17:44:26.208: E/AndroidRuntime(13952): at com.example.dummy.MainActivity.onCreate(MainActivity.java:36)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.Activity.performCreate(Activity.java:5163)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
09-11 17:44:26.208: E/AndroidRuntime(13952): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2061)
09-11 17:44:26.208: E/AndroidRuntime(13952): ... 11 more
09-11 17:44:43.325: I/Process(13952): Sending signal. PID: 13952 SIG: 9
As mentioned in the comments, most likely the settings variable is the problem.
Try initializing it in your onCreate() method.
#Override
protected void onCreate(Bundle state){
super.onCreate(state);
SharedPreferences settings = getSharedPreferences("mysettings", Context.MODE_PRIVATE);
}
You can find more information on how to use these shared preferences here.
From your example:
LanguagePreferences l = new LanguagePreferences();
You are creating a new Activity object here.
You're not supposed to do that. Activities are managed by android. You should use the lifecycle methods and callbacks to interact with them.
Store the language only in the shared preferences, not in a member variable of the activity.
You are Trying to create an instance of an Activity with at the line below, which is wrong.
LanguagePreferences l = new LanguagePreferences();
You cannot reach the activity's method like this. You can Add an Application Class and write a public static method like:
public class ApplicationClass extends Application {
public static SharedPreferences mSharedPrefs;
public static SharedPreferences.Editor mPrefsEditor;
private static ApplicationClass instance;
public ApplicationClass() {
instance = this;
}
#Override
public void onCreate() {
super.onCreate();
mSharedPrefs = getSharedPreferences("mysettings", MODE_PRIVATE);
mPrefsEditor = mSharedPrefs.edit();
}
public static SharedPreferences getmSharedPrefs() {
if (mSharedPrefs == null) {
mSharedPrefs = getContext().getSharedPreferences("mysettings",
MODE_PRIVATE);
}
return mSharedPrefs;
}
public static SharedPreferences.Editor getmPrefsEditor() {
if (mPrefsEditor == null || mSharedPrefs == null) {
mSharedPrefs = getContext().getSharedPreferences("mysettings",
MODE_PRIVATE);
mPrefsEditor = mSharedPrefs.edit();
}
return mPrefsEditor;
}
}
And you can use this like:
ApplicationClass.getmPrefsEditor().putString("key","value").commit();
And this:
ApplicationClass.getmSharedPrefs().getString("key");
in your Activities and Fragments.

public static hashmap clear and put not work properly [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
At first I click the “mn_column” button, then works properly. But I go back and click again “mn_column” or “mn_blog” then it show flowing error :
08-10 02:30:16.441: E/AndroidRuntime(1098): FATAL EXCEPTION: main
08-10 02:30:16.441: E/AndroidRuntime(1098): Process: com.sams.main.news, PID: 1098
08-10 02:30:16.441: E/AndroidRuntime(1098): java.lang.NullPointerException
08-10 02:30:16.441: E/AndroidRuntime(1098): at com.sams.main.news.MainNewsActivity$6.onClick(MainNewsActivity.java:539)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.view.View.performClick(View.java:4438)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.view.View$PerformClick.run(View.java:18422)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.os.Handler.handleCallback(Handler.java:733)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.os.Handler.dispatchMessage(Handler.java:95)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.os.Looper.loop(Looper.java:136)
08-10 02:30:16.441: E/AndroidRuntime(1098): at android.app.ActivityThread.main(ActivityThread.java:5017)
08-10 02:30:16.441: E/AndroidRuntime(1098): at java.lang.reflect.Method.invokeNative(Native Method)
08-10 02:30:16.441: E/AndroidRuntime(1098): at java.lang.reflect.Method.invoke(Method.java:515)
08-10 02:30:16.441: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
08-10 02:30:16.441: E/AndroidRuntime(1098): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
08-10 02:30:16.441: E/AndroidRuntime(1098): at dalvik.system.NativeStart.main(Native Method)
Picture:
Code:
MainNewsActivity.java
public class MainNewsActivity extends TabActivity {
String menuLink;
int menuKey;
private List<RssItem> menuItems = new ArrayList<RssItem>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
Button mnColumn = (Button) layout.findViewById(R.id.mn_column);
mnColumn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
menuKey = 0;
//Varialbes.menuNewsMap.clear();
menuLink = "http://www.aaa.org/rss/rssblog";
menuItems = pareser.getNewsList(menuLink);
Varialbes.menuNewsMap.put(menuKey, menuItems);
Intent intent = new Intent(MainNewsActivity.this,MenuNewsActivity.class);
intent.putExtra(Varialbes.PAPER, paper);
intent.putExtra(Varialbes.MENU_TITLE, "Column");
intent.putExtra(Varialbes.MENU_KEY, menuKey);
startActivity(intent);
}
});
Button mnBlog = (Button) layout.findViewById(R.id.mn_blog);
mnBlog.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
menuKey = 1;
menuLink = "http://www.bbb.org/rss/rssblog";
menuItems = pareser.getNewsList(menuLink);
Varialbes.menuNewsMap.put(menuKey, menuItems);
Intent intent = new Intent(MainNewsActivity.this,MenuNewsActivity.class);
intent.putExtra(Varialbes.PAPER, paper);
intent.putExtra(Varialbes.MENU_TITLE, "Blog");
intent.putExtra(Varialbes.MENU_KEY, menuKey);
startActivity(intent);
}
});
}
}
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DEE4DF"
android:orientation="vertical" >
<Button
android:id="#+id/mn_column"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.98"
android:background="#drawable/menu_selector"
android:gravity="left|center_vertical|center_horizontal"
android:text="Column" />
<Button
android:id="#+id/mn_blog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2.98"
android:background="#drawable/menu_selector"
android:gravity="left|center_vertical|center_horizontal"
android:text="Blog" />
</LinearLayout>
Varialbes.java
public class Varialbes {
public static final String PAPER = "paper";
public static final String MENU_TITLE = "title";
public static final String MENU_KEY = "menu_key";
public static HashMap<Integer, List<RssItem>> menuNewsMap = new HashMap<Integer, List<RssItem>>();
}
MenuNewsActivity.java
public class MenuNewsActivity extends Activity {
private ListView listNews;
private List<RssItem> items;
private Button backButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu_news);
// ..........................Back..........................
backButton = (Button) findViewById(R.id.back_button);
backButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent backIntent = new Intent(MenuNewsActivity.this,
MainNewsActivity.class);
finish();
startActivity(backIntent);
}
});
// ................................................
listNews = (ListView) findViewById(R.id.newsList);
Bundle bundle = getIntent().getExtras();
int paper = bundle.getInt(Varialbes.PAPER);
TextView tv = (TextView) findViewById(R.id.hedderText);
String menutitle = bundle.getString(Varialbes.MENU_TITLE);
int menuKey = bundle.getInt(Varialbes.MENU_KEY);
tv.setText(menutitle);
items = Varialbes.menuNewsMap.get(menuKey);
MenuNewsAdater adapter = new MenuNewsAdater(this,
Varialbes.ICONS[paper], items);
listNews.setAdapter(adapter);
}
}
Please help me.
You pareser variable does not properly. So you
add flowing code:
RssParser pareser= new RssParser();
Try this way:
public void onClick(View v) {
menuKey = 0;
Varialbes.menuNewsMap.clear();
menuLink = "http://www.aaa.org/rss/rssblog";
RssParser pareser= new RssParser();
menuItems = pareser.getNewsList(menuLink);
Varialbes.menuNewsMap.put(menuKey, menuItems);
Intent intent = new Intent(MainNewsActivity.this,MenuNewsActivity.class);
intent.putExtra(Varialbes.PAPER, paper);
intent.putExtra(Varialbes.MENU_TITLE, "Column");
intent.putExtra(Varialbes.MENU_KEY, menuKey);
startActivity(intent);
}

Update and remove GridView items at runtime

i am developing a project where i compare the images of two items,So if two items will have same image after clicking these items should be hide. my code is given below and this code encounter a problem. is this a logical error or any other issue? i try to solve the issue but did't resolve.. Please guide me... here is my main Activity.
MainActivity.java
public class MainActivity extends Activity {
Context ctx;
int imagesArray[];
ImageAdapter adapter;
List<Integer> pictures;
boolean flage = false;
GridView gridView;
int save1, save2;
int img1 = -1, img2 = -1;
public int OriginalArray[] = { R.drawable.sample_0, R.drawable.sample_1,
R.drawable.sample_2, R.drawable.sample_3, R.drawable.sample_0,
R.drawable.sample_1, R.drawable.sample_2, R.drawable.sample_3 };
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ImageView myimage = new ImageView(ctx);
gridView = (GridView) findViewById(R.id.gv_memory);
gridView.setAdapter(adapter);
shuffleArray();
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) {
// TODO Auto-generated method stub
myimage.setImageResource(pictures.get(position));
if (flage == false) {
img1 = pictures.get(position);
flage = true;
} else if (flage == true) {
img2 = pictures.get(position);
checkResult();
flage = false;
}
}
private void checkResult() {
// TODO Auto-generated method stub
if (img1 == img2) {
adapter.pictureList.remove(img1);
adapter.pictureList.remove(img2);
adapter.notifyDataSetChanged();
Toast.makeText(MainActivity.this, "Congratualatin !!!!",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this, "Sorry!!!!",
Toast.LENGTH_LONG).show();
}
}
});
}
private void shuffleArray() {
// TODO Auto-generated method stub
pictures = new ArrayList<Integer>();
for (int index = 0; index < OriginalArray.length; index++) {
pictures.add(OriginalArray[index]);
}
Collections.shuffle(pictures);
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter {
private Context context;
List<Integer> pictureList = new ArrayList<Integer>();
public ImageAdapter(Context c) {
context = c;
for (int i = 0; i < 8; i++) {
pictureList.add(R.drawable.question);
}
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return (pictureList.size());
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return pictureList.get(position);
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView myimage = new ImageView(context);
myimage.setImageResource(pictureList.get(position));
myimage.setScaleType(ImageView.ScaleType.CENTER_INSIDE);
myimage.setLayoutParams(new GridView.LayoutParams(70, 70));
return myimage;
}
}
LogCat.
03-14 06:09:03.793: D/dalvikvm(2877): GC_FOR_ALLOC freed 54K, 8% free 2771K/2996K, paused 111ms, total 117ms
03-14 06:09:03.802: I/dalvikvm-heap(2877): Grow heap (frag case) to 3.943MB for 1127536-byte allocation
03-14 06:09:03.922: D/dalvikvm(2877): GC_FOR_ALLOC freed 2K, 6% free 3870K/4100K, paused 118ms, total 118ms
03-14 06:09:03.962: D/AndroidRuntime(2877): Shutting down VM
03-14 06:09:03.962: W/dalvikvm(2877): threadid=1: thread exiting with uncaught exception (group=0x41465700)
03-14 06:09:03.972: E/AndroidRuntime(2877): FATAL EXCEPTION: main
03-14 06:09:03.972: E/AndroidRuntime(2877): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.memory/com.example.memory.MainActivity}: java.lang.NullPointerException
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.access$600(ActivityThread.java:141)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.os.Handler.dispatchMessage(Handler.java:99)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.os.Looper.loop(Looper.java:137)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.main(ActivityThread.java:5103)
03-14 06:09:03.972: E/AndroidRuntime(2877): at java.lang.reflect.Method.invokeNative(Native Method)
03-14 06:09:03.972: E/AndroidRuntime(2877): at java.lang.reflect.Method.invoke(Method.java:525)
03-14 06:09:03.972: E/AndroidRuntime(2877): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
03-14 06:09:03.972: E/AndroidRuntime(2877): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
03-14 06:09:03.972: E/AndroidRuntime(2877): at dalvik.system.NativeStart.main(Native Method)
03-14 06:09:03.972: E/AndroidRuntime(2877): Caused by: java.lang.NullPointerException
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.view.ViewConfiguration.get(ViewConfiguration.java:318)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.view.View.<init>(View.java:3264)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.widget.ImageView.<init>(ImageView.java:112)
03-14 06:09:03.972: E/AndroidRuntime(2877): at com.example.memory.MainActivity.onCreate(MainActivity.java:33)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.Activity.performCreate(Activity.java:5133)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-14 06:09:03.972: E/AndroidRuntime(2877): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
03-14 06:09:03.972: E/AndroidRuntime(2877): ... 11 more
03-14 06:14:04.503: I/Process(2877): Sending signal. PID: 2877 SIG: 9
You have
final ImageView myimage = new ImageView(ctx);
ctz is not initialized. Its only declared as Context ctx;
You have this
gridView.setAdapter(adapter);
But you need to intialize adapter before using the same
So change to
setContentView(R.layout.main);
final ImageView myimage = new ImageView(this); //this refers to Activity context
gridView = (GridView) findViewById(R.id.gv_memory);
adapter = new ImageAdapter(this)
gridView.setAdapter(adapter);

How to change Pin Number using sharedpreferences in Android?

I'm having implement android demo for application pin using shared preferences which i enter pin and confirm the pin.The problem is that when I am accessing this values, it is not returning updated values, it gives me NullPointerException.
String strGetPin;
public final String pref_filename = "settings";
String myPassword;
static SharedPreferences shaedPref;
public static String strdataReturned = "";
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings_list);
activity = this;
edtxt_EnterPin = (EditText) findViewById(R.id.edtxt_Pin);
edtxt_NewPin = (EditText) findViewById(R.id.edtxt_NewPin);
edtxt_ConfirmPin = (EditText) findViewById(R.id.edtxt_ConfirmPin);
shaedPref = getSharedPreferences(pref_filename, 0);
strdataReturned = shaedPref.getString("user", "");
if (strdataReturned.equals("")) {
SharedPreferences.Editor editor = shaedPref.edit();
editor.putString("user", "1234"); // newData is new pass,passwordKey is key
editor.commit();
}
btnChangePin = (Button) findViewById(R.id.btn_ChangePin);
btnChangePin.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
myDialog = new Dialog(Settings_Activity.this);
myDialog.setContentView(R.layout.pin_change);
myDialog.setTitle("Application PIN Change");
myDialog.setCancelable(false);
//Button For Change Pin save
Button btnChange = (Button) myDialog.findViewById(R.id.btn_Save);
btnChange.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String stringData = edtxt_EnterPin.getText().toString().trim();
strdataReturned = shaedPref.getString("user", ""); // key/def message
// if stored password is equal to entered password
if (strdataReturned.equals(stringData)) {
String newData = edtxt_NewPin.getText().toString().trim();
SharedPreferences.Editor editor = shaedPref.edit();
editor.putString("user", newData); // newData is new pass, passwordKey is key
editor.commit();
Toast.makeText(getApplicationContext(), "PIN Change successfully", Toast.LENGTH_LONG).show();
edtxt_EnterPin.setText("");
edtxt_NewPin.setText("");
} else {
Toast.makeText(getApplicationContext(), "Wrong Pin!", Toast.LENGTH_LONG).show();
edtxt_EnterPin.setText("");
edtxt_NewPin.setText("");
}
}
});
Button btnCancel = (Button) myDialog.findViewById(R.id.btn_Cancel);
btnCancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
myDialog.dismiss();
}
});
myDialog.show();
}
});
}
Logcat:
04-09 18:42:12.694: D/(29235): onClick: Stoping Service
04-09 18:42:12.781: E/(29235): Service Stopped
04-09 18:42:19.861: D/dalvikvm(29235): GREF has increased to 201
04-09 18:42:50.712: D/AndroidRuntime(29235): Shutting down VM
04-09 18:42:50.712: W/dalvikvm(29235): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-09 18:42:50.721: E/AndroidRuntime(29235): FATAL EXCEPTION: main
04-09 18:42:50.721: E/AndroidRuntime(29235): java.lang.NullPointerException
04-09 18:42:50.721: E/AndroidRuntime(29235): at com.sms_alert.Settings_Activity$1$1.onClick(Settings_Activity.java:95)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.view.View.performClick(View.java:2485)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.view.View$PerformClick.run(View.java:9080)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.os.Handler.handleCallback(Handler.java:587)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.os.Handler.dispatchMessage(Handler.java:92)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.os.Looper.loop(Looper.java:123)
04-09 18:42:50.721: E/AndroidRuntime(29235): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-09 18:42:50.721: E/AndroidRuntime(29235): at java.lang.reflect.Method.invokeNative(Native Method)
04-09 18:42:50.721: E/AndroidRuntime(29235): at java.lang.reflect.Method.invoke(Method.java:507)
04-09 18:42:50.721: E/AndroidRuntime(29235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-09 18:42:50.721: E/AndroidRuntime(29235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-09 18:42:50.721: E/AndroidRuntime(29235): at dalvik.system.NativeStart.main(Native Method)
04-09 18:42:53.932: I/Process(29235): Sending signal. PID: 29235 SIG: 9
Try clearing the SharedPreferences before writing to it.
editor.clear();

Code keeps thowing a NPE, cannot find the cause

i cannot find the null pointer exception in my code, i was wondering if anyone could help me find what is giving an error.
code:
package com.dingle.ubat;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PowerManager;
import android.view.Display;
import android.view.MotionEvent;
import android.widget.Toast;
public class UltraBrightAndroidTorchActivity extends Activity {
/** Called when the activity is first created. */
PowerManager pm;
PowerManager.WakeLock wl;
public boolean flash = false;
public boolean enableFlash = false;
public boolean dimDisplay = false;
Display display = getWindowManager().getDefaultDisplay();
public int windowWidth = display.getWidth();
public int windowHeight = display.getHeight();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
pm = (PowerManager) getSystemService(Context.POWER_SERVICE);
wl = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK, "My Tag");
flash = this.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if(flash == true && enableFlash == true){
try {
DroidLED led = new DroidLED();
led.enable(!led.isEnabled());
}
catch(Exception e) {
Toast.makeText(this, "Error interacting with LED.", Toast.LENGTH_SHORT).show();
throw new RuntimeException(e);
} }
wl.acquire();
}
#Override
public boolean onTouchEvent(MotionEvent event) {
//int tx = (int) event.getRawX();
int ty = (int) event.getRawY();
if (ty <= (windowHeight/2)){
dimDisplay = !dimDisplay;
}
if (ty >= (windowHeight/2)){
enableFlash = !enableFlash;
}
return super.onTouchEvent(event);
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
wl.release();
}
#Override
protected void onNewIntent(Intent intent) {
// TODO Auto-generated method stub
super.onNewIntent(intent);
wl.release();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
wl.release();
}
#Override
protected void onStop() {
// TODO Auto-generated method stub
super.onStop();
wl.release();
}
}
Error list:
12-10 20:40:42.224: W/dalvikvm(274): threadid=3: thread exiting with uncaught exception (group=0x4001b188)
12-10 20:40:42.232: E/AndroidRuntime(274): Uncaught handler: thread main exiting due to uncaught exception
12-10 20:40:42.282: E/AndroidRuntime(274): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.dingle.ubat/com.dingle.ubat.UltraBrightAndroidTorchActivity}: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2417)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.os.Handler.dispatchMessage(Handler.java:99)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.os.Looper.loop(Looper.java:123)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.main(ActivityThread.java:4363)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.reflect.Method.invokeNative(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.reflect.Method.invoke(Method.java:521)
12-10 20:40:42.282: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
12-10 20:40:42.282: E/AndroidRuntime(274): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
12-10 20:40:42.282: E/AndroidRuntime(274): at dalvik.system.NativeStart.main(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): Caused by: java.lang.NullPointerException
12-10 20:40:42.282: E/AndroidRuntime(274): at com.dingle.ubat.UltraBrightAndroidTorchActivity.<init>(UltraBrightAndroidTorchActivity.java:20)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.Class.newInstanceImpl(Native Method)
12-10 20:40:42.282: E/AndroidRuntime(274): at java.lang.Class.newInstance(Class.java:1479)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-10 20:40:42.282: E/AndroidRuntime(274): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2409)
12-10 20:40:42.282: E/AndroidRuntime(274): ... 11 more
any help and criticism is greatly appreciated
code is being compiled for android 2.1. code is a basic, crappily coded torch app.
thanx
The code is bailing out on one of these two lines:
Display display = getWindowManager().getDefaultDisplay();
public int windowWidth = display.getWidth();
Either getWindowManager() is returning a null and that first initializer will fail, or getDefaultDisplay() is, and the second one will.
You should probably move these initializations to the onCreate handler. Having them at instance initialization sounds a bit risky - the activity's "environment" might not be completely set up yet at that point.
(And check the return values.)
Activity is full available only after it has been created. This line does not do the intended purpose:
 Display display = getWindowManager().getDefaultDisplay();
Move this line and subsequent ones inside onCreate method.

Categories

Resources