DDMS Won't Show Debug Output - java

I am currently taking an Android course and I just learned about DDMS and debugging with Logcat.
However, the problem I have is that there are no outputs being shown on DDMS whenever I run the program on the emulator (using Eclipse IDE)
Here is my code
package com.fv.android.practice.project;
import java.io.FileOutputStream;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends ActionBarActivity {
public static final String DEBUGTAG = "FV";
public static final String TEXTFILE = "notesquirrel.txt";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addSaveButtonListener();
}
private void addSaveButtonListener() {
Button saveBtn = (Button) findViewById(R.id.save);
saveBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText editText = (EditText) findViewById(R.id.text);
String text = editText.getText().toString();
Log.d(DEBUGTAG, "Save button clicked: " + text);
}
});
}
#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;
}
}
And XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/text"
android:maxLength="400"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:hint="Enter Your Text Here" />
<Button
android:id="#+id/save"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Save" />
</LinearLayout>
When I type something into the "note" app, I should have some sort of output saying that I typed something, but I have zero output from the DDMS. Nothing at all. What can I do to fix this? I already went to the manifest and tried to set Debuggable to true but Eclipse tells me not to hardcode the debugging process, or something like that.
Also, I checked to see if the emulator was selected for debugging in the DDMS, and it is.

Fixed the issue. Now using Android Studio.

Related

I want to hide and unhide text using a button in android ; The code is correct but it still doesn't work

Here is the xml file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_hide"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.andrew.hide.Hide">
<TextView
android:text="This is the text"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="81dp"
android:id="#+id/textView"
android:textSize="40sp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Hide"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/button"
android:textSize="28sp"
android:onClick="hide"
android:layout_marginBottom="184dp" />
</RelativeLayout>
Here is the java file
package com.example.andrew.hide;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Hide extends Activity {
Button b1;
TextView t1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hide);
b1=(Button) findViewById(R.id.button);
t1=(TextView) findViewById(R.id.textView);
String text=b1.getText().toString();
}
public void hide(View view) {
if (b1.getText().toString() == "Hide") {
t1.setVisibility(View.INVISIBLE);
b1.setText("Unhide");
}
if (b1.getText().toString() == "Unhide") {
t1.setVisibility(View.VISIBLE);
b1.setText("Hide");
}
}
}
I am new to android development , so ignore if this doubt is silly.
I manually checked by using Log and I found that getText works perfectly
Nothing happens whenever i click the button (b1 here),How do I fix this?
Use equals to compare strings.
Try this,
public void hide(View view) {
String text = b1.getText().toString();
if (text.equals("Hide")) {
t1.setVisibility(View.INVISIBLE);
b1.setText("Unhide");
} else if (text.equals("Unhide")) {
t1.setVisibility(View.VISIBLE);
b1.setText("Hide");
}
}
Why don't you simply check the TextView visibility and perform your actions based on that ? It's all the setters and getters right ?
b1.onClickListener(new OnClickListener() {
if (t1.getVisibility() == View.Visible) {
t1.setVisibilty(View.GONE);
//--- your code
} else {
t1.setVisibilty(View.VISIBLE);
//--- your code
}
);

Using seterror for edittext validation in android

I am trying to validate a edittext using the seterror method.I am using the focus changed listener for checking whether the edittext has length of zero or not.But when i try to type into the required edittext, i cannot type into it and the text is being typed into the second one.Please help!!
The code:
package com.example.usernamevalidation;
import android.os.Bundle;
import android.app.Activity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.View;
import android.view.View.OnFocusChangeListener;
import android.widget.EditText;
public class MainActivity extends Activity {
EditText txtusername,txtpassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtusername=(EditText) findViewById(R.id.txtusername);
txtpassword=(EditText) findViewById(R.id.txtpassword);
txtusername.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(!hasFocus)
{
if(txtusername.getText().toString().length()==0)
{
txtusername.requestFocus();
txtusername.setError("Username cannot be left blank");
}
}
}
});
}
#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;
}
}
The xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<EditText
android:layout_width="300px"
android:layout_height="wrap_content"
android:id="#+id/txtusername"
/>
<EditText
android:layout_width="300px"
android:layout_height="wrap_content"
android:id="#+id/txtpassword"
/>
</LinearLayout>
Remove this line : txtusername.requestFocus();
Use a TextWatcher and in its onTextChanged() check whatever you want.
it is very simple you have create more your code more complex let try this code Use TextUtils TextUtils.isEmpty-Returns true if the string is null or 0-length.
EditText txtusername = (EditText) findViewById(R.id.txtusername);
EditText txtpassword = (EditText) findViewById(R.id.txtpassword);
if(TextUtils.isEmpty(txtusername.getText().toString())){
txtusername.setError("Username cannot be left blank");
txtusername.requestFocus();
}
if(TextUtils.isEmpty(txtpassword.getText().toString())){
txtpassword.setError("password cannot be left blank");
txtpassword.requestFocus();
}
//do logging process

Can't get text from editText Android

I'm having some issues with my school project. I can't get the text from the textbox. I searched for a solution but nothing found.. I'll be grateful if somebody help me :)
So here is my Java code:
package com.src.vicnote;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.os.Build;
public class NewNoteActivity extends ActionBarActivity {
Button saveButton;
EditText textData;
Context context;
String text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
saveButton = (Button) this.findViewById(R.id.buttonSave);
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
textData = (EditText) findViewById(R.id.editText);
text = textData.getText().toString();
//text = "this is sparta!";
Log.d("ADebugTag", "string: \"" + text + "\" end of note text");
new SaveClass(text/*, context.getApplicationContext()*/);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.new_note, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_new_note,
container, false);
return rootView;
}
}
}
And my XML
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.src.vicnote.NewNoteActivity"
tools:ignore="MergeRootFrame" >
<Button
android:id="#+id/buttonSave"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Save" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/buttonSave"
android:ems="10"
android:gravity="top"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
</RelativeLayout>
Also I want to ask you what will happen if the text is cyrillic? Will be there any problem?
Can't really tell what your error is without a logCat.
but one thing that might fix your problem is replacing
textData = (EditText) findViewById(R.id.editText);
text = textData.getText().toString();
with
textData = (EditText)getActivity().findViewById(R.id.editText);
text = textData.getText().toString();
// Toast for debugging purposes
Toast.makeText(getActivity(),text,0).show();
When using Fragments you should really read about getView() & getActivity().
Update
where in the xml have you set the text?
you need to set your text to something before you actually call the getText() command.
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/buttonSave"
android:ems="10"
android:gravity="top"
----- add this line ------
android:text="whatever you want"
--------------------------
android:inputType="textMultiLine" >
Good Luck
Adding a getActivity() did the trick before accessing the editText.
For example:
EditText addressText = (EditText) getActivity().findViewById(R.id.location);
getEditableText return an editable object, and you can't edit the object like that, so it is null.
simply use "getText" instead.
I don't know about Cyrillic, but it shouldn't cause any problems.

Getting Data from Multiple EditText after a click of single Button

I'm totally new to this forum and need help from you guys. I've been developing an Android application to find the efficiency of an Induction Motor. I'm new to Android App developement so I need your help.
I've created the layout and I have multiple edit text to get the inputs from the user, all I want to do is get the input from the edittext on the press of a single button. Can anyone help me with the java coding
The XML Part of the layout is as follows:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".StartingPoint" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="THE EFFICIENCY IS 0"
android:gravity="center"
android:textSize="25dp"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER VOLTAGE"
android:gravity="center"
android:id="#+id/etVoltage"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER CURRENT"
android:gravity="center"
android:id="#+id/etCurrent"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER FREQUENCY"
android:gravity="center"
android:id="#+id/etFrequency"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER STATOR PHASE RESISTANCE"
android:gravity="center"
android:id="#+id/etStatorresistance"
/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="ENTER NO LOAD POWER"
android:gravity="center"
android:id="#+id/etLoadpower"
/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text= "FIND EFFICIENCY"
android:gravity="center"
android:id="#+id/bFind"
/>
</LinearLayout>
The Java Part of the coding is StartingPoint.java
package com.blitzkrieg.vishwas;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class StartingPoint extends Activity {
EditText current, voltage, frequency, loadpower, statorresistance;
Button find;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
EditText current = (EditText) findViewById(R.id.etCurrent);
EditText voltage = (EditText) findViewById(R.id.etVoltage);
EditText frequency= (EditText)findViewById(R.id.etFrequency);
EditText loadpower= (EditText)findViewById(R.id.etLoadpower);
EditText statorresistance= (EditText)findViewById(R.id.etStatorresistance);
Button find = (Button) findViewById(R.id.bFind);
find.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}
}
Doubts that I've in my mind regarding this coding
Is the use of findViewbyId for edittext correct over here, I want to get the input values from the user using edit text.
Where in this Java coding shall I put the line of code to fetch the data from all the editText.
I want to get the result that is the efficiency of the motor just with the press of a single button which "Find Efficiency" in case of this program.
Is the data entered into the editText automatically fetched after enterin the data in that edit Text or do I need to add some buttons for fetching the data?
Please Help me guys.
Just replace your code with this code.It think this code will do your job.
1.You have declared Edittext reference correctly but you dont have to again n again declared in your code.Before your onCreate() method referencing Editext is all right. This may give you quite a handle say global which you can use in your this class everywhere you want.Inside your onCreate() you write this;
EditText current = (EditText) findViewById(R.id.etCurrent);
But declaring like this you cannot get this EditText value inside a button or where ever because this EditText has limited scope.Write like this;
current = (EditText) findViewById(R.id.etCurrent);
2.When you click on button you wanna get the EditText values.You should have to do like below i have done inside onClick().
3.As i get values in String inside onClick().Show them in LogCat.Display it here the result.
4.When you click the button all the data in every EditText will you get in String.Now do whatever you want.
package com.blitzkrieg.vishwas;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class StartingPoint extends Activity {
EditText current, voltage, frequency, loadpower, statorresistance;
Button find;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_starting_point);
current = (EditText) findViewById(R.id.etCurrent);
voltage = (EditText) findViewById(R.id.etVoltage);
frequency= (EditText)findViewById(R.id.etFrequency);
loadpower= (EditText)findViewById(R.id.etLoadpower);
statorresistance= (EditText)findViewById(R.id.etStatorresistance);
Button find = (Button) findViewById(R.id.bFind);
find.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
/* This code get the values from edittexts */
String currentvalue = current.getText().toString();
String voltagevalue = voltage.getText().toString();
String frequencyvalue = frequency.getText().toString();
String loadpowervalue = loadpower.getText().toString();
String statorresistancevalue = statorresistance.getText().toString();
/*To check values what user enters in Edittexts..just show in logcat */
Log.d("currentvalue",currentvalue);
Log.d("voltagevalue",voltagevalue);
Log.d("frequencyvalue",frequencyvalue);
Log.d("loadpowervalue",loadpowervalue);
Log.d("statorresistancevalue",statorresistancevalue);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.starting_point, menu);
return true;
}
}

My code is correct, my Droid emulator runs, but the App will not run (Eclipse)

I am fairly new to programming. Recently i was following tutorials for Android Development in the New Boston channel on youtube. I understand that there were many updates and changes in the Eclipse software, but i cannot figure this out on my own. I run, the code builds, and the emulator opens smoothly. The app starts to load, but than it closes and says " Unable to run, The New Boston." (Name of the project). I reset the ADB, i uploaded the SDK packages, I've spent all day trying to figure out why the app wont load. Here is the code:
(Mainactivity.Java)
package com.thenewboston.travis;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int counter;
Button add, sub;
TextView display;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
add = (Button) findViewById(R.id.bAdd);
sub = (Button) findViewById(R.id.bSub);
display = (TextView) findViewById(R.id.tvDisplay);
add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter +=1;
display.setText("Your total is " + counter );
}
});
sub.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
counter --;
display.setText("Your total is " + counter );
}
});
}
#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;
}
}
Layout.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/your_total_is_0"
android:textSize="45dp"
android:layout_gravity="center"
android:gravity="center"
android:id="#+id/tvDisplay" />
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/add_one"
android:layout_gravity="center"
android:id="#+id/bAdd"/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="#string/subtract_one"
android:layout_gravity="center"
android:id="#+id/bSub"/>
</LinearLayout>
thats the code i have, it just will not show up in the emulator window.
You should check Logcat output of the emulator. This will show the error message and will help you with pinpointing problems like this. You can view Logcat output in Eclipse.
Checking your code I can't find any obvious error.
I think the problem is in your AndroidManifest.xml. Please post that. It could be that you forgot to declare the activity itself, or as startup activity in the proper way.

Categories