|
Seriously? A NullPointerException is being thrown on line 230 because date is null . A simple walkthrough with the debugger would have revealed this.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I have initialized Date date = new Date(); but now I get application has stopped working and error is still in the same line!!!
try
{
Date date = new Date();
String getdate = spinner.getItemAtPosition(position).toString() + value
+ textView2.getText().toString();
date = sdf.parse(getdate);
} catch (ParseException ex)
{
}
isValid = isDateValid(date.getMonth(), date.getDay(), date.getYear());
|
|
|
|
|
Pavlex4 wrote: I have initialized Date date = new Date(); Which is not the one being passed to isDateValid() .
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
|
Maybe look at (or around) line 12.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
You mean this "private Date date;" ?
|
|
|
|
|
Correct.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
What is problem with that line?
|
|
|
|
|
|
I added date = new Date(); to button onclicklistener but when I click button inside app it gets pressed and stays like that and nothing happens!!!
button.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
String dayOfWeek;
boolean isValid;
date = new Date();
Calendar c = Calendar.getInstance();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd_HHmmss");
String strDate = sdf.format(c.getTime());
|
|
|
|
|
You really need to make more effort to do some debugging of your code first. You have a problem which could be caused by any part of your code, so you need to do the troubleshooting to collect more information. Also you have a try/catch block in your code that reads:
try
{
String getdate = spinner.getItemAtPosition(position).toString() + value
+ textView2.getText().toString();
Date date = sdf.parse(getdate);
}
catch (ParseException ex)
{
}
Do not do this, you are just hiding any problems that may occur. Put some proper code in the catch block to help you find errors.
|
|
|
|
|
Can you not see that you have two Date objects (in separate scopes)?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Why check your own code when you can get it done for free at CodeProject? 
|
|
|
|
|
Where can I take my application and O/S program ideas too?
|
|
|
|
|
For the purpose of what?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
I have created app that detects otg cable using service. I have created button in action bar with icon,how to make if otg cable is connected hide that button if not connected it should show text and when clicked on it to get popup window with text and animation?
MainActivity.class
public class MainActivity extends AppCompatActivity
{
public void startOtgService()
{
startService(new Intent(MainActivity.this, OtgService.class));
}
public void stopOtgService()
{
stopService(new Intent(MainActivity.this, OtgService.class));
}
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view)
{
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
Button startButton = (Button)this.findViewById(R.id.startButton);
Button stopButton = (Button)this.findViewById(R.id.stopButton);
startButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
startOtgService();
}
});
stopButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
stopOtgService();
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
if(id == R.id.action_cable)
{
}
return super.onOptionsItemSelected(item);
}
}
OtgService.class
public class OtgService extends Service
{
private boolean mOtgConnected = false;
private Handler mHandler;
Timer taskTimer = new Timer();
TimerTask task = new TimerTask()
{
private int last_Length = -1;
@Override
public void run()
{
Context context = OtgService.this.getBaseContext();
File directory = new File("/sys/bus/usb/devices");
File[] contents = directory.listFiles();
int conn_length = contents.length;
if(conn_length ==last_Length)
{
return;
}
else
{
last_Length = conn_length;
}
if(conn_length == 0)
{
mOtgConnected = false;
mHandler.post(flagChangedTask);
}
else if (conn_length > 0)
{
mOtgConnected = true;
mHandler.post(flagChangedTask);
}
}
};
Runnable flagChangedTask = new Runnable()
{
@Override
public void run()
{
if (mOtgConnected)
Toast.makeText(OtgService.this,"otg connected",Toast.LENGTH_SHORT).show();
else
Toast.makeText(OtgService.this,"otg not connected",Toast.LENGTH_SHORT).show();
}
};
public OtgService()
{
}
private void onStartCompat(Intent intent)
{
Log.e("###", "Starting service!");
if (mHandler == null)
mHandler = new Handler(getMainLooper());
taskTimer.scheduleAtFixedRate(task, 0, 1000);
}
@Override
public void onStart(Intent intent, int startId)
{
onStartCompat(intent);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId)
{
onStartCompat(intent);
return START_STICKY;
}
@Override
public void onDestroy()
{
}
@Override
public IBinder onBind(Intent intent)
{
return null;
}
}
|
|
|
|
|
Hello there. First of all, permission working fine when I use onRequestPermissionsResult() in MainActivity.
Problem occurs when I try to seperate permission from MainActivity using the following model
public interface RequestPermissionsResultInterface
{
void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults);
}
public interface PermissionManagerInterface
{
void onPermissionGranted(String message, int requestCode);
void onPermissionDenied(String message, int requestCode);
}
public class PermissionManager implements RequestPermissionsResultInterface
{
private Activity mActivity;
private static Context mContext;
private static PermissionManager mPermissionManager;
private static volatile PermissionManagerInterface mManagerInterface;
public static PermissionManager getInstance(Context context)
{
if (mPermissionManager == null)
{
synchronized (PermissionManager.class)
{
if (mPermissionManager == null)
{
mPermissionManager = new PermissionManager(context);
}
}
}
return mPermissionManager;
}
public PermissionManager(Context context) {
mContext = context;
}
public RequestPermissionsResultInterface askPermission(
Activity mActivity,
String permissionName,
final PermissionManagerInterface managerInterface,
final int requestCode)
{
if (ContextCompat.checkSelfPermission(mActivity, permissionName) != PackageManager.PERMISSION_GRANTED)
{
}
else
ActivityCompat.requestPermissions(
mActivity,
new String[]{permissionName},
requestCode);
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults)
{
if(grantResults[0] == PackageManager.PERMISSION_GRANTED)
mManagerInterface.onPermissionGranted("Permission Granted", requestCode);
else
mManagerInterface.onPermissionDenied("Permission Denied", requestCode);
}
}
Then I use PermissionManager in the MainActivity::OnCreate() like this
mPermissionManagerInterface = new PermissionManagerInterface() {
@Override
public void onPermissionGranted(String message, int requestCode)
{
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
@Override
public void onPermissionDenied(String message, int requestCode)
{
Toast.makeText(MainActivity.this, message, Toast.LENGTH_SHORT).show();
}
};
mPermissionManager = PermissionManager.getInstance(this);
mPermissionManager.askPermission(
this,
Manifest.permission.READ_EXTERNAL_STORAGE,
mPermissionManagerInterface,
EXTERNAL_STORAGE_PERMISSION_CODE);
As said in the title, it allows the permission....but it does not persist. When I close and rerun the application again, I have to allow this permission again. What could be wrong? Thanks for any pointer.
|
|
|
|
|
Did you ask for these permissions in the manifest file? Permissions granted there, are typically available for the device and you can "rely" on the fact that they are granted. Since you are requesting the permission on runtime, during the session it is likely that your application will require to request it again.
Also, note that moving upwards from Android 6.0 users can accept or deny permissions granted during install time as well, so there is very less that you can do other than PermissionManager object to always check if you have permissions, otherwise it will always end up with a runtime error. Control your app permissions on Android 6.0 and up - Google Play Help
There are even facts more to the story, read, Everything every Android Developer must know about new Android's Runtime Permission :: The Cheese Factory.
Post above: In Android 6.0 Marshmallow, application will not be granted any permission at installation time. Instead, application has to ask user for a permission one-by-one at runtime.
In other words, it is the only way to go as it guarantees that the code will execute in the case when you have the permission.
Also, this allows users to enable permissions by default in the settings, so in case your users are loving your application and they want to grant it some permissions they can (through this permissions model) go to the settings and allow permissions for the features they want your application to always work; read the Google Play Help link I provided.
Not recommended approach: You can create a singleton pattern in Android application and then request the permissions on the startup, then during the entire Android's awake period, it will have the permission but upon a restart it will request them again. But nonetheless, this is the least or not at all recommended approach from me.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Afzaal Ahmad Zeeshan wrote: Not recommended approach: You can create a singleton pattern in Android application and then ....
You are absolutely right. I am following this pattern. I don't exactly know why but I am using it (See PermissionManager ). I guess I wanted to seperate this part from MainActivity. Question is: WHY IS IT DOING IT? WHY IT IS ASKING FOR PERMISSIONS AGAIN???? WHAT IS THE SOLUTION??
|
|
|
|
|
Django_Untaken wrote: Question is: WHY IS IT DOING IT? WHY IT IS ASKING FOR PERMISSIONS AGAIN???? WHAT IS THE SOLUTION?? Instead of shouting, (which is never going to work here as we are not bound to do anything), just re-read the reply.
The sh*t I complain about
It's like there ain't a cloud in the sky and it's raining out - Eminem
~! Firewall !~
|
|
|
|
|
Afzaal Ahmad Zeeshan wrote: Instead of shouting,...
I did not shout. I would never. I just capitalized my questions. In the last 10 years as codeproject user, I never saw "A" telling "B" not to shout just because "B" wrote questions in capitals. However things can be misinterpreted when in written form.
|
|
|
|
|
Django_Untaken wrote: I did not shout. ... I just capitalized my questions.
Typing in ALL CAPS is the internet equivalent of shouting. This is common knowledge, and applies to the entire internet, not just CodeProject.
Django_Untaken wrote: In the last 10 years as codeproject user, I never saw "A" telling "B" not to shout just because "B" wrote questions in capitals.
Then you haven't been looking very hard. Every time someone posts a question in ALL CAPS, they have quickly been rebuked for shouting and, where possible, their message has been edited to the correct case.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
I want to check every second if /sys/bus/usb/devices/ directory is empty or it contain files because I want to make text inside app that says otg not connected if directory is empty or to say otg connected if directory contain files and I want to check that outside the app,while it runs in background! I need to check every second if directory is empty or it contain files?
I have crated detection using alarm manager but it doesn't work.It's just repeating "otg connected"
MainActivity.class
public class MainActivity extends AppCompatActivity
{
private Process suProcess;
private PendingIntent pendingIntent;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getRoot();
startAlarm();
}
private void getRoot()
{
try
{
suProcess = Runtime.getRuntime().exec("su");
}
catch (IOException e)
{
}
}
private void startAlarm()
{
Intent alarmIntent = new Intent(MainActivity.this, AlarmReceiver.class);
PendingIntent pendingIntent = PendingIntent.getBroadcast(this, 0, alarmIntent, 0);
AlarmManager manager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
int interval = 1000;
manager.setInexactRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), interval, pendingIntent);
}
}
Alarm Receiver.class
public class AlarmReceiver extends BroadcastReceiver
{
@Override
public void onReceive(Context context,Intent intent)
{
File[] listFiles = new File("/sys/bus/usb/devices").listFiles();
if (listFiles == null) Toast.makeText(context,"otg not connected",Toast.LENGTH_SHORT).show();
if (listFiles != null) Toast.makeText(context,"otg connected",Toast.LENGTH_SHORT).show();
}
}
|
|
|
|
|
|
Directory "/sys/bus/usb/devices" is empty when otg cable is not attached !!!!
|
|
|
|