score:70
I'm almost sure that this is caused when the thread finish its work but the activity is no longer visible.
You should check if the getActivity()
call return null, and ...
To apply corrections on your code, look at this:
// (Calendar) Date function - Displays dateview on Card
final boolean keepRunning1 = true;
Thread thread_two = new Thread(){
@Override
public void run(){
while(keepRunning1){
// Make the thread wait half a second. If you want...
try {
Thread.sleep(500);
} catch (InterruptedException e) {
Toast.makeText(getActivity().getApplicationContext(), "Default Signature Fail", Toast.LENGTH_LONG).show();
e.printStackTrace();
}
// here you check the value of getActivity() and break up if needed
if(getActivity() == null)
return;
getActivity().runOnUiThread(new Runnable(){
@Override
public void run(){
TextView date = (TextView) getView().findViewById(R.id.date);
date.setText(DateUtils.formatDateTime(getActivity().getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
}
});
}
}
};thread_two.start();
score:5
Try this one
TextView date = (TextView) getView().findViewById(R.id.date);
is date is null or not check
if(date !=null){
date.setText(DateUtils.formatDateTime(getActivity().getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
}
score:8
The reason for the NPE is that your thread is not bound to the fragment lifecycle. Once the fragment is detached from its hosting activity, getActivity()
returns null.
As a solution, consider removing the thread altogether and just use postDelayed()
on a Handler
on the UI thread to post Runnable
s that do the updates you want after a delay.
score:10
Put
if(getActivity() == null)
return;
before getActivity().runOnUiThread(new Runnable(){
that way when the back button is closed and your Thread
is still running it will check whether the calling Activity
still exists.
If it does not it will return
.
score:21
After pressing back, your background thread is still running. By the time that thread reaches the getActivity().runOnUiThread()
code, the activity no longer exists. Check if the activity still exists like so:
if (getActivity() != null) {
getActivity().runOnUiThread(new Runnable(){
@Override
public void run(){
TextView date = (TextView) getView().findViewById(R.id.date);
date.setText(DateUtils.formatDateTime(getActivity().getBaseContext(), System.currentTimeMillis(),DateUtils.FORMAT_SHOW_WEEKDAY | DateUtils.FORMAT_SHOW_DATE | DateUtils.FORMAT_SHOW_YEAR));
}
});
}
Source: stackoverflow.com
Related Query
- Exporting SWT with JavaFX WebView component as Runnable JAR from Eclipse results in NullPointerException
- Java Eclipse: Difference between exporting as a JAR and exporting as a Runnable JAR
- What is the difference between runnable jar library handling options?
- this.getClass().getClassLoader().getResource("...") and NullPointerException
- JavaScript validator throws NullPointerException in Eclipse
- "Launch Configuration" Shows up Blank When Trying to Export Runnable Jar?
- NullPointerException on getActivity().runOnUiThread(new Runnable(){
- Eclipse exported Runnable JAR not showing images
- Eclipse cannot create runnable jar - No resources selected
- How do I create a runnable JAR in IntelliJ as I would in Eclipse
- Constant 'Running Android Lint' Failed, nullpointerexception popping up in Eclipse (but doesn't seem to be my code)
- NullPointerException in eclipse in Eclipse itself at PartServiceImpl.internalFixContext
- Javadoc for Android project in Eclipse fails with NullPointerException
- Eclipse throws NullPointerException during Maven update
- JDI Event Dispatch nullpointerexception
- Eclipse - Extract/package the required libraries into the same Runnable JAR
- Runnable Jar cannot find Resources and Other Libraries
- How to make a runnable jar for an application that uses JavaFX without native installers
- Why does Eclipse export my entire project as runnable JAR file?
- Maven and Eclipse : loading default properties in maven library project and use it in runnable Jar
- Do runnable jars (via Eclipse) contain tracking information?
- Exporting to Runnable jar with extra native code libraries in eclipse
- Facebook login NullPointerException
- Eclipse: include source code while exporting as runnable jar
- Runnable JARs missing Images/Files (Resources)
- Google app engine nullpointerexception when trying to fetch list instead of searching by primary key
- No Ceylon runnable element
- Eclipse create runnable jar problem
- java.lang.exception no runnable methods junit
- Export JavaFX Project to a runnable using Eclipse
More Query from same tag
- Could not calculate build plan :artifact org.apache.maven.plugins:maven-resources-plugin:pom:2.4.3 is not available in the local repository
- NoSuchElementException problem in java eclipse
- Apache Tomcat 8: Getting Error while creating new Server
- How to export all dependency external libraries into a folder in eclipse automatically
- Bring back the original project from a WSDL file or a WAR file
- AVD: Is there an inspector?
- ClassNotFoundException: com.itextpdf.text.Element
- Cannot Run As a c++ project in eclipse
- print function in Python3
- My CSS Stylesheet Isn't Linking
- How do I show syntax coloring and scoping on Eclipse IDE?
- Eclipse : ScheduledExecutorService.scheduleWithFixedDelay does not remove on publish
- your project contains errors Eclipse Red Exclamation Mark At Project
- Can't get my AdMob ads to work
- One Activity with different Text and Images
- Install an android application through another application
- How to make WindowBuilder create fields that are not nested in Eclipse, Java
- Linked source folders
- Eclipse Error when inserting Admob Ad Unit ID
- Packfile is truncated error while cloning Git repository
- Unable to run iDempiere model.generator in eclipse
- Android Eclipse emulator: Can't connect to activity manager; is the system running?
- SLF4J Custom Binding Not Working
- How can I configure eclipse to show Assertion errors?
- JBOSS has too many libs! (a problem with m2eclipse)
- Eclipse + GWT -> Out of memory in development mode
- Problem with installing an application on android
- How do I embed this into my website?
- java show error all java packages
- Using a python module in Java through Jython but I am very new to paths and how to configure them