I just began in Java and Android. This website already helped me a lot.
Here I'm stuck with something very easy. In other language I never had problem but there..
If I try the following, i have no error on eclipse but once running in the emulator, the software crash at the "if" line.
String message = mOutEditText.getText().toString();
if (message.length() > 4) {
If I use if (8 > 4) { then I have no problem.
I also tried if (message.toString.length() > 4) { without any success.
Thanks for your help.
It's hard to say without knowing the error but just about the only thing that could possibly go wrong with that line is a NullPointerException i.e. when you have this problem, it is because:
String message = mOutEditText.getText().toString();
is effectively resolving to:
String message = null;
Most likely this is because your EditText does not have any text in it (from the name of your variable I'm guessing you are using an EditText).
You can account for this case by checking that message is not null. Here is one way of doing it:
if (message != null && message.length() > 4) {
For instance, you initialize message as a static string like "Stack overflow" and try
message.length();
if it works, then there is no problem with toString() function.
Best of luck..!!
In String There are two way of getting the length of String:-this is working properly
String message="";
protected void onCreate(Bundle savedInstanceState){ s.length();}
Related
I have android platform on one end and arduino on the other, connected via serial. Everything works fine, however in some cases arduino restarts itself and causes a flow of unknown characters while its restarting to the serial.
Here is a serial log while arduino is rebooting:
�z������"&O�Z&���B
���F ���cd�:{����t�>��+������2�~����. ���r���DD���^��.�.B�.��ڮ2t��Z:��,R��A�ڢr��Ckˡ���.-���N^���b�����^���
Question is, how can I check on android end if the response was malformed?
You should probably add some kind of "framing" to your messages. CR/LF isn't enough.
For example, put a special "preamble" at the front, and watch for it on the Android side. Choose something that will not occur in the body ("payload") of the message. And choose something that is very unlikely to occur in the random chars that show up on a reboot, a couple of chars long.
You could also put a CRC at the end. "Fletcher" is easy.
I ended up using simple solution like this:
private String filterData(String receivedStr) {
if (receivedStr.contains(RECV_HEADER) && receivedStr.contains(mReadRules.RECV_END)) {
int header_pos = receivedStr.indexOf(RECV_HEADER);
int crc_pos = receivedStr.indexOf(RECV_END);
return receivedStr.substring(header_pos, crc_pos);
} else {
return null;
}
}
It also extracts message if its wrapped around with malformed data.
I wrote an app which connects to java server. It sends a small message to server and the server answers if the message is "on". For any other case the server is not sending data back.
I wrote a line to check if the inputstream is null or not, but it gets a nullpointer exception on no answer from server. And its wierd for me:
if (!(line = in.readLine()).equals(null)) { Remote.setValue(line);
}
So if "line" is null, then it should skip (or go to else part if it would where) the part where I set the value, but it hangs on. Simply blocking the whole app.
What I did wrong? Why is this? How can I solve it? Please help!
You're not checking if the input stream is null. You're checking if readLine returns null.
Try this instead :
if (in != null) {
line = in.readLine();
if (line != null) {
Remote.setValue(line);
}
}
EDIT :
Use socket.setSoTimeout (x) where x is some positive integer in order for in.readLine() not to block until an input is available.
I want to install my app which contains regestration form whose username and password is stored im sqlite database. after copyiny .apk file my apps does not contains any database table for that work.
So my question is how to import this database in my app which is installed on android device it is working fine on emulator.
Have you initialized database class object
EditDatabase db=new EditDatabase(this);
onCreate or onResume activity?
if not then initialize it.
Update from logcat output
Based on the logcat output, it shows that this is an unhandled exception. i.e. it happens outside the try{} ... catch{} block of the onClick(View) method.
This means that it is a problem in one of these 2 lines:
unname = username1.getText().toString();
storePassword1 = db.getdata(unname);
My feeling is that username1 is null because it is not referenced in your layout XML file.
Here's how you should check - replace those 2 lines with these null checks:
if (unname != null)
{
unname = username1.getText().toString();
}
else
{
Log.d("MyTag", "unname is null");
}
if (storePassword1 != null)
{
storePassword1 = db.getdata(unname);
}
else
{
Log.d("MyTag", "storePassword1 is null");
}
if (unname == null || storePassword1 == null)
{
return;
}
Run the code again, check the logcat output again, and see if it tells you about the problem.
Also try not to use System.out(String) - rather use the Log.d(String, String) methods. These are more useful on Android.
Original
Firstly, please provide a stack trace - it will show where the null pointer error happened.
For Android, the most useful way of doing that will be to use the adb logcat terminal command. This outputs the internal log of your Android device/emulator to the screen, so you can view what went wrong.
In your catch{} block, I would put the following line:
Log.d("MyTag", "Stack Trace of exception...", e);
This will output the text, and information about the error e to this log - I think it will include the stack trace.
Copy the lines starting with "MyTag" and paste them into your question.
Secondly, without the stack trace, confirm that these 3 variables are not null typically:
username1
db
password (if the app actually crashed with the NullPointerException then this one is not the problem, because the exception would be handled by the catch{} block)
i.e. confirm that you have initialised all of them before the click event.
After many tries, I decided to ask the question again. In my last question, someone said I should have a look at Jsoup. I wrote some code but it won't work. It's an android app. But it totally crashes. with the error message:
Unfortunately, (appname) has stopped
See the full error message
My code for extracting text from the <div>:
public void ButtonClick(View view) throws IOException {
Document doc = dereference("here is my url");
String text = extractContent(doc);
updateUI(text);
}
private Document dereference(String uri) {
Connection connection = Jsoup.connect(uri);
return connection.get();
}
private String extractContent(Document doc) {
Elements divs = doc.select("div.onlinestatus");
return divs.text();
}
private void updateUI(String text) {
TextView tv = (TextView)findViewById(R.id.textView1);
tv.setText(text);
}
the input from the url:
<html><!-- [...] --><body>
<div class='onlinestatus'>Server ist online! <br /></div>
</body></html>
Can someone spot the mistake?
Edit: when I perform all these operations in a separate thread, I get a different error. Error log and code can be found here.
This is not a Jsoup question after all.
If you look up the error from your error log (first line) where it reads
NetworkOnMainThreadException
Googling for "android NetworkOnMainThreadException" yiedls a page from the android developer reference, which states
The exception that is thrown when an application attempts to perform a networking operation on its main thread.
This would explain your previous attempts at moving code into a separate thread, which seemed to produce different results.
Have a look at the page suggested in the android developer reference, on Designing for Responsiveness. That should give you an answer.
The next thing that can fail, is that you need to do all UI changes in the thread that created the UI. The CalledFromWrongThreadException you got here tells you exactly what went wrong. Looking up that error will lead you to a question+answer similar to this one.
If you have problems with that, I suggest you ask a new question, if your problem is not already covered on StackOverflow (but I believe it is!)
I would like to detect the unknown phone number in my call logs list.
An unknown number on the phone is either -1 or -2.
My question is how to detect this type of call?.
I try to detect with :
number = Integer.parseInt(number);
if(number < 0){
}
but it is not working.
Try the following:
1) Log everything.
Put
Log.debug("ClassName", "number = " +number);
2) number = Integer.parseInt(number);
does not look quite right.
try
assuming incomingPhoneNumber is defined as a String.
String incomingPhoneNumber;
try{
int phoneNumber = Integer.parseInt(incomingPhoneNumber);
}catch (Exception e) {
Log.debug("ClassName", "error = "+e.getMessage());
}
in eclipse you need to goto window -->Show View --> Logcat to and examine the log message but you should also just set breakpoints and run in debug mode. Step one line at a time.
Tell us if you get a negative number for the incoming call.
3) Place breakpoints at the very beginning and run in debug mode. Tell us what you get.
4) Communicate with the user questions. People are trying to help but you need to answer our questions for us to help you. For example we do need to see the definition for number to know if
the code is going to work.