I have added third party jar file in the project.
At compilation time there is no error but at run time the jar is not loaded and throwing "java.lang.NoClassDefFoundError".
Error in Console:
AndroidRuntime(7549): FATAL EXCEPTION: main
AndroidRuntime(7549): java.lang.NoClassDefFoundError:
AndroidRuntime(7549): at java.lang.Class.newInstanceImpl(Native Method)
AndroidRuntime(7549): at java.lang.Class.newInstance(Class.java:1301)
Fix:
Right click on the project - properties - Java Build Path - Libraries.
We can add jar files in to the location, where it helps in taking the jar file for the project.
If the project is throwing exception "java.lang.NoClassDefFoundError"then check the box of appropriate jar file in "Project - properties - Java Build Path - order and export".
Clean and Build the project and test.
This blog is to share the knowledge or tech tips in Java, Android, iOS and more..., which I came across in my Career.
Friday, October 5, 2012
Thursday, September 27, 2012
IPhone: Pass data between view controllers
There are several ways to pass the data between the view controllers.
First we will get an idea about the protocols and delegates.
Delegates are basically a way of extending the behavior of a class without needing to create subclasses. Applications in iOS often use delegates when one class calls back to another after an important action occurs.
1.
Delegates & Protocols
2.
NSUserDefaults
3.
Database
4.
Singleton Object
Here I am going to explain using Delegates & Protocols.First we will get an idea about the protocols and delegates.
Protocols
Protocols declare methods that can be implemented by any class. Protocols are useful in at least three situations:
·
To declare methods that others are expected to
implement
·
To declare the interface to an
object while concealing its class
·
To capture similarities among classes that are
not hierarchically related
Declaring a Protocol
You declare formal protocols with
the @protocol directive:
@protocol ProtocolName
method
declarations
@end
Optional Protocol Methods
Protocol methods can be marked as
optional using the @optional keyword. Corresponding to the @optional modal keyword, there is a @required keyword to formally denote the semantics of the default
behavior. You can use @optional and @required to partition your protocol into sections as you see fit. If
you do not specify any keyword, the default is @required.
@protocol MyProtocol
- (void)requiredMethod;
@optional
- (void)anOptionalMethod;
- (void)anotherOptionalMethod;
@required
- (void)anotherRequiredMethod;
@end
Delegates
iOS uses Objective-C delegates to implement the delegation pattern, in which one object passes work off to another. The object doing the work is the delegate of the first object. An object tells its delegate to do work by sending it messages after certain things happen.Delegates are basically a way of extending the behavior of a class without needing to create subclasses. Applications in iOS often use delegates when one class calls back to another after an important action occurs.
For
Java Developers:
Protocol is similar to Interface
|
Protocol
|
Interface
|
|
@protocol ProtocolName
-(void)methodName(NSString
*):value;
@end
|
public interface ProtocolName {
public void
methodName(String value);
}
|
|
Declare protocol in .h file and
import the .h file, wherever protocol has to be called
|
implements the
interface and have a logic in a class
|
Sample Code : Here
Screenshots:
View Controller Loaded
Entered Sample Text and Pressed Updated Button
Data transferred to Second View controller
Edited the Text and Pressed Back
Edited Text updated in first view controller
Java: Convert String to Hex ASCII
Convert String to
Hex ASCII
To convert String to Hex
ASCII, We can use format,
Returns a formatted
string using the specified format string and arguments.
Parameters:
format - A format string
args - Arguments
referenced by the format specifiers in the format string. If there are more
arguments than format specifiers, the extra arguments are ignored. The number
of arguments is variable and may be zero. The maximum number of arguments is
limited by the maximum dimension of a Java array as defined by the Java
Virtual Machine Specification. The behaviour on a null argument
depends on the conversion.
Returns:
A formatted string
Throws:
IllegalFormatException
- If a format string contains an illegal syntax, a format specifier that is
incompatible with the given arguments, insufficient arguments given the format
string, or other illegal conditions. For specification of all possible
formatting errors, see the Details section of the formatter class
specification.
NullPointerException
- If the format is null
By using formatter class,
we can convert char to hexadecimal integer.
Conversion
|
Argument
|
Category
Description
|
'x', 'X'
|
integral
|
The result is formatted as a
hexadecimal integer
|
'd'
|
integral
|
The result is formatted as a decimal integer
|
'o'
|
integral
|
The result is formatted as an octal
integer
|
Sample Code
public class ConvertStringToHexASCII
{
public static void
main(String[] args)
{
System.out.println(ConvertStringToHexASCII.convertStringToHexASCII("ABC"));
}
private static String
convertStringToHexASCII(String value)
{
StringBuffer
convertedValue = new StringBuffer();
for (char c :
value.toCharArray())
{
convertedValue.append(String.format("0x%02x", (int)c));
convertedValue.append(",
");
}
return
convertedValue.toString();
}
}
Subscribe to:
Posts
(
Atom
)




