Using the runOnUiThread to update Listview - java

I have written a thread that uses runOnUiThread to update the listview approximately every 500ms. This thread is written outside OnCreate(), but is called from OnCreate. Everything compiles fine, but I seem to be getting an error that tells me that my adapter is null, even though I use that same adapter to "initialize" the listview in OnCreate. I have posted both my code and my log. The main error that I run into, java.lang.NullPointerException, refers to line 454 of the MainActivity, which in this case is adapter.notifyDataSetChanged();. Any help would be greatly appreciated.
MainActivity
public class MainActivity extends Activity
{
// Sensor Constants
public static String temperature;
public static String humidity;
public static String lpg;
public static String alcohol;
public static int temperature_int;
public static int humidity_int;
public static int lpg_int;
public static int alcohol_int;
// Layout
ListView listView;
ItemAdapter adapter;
// USB
UsbManager USB_Manager;
UsbDevice Sense;
PendingIntent permission;
IntentFilter filter;
//BroadcastReceiver receiver;
//USBBuffer_s_received_data = 0;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize Interface
Model.LoadModel();
listView = (ListView) findViewById(R.id.listView);
String[] ids = new String[Model.Items.size()];
for (int i= 0; i < ids.length; i++)
{ids[i] = Integer.toString(i+1);}
ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids);
listView.setAdapter(adapter);
//USB
if ((UsbManager) getSystemService(Context.USB_SERVICE) != null)
{
USB_Manager = (UsbManager) getSystemService(Context.USB_SERVICE);
if (USB_Manager.getDeviceList().values().iterator().next() != null)
{
Sense = USB_Manager.getDeviceList().values().iterator().next();
if ((UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE) != null)
{
Sense = (UsbDevice) getIntent().getParcelableExtra(UsbManager.EXTRA_DEVICE);
}
}
}
// Update Layout
temperature_int = 30; humidity_int = 6; lpg_int = 5000; alcohol_int = 500;
temperature = String.valueOf(temperature_int);
humidity = String.valueOf(humidity_int);
lpg = String.valueOf(lpg_int);
alcohol = String.valueOf(alcohol_int);
Model.LoadModel();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
GUI_Update();
}
#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;
}
private void GUI_Update()
{
new Thread()
{
public void run()
{
while (true)
{
try
{
runOnUiThread(new Runnable()
{
#Override
public void run()
{
Model.LoadModel();
listView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
});
Thread.sleep(500);
}
catch (InterruptedException e)
{
}
}
}
}.start();
}
}
Log
12-21 14:39:02.109: D/AndroidRuntime(32704): Shutting down VM
12-21 14:39:02.109: W/dalvikvm(32704): threadid=1: thread exiting with uncaught exception (group=0x4198ac68)
12-21 14:39:02.109: E/AndroidRuntime(32704): FATAL EXCEPTION: main
12-21 14:39:02.109: E/AndroidRuntime(32704): Process: com.byrdonatwigge.sense, PID: 32704
12-21 14:39:02.109: E/AndroidRuntime(32704): java.lang.NullPointerException
12-21 14:39:02.109: E/AndroidRuntime(32704): at com.byrdonatwigge.sense.MainActivity$2$1.run(MainActivity.java:454)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.os.Handler.handleCallback(Handler.java:733)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.os.Handler.dispatchMessage(Handler.java:95)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.os.Looper.loop(Looper.java:136)
12-21 14:39:02.109: E/AndroidRuntime(32704): at android.app.ActivityThread.main(ActivityThread.java:5081)
12-21 14:39:02.109: E/AndroidRuntime(32704): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 14:39:02.109: E/AndroidRuntime(32704): at java.lang.reflect.Method.invoke(Method.java:515)
12-21 14:39:02.109: E/AndroidRuntime(32704): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:781)
12-21 14:39:02.109: E/AndroidRuntime(32704): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 14:39:02.109: E/AndroidRuntime(32704): at dalvik.system.NativeStart.main(Native Method)

#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize Interface
Model.LoadModel();
listView = (ListView) findViewById(R.id.listView);
String[] ids = new String[Model.Items.size()];
for (int i= 0; i < ids.length; i++)
{ids[i] = Integer.toString(i+1);}
// change following line:
this.adapter = new ItemAdapter(this,R.layout.row, ids);

You are creating a local variable adapter in your onCreate() and assigning a new ItemAdapter to it.
ItemAdapter adapter = new ItemAdapter(this,R.layout.row, ids);
This is called variable shadowing; that variable has absolutely nothing to do with the instance field adapter that you're trying to use in GUI_Update() .. which is why it's null there, and throws the NPE.
Your IDE should actually be pointing that out to you.

Related

Exception when trying to send a message to firebase server

Can you please find out what is wrong with this code?
public class NewMessageActivity extends AppCompatActivity {
private DatabaseReference mDatabaseReference;
String mDisplayName = "John";
EditText mNewMessageField;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final EditText mNewMessageField = (EditText) findViewById(R.id.newMessageText);
setContentView(R.layout.activity_new_message);
ImageButton mSendButton = (ImageButton) findViewById(R.id.sendButton);
mSendButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
sendMessage();
finish();
}
});
}
private void sendMessage(){
mDatabaseReference= FirebaseDatabase.getInstance().getReference();
String input = mNewMessageField.getText().toString();
SingleMessage singleMessage = new SingleMessage(input, mDisplayName);
mDatabaseReference.child("messages").push().setValue(singleMessage);
}
}
Immediately after I press the send button, the app stops working and I get this error message:
FATAL EXCEPTION: main
java.lang.NullPointerException
at com..NewMessageActivity.sendMessage(NewMessageActivity.java:42)
at com..NewMessageActivity.access$000(NewMessageActivity.java:14)
at com.***.NewMessageActivity$1.onClick(NewMessageActivity.java:33)
at android.view.View.performClick(View.java:4209)
at android.view.View$PerformClick.run(View.java:17457)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5341)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:929)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:696)
at dalvik.system.NativeStart.main(Native Method)
11-28 20:05:51.566 508-527/? E/AppErrorDialog: Failed to get ILowStorageHandle instance
You need to call setContentView() before you search for the EditText widget. You also must use the instance of mNewMessageField declared at package level. Don't declare a new instance in onCreate().
setContentView(R.layout.activity_new_message);
mNewMessageField = (EditText) findViewById(R.id.newMessageText); // <= CHANGED
if (mNewMessageField == null) {
System.out.println("mNewMessageField is NULL");
}
To get more clues, add this debug output:
private void sendMessage(){
mDatabaseReference= FirebaseDatabase.getInstance().getReference();
if (mNewMessageField == null) {
System.out.println("mNewMessageField is NULL");
}
Editable ed = mNewMessageField.getText();
if (ed == null) {
System.out.println("Editable is NULL");
}
String input = mNewMessageField.getText().toString();
System.out.println("input=" + input);
SingleMessage singleMessage = new SingleMessage(input, mDisplayName);
mDatabaseReference.child("messages").push().setValue(singleMessage);
}

NoClassDefFoundError Exception when launching app in GenyMotion emulator

I wanted to test my code on another device than my physical one, so I launched the app in GenyMotion emulator with a Samsung Galaxy S3 (API 18), but it keeps throwing a NoClassDefFoundError Exception in my class "SlidingMenuUtil" (a customized drawer menu) which is called on startup by my MainActivity.
Here is code from my onCreate in MainActivity:
#Bind(R.id.viewContentFullScreen) RelativeLayout viewContentFullScreen;
#Bind(R.id.viewContentTopBar) RelativeLayout viewContentTopBar;
#Bind(R.id.topBarWrapper) RelativeLayout topbarView;
private ViewContainer viewContainer;
private SlidingMenuUtil leftMenu;
private Bundle bundle;
private MessageHandler messageHandler;
private GoBackFunction currentGoBackFunction;
private CallbackManager callbackManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
bundle = savedInstanceState;
setContentView(R.layout.activity_main);
leftMenu = new SlidingMenuUtil(this, SlidingMenuUtil.MenuType.LEFT, R.layout.drawer_menu, (int)(LayoutUtil.getScreenWidth(this) * 0.75), false);
populateMenu();
ButterKnife.bind(this);
messageHandler = new MessageHandler(this, findViewById(R.id.spinner));
FacebookSdk.sdkInitialize(getApplicationContext());
callbackManager = CallbackManager.Factory.create();
}
The problem occurs in the SlidingMenuUtil class on line: 67. Here is the constructor for the class:
public SlidingMenuUtil(Activity activity, MenuType menuType, int menuLayout, int shownMenuWidth, boolean fadeEffectOn) {
this.activity = activity;
this.menuType = menuType;
this.shownMenuWidth = shownMenuWidth;
this.fadeEffectOn = fadeEffectOn;
this.screenWidth = LayoutUtil.getScreenWidth(activity);
this.rootView = (ViewGroup)((ViewGroup)activity.findViewById(android.R.id.content)).getChildAt(0);
this.activityWrapper = new RelativeLayout(activity);
this.activityWrapper.setLayoutParams(this.rootView.getLayoutParams());
this.overlay = new RelativeLayout(activity);
this.overlay.setLayoutParams(new ViewGroup.LayoutParams(-1, -1));
this.overlay.setBackgroundColor(Color.parseColor("#000000"));
this.overlay.setAlpha(0.0F);
this.overlay.setVisibility(View.GONE);
this.overlay.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (menuOpen) {
toggle(new ToggleMenu() {
#Override
public void animationDone() {
}
});
}
}
});
this.rootView.addView(this.overlay);
this.menu = (LinearLayout)activity.getLayoutInflater().inflate(menuLayout, (ViewGroup) null, false);
this.menu.setLayoutParams(new ViewGroup.LayoutParams(shownMenuWidth, -1));
if (menuType == MenuType.LEFT) {
this.menu.setTranslationX((float)(-shownMenuWidth));
} else {
this.menu.setTranslationX((float)(screenWidth));
}
this.rootView.addView(this.menu);
this.menuOpen = false;
}
lin 67 is:
this.overlay.setOnClickListener(new View.OnClickListener() {
As mentioned before the problem only occurs in the emulator.
Here is the log:
812-812/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NoClassDefFoundError: nightout.dk.nightoutandroid.utils.SlidingMenuUtil$1
at nightout.dk.nightoutandroid.utils.SlidingMenuUtil.<init>(SlidingMenuUtil.java:67)
at nightout.dk.nightoutandroid.MainActivity.onCreate(MainActivity.java:40)
at android.app.Activity.performCreate(Activity.java:5133)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5103)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
at dalvik.system.NativeStart.main(Native Method)
I hope somone can help me out.
Any help will be gratly appreciated

Shared Preferences Null Pointer Exception

I am working on adding Shared Preferences to my Note taking Android App and I am running into a peculiar null pointer exception. I am currently testing settings for font size and typeface in my app and have been able to successfully save the shared preferences for both but am having trouble with the retrieval part. When the settings are changed, they successfully change the font and font size on a Fragment for the remainder of time the app is opened but I cannot restore them if the app is restarted.
First weird Null Pointer is in the onCreate.
OnCreate:
//create a new note fragment if one has not been created yet
mNoteFragment = (NoteFragment) getFragmentManager().findFragmentById(R.id.container);
if (mNoteFragment == null) {
mNoteFragment = new NoteFragment();
getFragmentManager().beginTransaction()
.replace(R.id.container, mNoteFragment).commit();
}
//restore SharedPreferences
SharedPreferences sharedPrefs = getPreferences(0);
int stylePref = sharedPrefs.getInt(SharedPreferanceConstants.PREF_FONT_SIZE, 2);
String fontPref = sharedPrefs.getString(SharedPreferanceConstants.PREF_TYPEFACE, "");
Log.e("STYLEID", String.valueOf(stylePref));
Log.e("FONTTYPE", fontPref);
onStyleChange(null , stylePref); //NULL POINTER EXCEPTION HERE
onFontChange(null, fontPref);
The logs output "3" and "Impact" which are the correct values for size and font which indicates that stylePref and fontPref are not null.
The next Null Pointer is below.:
#Override
public void onStyleChange(CustomStyleDialogFragment dialog, int styleId) {
Log.d("NOTEFRAGMENT", String.valueOf(mNoteFragment));
mNoteFragment.setCustomStyle(styleId); //NULL POINTER EXCEPTION HERE
}
#Override
public void onFontChange(CustomStyleDialogFragment dialog, String fontName) {
mNoteFragment.setCustomFont(fontName);
}
I have tested logging the value of styleId and got "3" so that doesn't seem to be the issue. mNoteFragment isnt null either based on the log.
Here is the third NP Exception in the NoteFragment.java. This is where I ultimately set the EditText view to the desired settings. I had no trouble changing font and size without shared preferences so I am not sure the issue here.
public void setCustomFont(String fontName) {
if(fontName.equals("Helvetica")) {
mEditText.setTypeface(mHelvetica);
}
else if(fontName.equals("Helvetica-Neue")) {
mEditText.setTypeface(mHelveticaNeue);
}
else if(fontName.equals("Impact")) {
mEditText.setTypeface(mImpact);
}
else {
mEditText.setTypeface(Typeface.DEFAULT);
}
}
public void setCustomStyle(int styleId) {
if(styleId == 1) {
mEditText.setTextAppearance(getActivity(), android.R.style.TextAppearance_Small);
}
else if(styleId == 2) {
mEditText.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
}
else if(styleId == 3) {
//NULL POINTER EXCEPTION HERE
mEditText.setTextAppearance(getActivity(), android.R.style.TextAppearance_Large);
}
}
And of course, here is the logcat. Thanks in advance!
11-18 10:31:53.030 18966-18966/com.richluick.blocnotes I/Process﹕ Sending signal. PID: 18966 SIG: 9
11-18 10:35:32.838 19248-19248/com.richluick.blocnotes D/STYLEID﹕ 3
11-18 10:35:32.838 19248-19248/com.richluick.blocnotes D/FONTTYPE﹕ Impact
11-18 10:35:32.839 19248-19248/com.richluick.blocnotes D/ERROR﹕ NoteFragment{422972f8 id=0x7f090001}
11-18 10:35:32.840 19248-19248/com.richluick.blocnotes D/AndroidRuntime﹕ Shutting down VM
11-18 10:35:32.840 19248-19248/com.richluick.blocnotes W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x4197dd40)
11-18 10:35:32.842 19248-19248/com.richluick.blocnotes E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.richluick.blocnotes, PID: 19248
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.richluick.blocnotes/com.richluick.blocnotes.BlocNotes}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
at android.app.ActivityThread.access$800(ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5097)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.richluick.blocnotes.NoteFragment.setCustomStyle(NoteFragment.java:86)
at com.richluick.blocnotes.BlocNotes.onStyleChange(BlocNotes.java:139)
at com.richluick.blocnotes.BlocNotes.onCreate(BlocNotes.java:61)
at android.app.Activity.performCreate(Activity.java:5248)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
            at android.app.ActivityThread.access$800(ActivityThread.java:139)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5097)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
            at dalvik.system.NativeStart.main(Native Method)
NoteFragment.java
public class NoteFragment extends Fragment {
public EditText mEditText;
private Typeface mHelvetica;
private Typeface mHelveticaNeue;
private Typeface mImpact;
private static final String TEXT = "text";
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
savedInstanceState.putString(TEXT, mEditText.getText().toString());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_note, container, false);
setHasOptionsMenu(true);
//restore state of app when activity is destroyed and restarted
mEditText = (EditText) rootView.findViewById(R.id.editText);
if (savedInstanceState != null) {
mEditText.setText(savedInstanceState.getString(TEXT));
}
//Store the font assets as variables
mHelvetica = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Helvetica_Reg.ttf");
mHelveticaNeue = Typeface.createFromAsset(getActivity().getAssets(), "fonts/HelveticaNeue_Lt.ttf");
mImpact = Typeface.createFromAsset(getActivity().getAssets(), "fonts/impact.ttf");
return rootView;
}
/**
* This is a setter method for setting the font the user has selected from the spinner
*
* param fontName the name of the font the user selected
* #return void
* */
public void setCustomFont(String fontName) {
if(fontName.equals("Helvetica")) {
mEditText.setTypeface(mHelvetica);
}
else if(fontName.equals("Helvetica-Neue")) {
mEditText.setTypeface(mHelveticaNeue);
}
else if(fontName.equals("Impact")) {
mEditText.setTypeface(mImpact);
}
else {
mEditText.setTypeface(Typeface.DEFAULT);
}
}
/**
* This is a setter method for setting the font style the user has selected from custom menu
*
* param styleId the integer id of the font stlye selected (SMALL, MEDIUM, LARGE)
* #return void
* */
public void setCustomStyle(int styleId) {
if(styleId == 1) {
mEditText.setTextAppearance(getActivity(), android.R.style.TextAppearance_Small);
}
else if(styleId == 2) {
mEditText.setTextAppearance(getActivity(), android.R.style.TextAppearance_Medium);
}
else if(styleId == 3) {
Log.d("EDITTEXT", String.valueOf(mEditText));
mEditText.setTextAppearance(getActivity(), android.R.style.TextAppearance_Large);
}
}
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate the menu; this adds items to the action bar if it is present.
super.onCreateOptionsMenu(menu, inflater);
inflater = getActivity().getMenuInflater();
inflater.inflate(R.menu.bloc_notes, menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_erase) {
mEditText.setText("");
}
return super.onOptionsItemSelected(item);
}
}
In your SomeActivity.onCreate() you are creating an instance of NoteFragment, but even if you commit it via a transaction, it won't be inflated and created right away. Which basically means, the NoteFragment.onCreateView() was not executed and the fragments UI is not yet created. However, your Activity.onCreate() assumes, that mFragment.mEditText is already set, which is not the case, so you run into a NullPointerException.
Take a look at the FragmentTransation.commit method: http://developer.android.com/reference/android/support/v4/app/FragmentTransaction.html#commit()
Schedules a commit of this transaction. The commit does not happen immediately; it will
be scheduled as work on the main thread to be done the next time that thread is ready.
Your mEditText in your NoteFragment is null at this time, hence the NPE.
Just init your member mEditText in onCreate() or onActivityCreated() of the fragment, not onCreateView(). That should ensure that mEditText is not null when you call your setters.
Whatever happens, you should be protecting against a null mEditText in your setter functions.
EDIT this is what I meant:
public void setCustomFont(String fontName) {
if(mEditText != null){
if(fontName.equals("Helvetica")) {
mEditText.setTypeface(mHelvetica);
}
else if(fontName.equals("Helvetica-Neue")) {
mEditText.setTypeface(mHelveticaNeue);
}
else if(fontName.equals("Impact")) {
mEditText.setTypeface(mImpact);
}
else {
mEditText.setTypeface(Typeface.DEFAULT);
}
}
}
EDIT 2: Oncreate will not be a good place. Basically you want to call your setter after the fragmentmanager has committed your change, so that mEditText has been initialized.
EDIT 3 - Try this with your initial code:
getFragmentManager().beginTransaction().replace(R.id.container, mNoteFragment).commit();
getFragmentManager().executePendingTransactions();
Okay so I am not sure why this works but I was able to figure out the anwer based on this question:
onCreateView() in Fragment is not called immediately, even after FragmentManager.executePendingTransactions()
I added the following line
getFragmentManager().executePendingTransactions();
and then moved everthing to the onStart method
#Override
protected void onStart() {
super.onStart();
//create a new note fragment if one has not been created yet
mNoteFragment = (NoteFragment) getFragmentManager().findFragmentById(R.id.container);
if (mNoteFragment == null) {
mNoteFragment = new NoteFragment();
getFragmentManager().beginTransaction().replace(R.id.container, mNoteFragment).commit();
getFragmentManager().executePendingTransactions();
}
//restore SharedPreferences
SharedPreferences sharedPrefs = getPreferences(0);
int stylePref = sharedPrefs.getInt(SharedPreferanceConstants.PREF_FONT_SIZE, 2);
String fontPref = sharedPrefs.getString(SharedPreferanceConstants.PREF_TYPEFACE, "");
Log.d("STYLEID", String.valueOf(stylePref));
Log.d("FONTTYPE", fontPref);
onStyleChange(null , stylePref);
onFontChange(null, fontPref);
}
and for some reason it works fine now. If anyone could explain why that would be great
Try use 'apply' instead of 'commit' , the justification is here -> What's the difference between commit() and apply() in Shared Preference by Lukas Knuth.
But basically, apply() will commits its changes to the in-memory SharedPreferences immediately.
It's strong recommended use a class to manager your preferences, like 'MyPreferencesManager'.

nullPointerException at getWritableDatabase()

New to Android here. I've made a new Fragments app, and I'm having trouble accessing the database from the Fragment.
Here's the class:
public class ShopListActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
DatabaseHelper db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_shoplist);
db = new DatabaseHelper(this); // tried getApplicationContext(), and getBaseContext() as well
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
public void onClickAddProduct(View view) {
TextView productName = (TextView) findViewById(R.id.add_product_text);
SQLiteDatabase dbWritable = db.getWritableDatabase(); // <-- crashes here
ContentValues values = new ContentValues();
values.put("Name", (String) productName.getText());
dbWritable.insert("Lists", "Name", values);
}
All the other solutions I found on SO didn't help.
DatabaseHelper is a very basic DB Helper class:
public DatabaseHelper(Context context) {
super(context, dbName, null, dbVersion);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_TABLES);
db.execSQL("INSERT INTO Lists (Name) VALUES('Test')");
}
Here's the Logcat:
07-07 21:03:24.351 9641-9641/com.chas.shopforme E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IllegalStateException: Could not execute method of the activity
at android.view.View$1.onClick(View.java:3606)
at android.view.View.performClick(View.java:4211)
at android.view.View$PerformClick.run(View.java:17362)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5227)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at android.view.View$1.onClick(View.java:3601)
... 11 more
Caused by: java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.chas.shopforme.ShopListActivity.onClickAddProduct(ShopListActivity.java:69)
... 14 more
Note: I know there are more questions like this, but their solutions didn't work for me.
try this
db = new DatabaseHelper(this.getApplicationContext());
I tried adding this as a comment to your question, but nasty things happen to code in comments.
Assuming you've tried a) in the comment above, and it didn't work, an example of b) follows:
protected void onCreate(Bundle savedInstanceState) {
...
Handler hand = new Handler();
hand.post(new Runnable() {
public void run() {
dbInit();
}
});
}
public void dbInit() {
db = new DatabaseHelper(this);
}
This allows the fragment manager to finish setting up the fragment before you try to use it. What it is doing is sending a message to itself which is processed when the fragment's message handler loop is running.

ImageLoader NullPointerException for an off-screen, SlidingMenu item?

I'm parsing a JSON response from a weather feed and the data I receive includes a url to an icon that corresponds with the conditions, then I display that icon in an ImageView. I was able to make this work in a sandbox app before moving the package of code into my project.
In the main project I use the latest versions of ActionBarSherlock and SlidingMenu for navigation, and both apps make use of Android-Async-Http-Client but now it has a fatal NullPointerException when trying to display the icon from that url in the ImageView. I don't think this is caused by any of the libraries.
I've seen that sliding menus mostly consist of some type of ListView handled with an Adapter, which I'm doing in a portion of my sliding menu. But the only clue I have is that it crashes because now it's an off-screen UI element that I'm trying to work with. Does it matter that I have something in my sliding menu that isn't handled by an Adapter? I can't imagine much else is different but maybe you guys some clue of what I'm missing. Hopefully it isn't something basic that I'm just forgetting... or maybe it is :)
Alright, thanks for any ideas.
public class MainActivity extends SlidingActivity implements
ActionBar.OnNavigationListener {
final String TAG = "MainActivity";
private TextView mSelected;
private String[] mLocations;
ImageLoader imageLoader;
ImageView weatherIcon;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageLoader = new ImageLoader(this);
wireViews();
getWeatherFeed();
Context context = getSupportActionBar().getThemedContext();
ArrayAdapter<CharSequence> list = ArrayAdapter.createFromResource(
context, R.array.activities, R.layout.sherlock_spinner_item);
list.setDropDownViewResource(R.layout.sherlock_spinner_dropdown_item);
getSupportActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getSupportActionBar().setListNavigationCallbacks(list, this);
getSupportActionBar().setDisplayShowTitleEnabled(false);
// set the Behind View
setBehindContentView(R.layout.menu_frame);
// SlidingMenu
SlidingMenu sm = getSlidingMenu();
sm.setShadowWidthRes(R.dimen.shadow_width);
sm.setShadowDrawable(R.drawable.shadow);
sm.setBehindOffsetRes(R.dimen.slidingmenu_offset);
sm.setFadeDegree(0.35f);
sm.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
setSlidingActionBarEnabled(false);
ListView menuList = (ListView) findViewById(R.id.menuList);
SlidingMenuItem[] data = new SlidingMenuItem[] {
new SlidingMenuItem("Item1 Label", "Item1 Sub-label",getResources().getDrawable(R.drawable.item1)),
new SlidingMenuItem("Item2 Label", "Item2 Sub-Label",getResources().getDrawable(R.drawable.item2)) };
MenuAdapter adapter = new MenuAdapter(this, R.layout.sliding_menu_item,
data);
menuList.setAdapter(adapter);
}
private void updateWeatherUi(String degreeText, String descriptionText,
String iconUrl) {
Log.i(TAG, "Updating UI elements...");
if (imageLoader != null) {
Log.i(TAG, "IMAGE_LOADER NOT NULL!!!");
}
imageLoader.DisplayImage(iconUrl, weatherIcon); // FIXME: FATAL CRASH HERE !!!
}
/** Build a Weather object from the JSON response.
* #throws JSONException */
private void buildWeatherObject(String rawResponse) throws JSONException {
Log.i(TAG, "Building Weather ojbect...");
JSONObject baseObject = new JSONObject(rawResponse);
JSONObject data = new JSONObject(baseObject.getString(Key.DATA));
JSONArray conditionArray = new JSONArray(
data.getString(Key.CURRENT_CONDITION));
for (int i = 0; i < conditionArray.length(); i++) {
JSONObject conditionElement = new JSONObject(
conditionArray.getString(i));
weather = new Weather();
weather.tempMaxF = conditionElement.getString(Key.TEMP_F);
JSONArray weatherDescriptionArray = new JSONArray(
conditionElement.getString(Key.W_WEATHER_DESC));
for (int j = 0; j < weatherDescriptionArray.length(); j++) {
JSONObject weatherDescriptionElement = new JSONObject(
weatherDescriptionArray.getString(j));
weather.weatherDesc = weatherDescriptionElement
.getString(Key.VALUE);
}
JSONArray weatherIconArray = new JSONArray(
conditionElement.getString(Key.WEATHER_ICON_URL));
for (int j = 0; j < weatherIconArray.length(); j++) {
JSONObject weatherIconElement = new JSONObject(
weatherIconArray.getString(j));
weather.weatherIconUrl = weatherIconElement
.getString(Key.VALUE);
Log.i(TAG, weather.weatherIconUrl);
}
conditionElement = null;
updateWeatherUi(weather.tempMaxF, weather.weatherDesc,
weather.weatherIconUrl);
}
}
/** Asynchronously request and receive weather feed data. */
private void getWeatherFeed() {
Log.i(TAG, "Getting asynchronous JSON feed...");
AsyncHttpClient client = new AsyncHttpClient();
client.get(WEATHER_FEED_URL, new AsyncHttpResponseHandler() {
#Override
public void onSuccess(String response) {
try {
buildWeatherObject(response);
} catch (JSONException e) {
e.printStackTrace();
}
}
#Override
public void onFailure(Throwable arg0, String arg1) {
super.onFailure(arg0, arg1);
showToast("Sorry, the weather forecast wasn't available just then.");
}
});
}
private void wireViews() {
Log.i(TAG, "Wiring UI elements...");
mSelected = (TextView) findViewById(R.id.text);
mLocations = getResources().getStringArray(R.array.activities);
weatherIcon = (ImageView) findViewById(R.id.weatherIconView);
}
12-21 01:01:11.130: I/MainActivity(28502): Building Weather ojbect...
12-21 01:01:11.170: I/MainActivity(28502): http://www.worldweatheronline.com/images/wsymbols01_png_64/wsymbol_0001_sunny.png
12-21 01:01:11.170: I/MainActivity(28502): Updating UI elements...
12-21 01:01:11.170: I/MainActivity(28502): IMAGE_LOADER NOT NULL!!!
12-21 01:01:11.170: D/AndroidRuntime(28502): Shutting down VM
12-21 01:01:11.170: W/dalvikvm(28502): threadid=1: thread exiting with uncaught exception (group=0x4001d5a0)
12-21 01:01:11.180: E/AndroidRuntime(28502): FATAL EXCEPTION: main
12-21 01:01:11.180: E/AndroidRuntime(28502): java.lang.NullPointerException
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.eric.app.networkxml.ImageLoader.DisplayImage(ImageLoader.java:42)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.eric.app.MainActivity.updateWeatherUi(MainActivity.java:129)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.eric.app.MainActivity.buildWeatherObject(MainActivity.java:172)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.eric.app.MainActivity.access$0(MainActivity.java:137)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.eric.app.MainActivity$1.onSuccess(MainActivity.java:195)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.loopj.android.http.AsyncHttpResponseHandler.handleSuccessMessage(AsyncHttpResponseHandler.java:160)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.loopj.android.http.AsyncHttpResponseHandler.handleMessage(AsyncHttpResponseHandler.java:173)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.loopj.android.http.AsyncHttpResponseHandler$1.handleMessage(AsyncHttpResponseHandler.java:85)
12-21 01:01:11.180: E/AndroidRuntime(28502): at android.os.Handler.dispatchMessage(Handler.java:99)
12-21 01:01:11.180: E/AndroidRuntime(28502): at android.os.Looper.loop(Looper.java:150)
12-21 01:01:11.180: E/AndroidRuntime(28502): at android.app.ActivityThread.main(ActivityThread.java:4263)
12-21 01:01:11.180: E/AndroidRuntime(28502): at java.lang.reflect.Method.invokeNative(Native Method)
12-21 01:01:11.180: E/AndroidRuntime(28502): at java.lang.reflect.Method.invoke(Method.java:507)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-21 01:01:11.180: E/AndroidRuntime(28502): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-21 01:01:11.180: E/AndroidRuntime(28502): at dalvik.system.NativeStart.main(Native Method)
Yikes! I tried to post a "snippet" but maybe I should've posted the complete file...
Putting the updateWeatherUi() logic at inside the end of buildWeatherObject() fixed this for me.
The problem is that I'm still not sure what was occurring here previously. The Logs were showing the proper data right up to the point of imageLoader.DisplayImage(String, ImageView) in my original code but I kept crashing there with NullPointerException.

Categories

Resources