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;
}
}
Related
I'll try my best to make it clear.
I'm trying to create a scrollview of item (let's say it's a shop) which I (the user) add by my self VIA the interface of the app AND can then modify by clicking on it inside the scrollview.
For example, my main page contains a button and the list of items. When I click on it it opens a dialog which asks me informations of the item I want to add. When I finished configuring the item, I'm back on the main page and I can see the item I just added and i can click on it to modify it if I need to.
What I struggle with here is the fact, in a scrollview we have to add views. Even if I know how to do it via java, how can I add, for each new item, a clicklistener ? how do I set the id for each new view (items) considering the fact that I only can set int ids ? etc.
Does someone knows any way to do what I try to ? I'll make a very simple example of code and interfaces screenshot here in order to be very clear.
My XML MainPage : "activity_main.xml"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/popUpAddItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Your Item"
tools:ignore="MissingConstraints">
</Button>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollViewItems">
<!-- this LinearLayout is an exemple of the shape/structure of an item -->
<LinearLayout
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/protoItem">
<TextView
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Item1 "
android:id="#+id/protoName">
</TextView>
<TextView
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Qt1 "
android:id="#+id/protoQuantity">
</TextView>
<TextView
android:clickable="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Price1 "
android:id="#+id/protoPrice">
</TextView>
</LinearLayout>
</ScrollView>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
XML of the custom pop-up: "dialog_popup.xml"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/window"
tools:ignore="MissingConstraints">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemName"
android:hint="Enter the name of the item">
</EditText>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemQuantity"
android:hint="Enter the quantity of the item">
</EditText>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/itemPrice"
android:hint="Enter the price the item">
</EditText>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Valider"
android:id="#+id/validationButton">
</Button>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Class to create a custom pop-up: "CreateCustomPopUp.java
package com.example.myapplication;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class CreateCustomPopUp extends Dialog {
private LinearLayout page;
private EditText name, quantity, price;
private Button validation;
private String varName="";
private int varQt= 0;
private float varPrice =0;
public CreateCustomPopUp(Activity activity)
{
super(activity, androidx.appcompat.R.style.Theme_AppCompat_Dialog);
setContentView(R.layout.dialog_popup);
this.page = findViewById(R.id.window);
this.name = findViewById(R.id.itemName);
this.price = findViewById(R.id.itemPrice);
this.quantity = findViewById(R.id.itemQuantity);
this.validation = findViewById(R.id.validationButton);
validation.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
varName=name.getText().toString();
varQt=Integer.valueOf(quantity.getText().toString());
varPrice=Float.valueOf(price.getText().toString());
dismiss();
}
});
}
public void build()
{
show();
}
public int getQt(){
return varQt;
}
public String getName(){
return varName;
}
public float getPrice(){
return varPrice;
}
}
Main activity Java : "MainActivity.java"
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends AppCompatActivity {
public MainActivity activity;
public String name ="";
public int qt =0;
public float price = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.activity=this;
Button addItem = (Button) findViewById(R.id.popUpAddItem);
addItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
CreateCustomPopUp popUp = new CreateCustomPopUp(activity);
popUp.build();
popUp.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
name = popUp.getName();
qt = popUp.getQt();
price = popUp.getPrice();
//Put/call here a function/class or whatever works that add this created item in the scrollview
}
});
}
});
LinearLayout prototypeItem = (LinearLayout) findViewById(R.id.protoItem);
prototypeItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Another popup that I'm borred to create, but I think you see the idea...
}
});
// and here is my issue : I cant reproduce this "set on click listener" for each item... because they dont already exist and I dont know how many I'll have !
}
}
Hope it's clear and you can help me ^^
bye
Use a <RecyclerView> instead. RecyclerView is the best choice when you have multiple items of same type and you have to dynamically modify them. Follow these links to learn about recycler view and click listener in recycler view RecyclerView example. Recycler View Click Listener. I will briefly discuss how to do this.
Create a item layout file. This is basically how each item of your recycler view looks. In your case this will be LinearLayout of protoitem declared in separate xml file.
Create a data model file that defines that represents your single entity. eg. Create a class Item with fields name, quantity, price and appropriate methods.
Add only recyler view in your activity_main.xml
Create an adapter that loads your data into recycler view and link your adapter to recycler view.
Create a list of your custom Item class List<Item> myList = new ArrayList<>();
Now whenever you add new Item via dialog just add your item in myList and update your adapter by adapter.notifyDataSetChanged(); Adapter will automatically add that item in your recycler view.
For click listener you need to create an interface in your adapter and implement that click listener in you main activity.
I am trying to get a part of a simple Android app working. I am confused as to why I am getting the error "Unfortunately, this app has stopped working".
Basically, I need the total value to increase when I click on a button based on what value they have. For example coffee adds 2 to the total. Every time I click a button that adds to the total value, the app crashes.
Here is the source code. I have just copied the parts that are specific to this problem.
package com.example.pp.application;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity implements AdapterView.OnItemSelectedListener {
TextView tv01;
Double Total = 0.00;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv01 = (TextView) findViewById(R.id.tv01);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, 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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void sendMessage(View view)
{
Intent intent = new Intent(MainActivity.this, MainActivity2.class);
startActivity(intent);
}
public void addCoffee() {
Total = Total + 2.20;
tv01.setText(""+Total+"");
}
public void addBus() {
Total = Total + 1.90;
tv01.setText(""+Total+"");
}
public void addMilk() {
Total = Total + 1.50;
tv01.setText(""+Total+"");
}
}
And here is the Manifest:
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/label">
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Coffee"
android:id="#+id/button"
android:onClick="addCoffee"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="42dp" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Bus"
android:onClick="addBus"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="100dp"
android:layout_height="wrap_content"
android:text="Milk"
android:onClick="addMilk"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:onClick="sendMessage"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/button5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="------------------"
android:id="#+id/tv01"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Total"
android:id="#+id/textView"
android:layout_toStartOf="#+id/tv01"
android:layout_toLeftOf="#+id/tv01" />
Instead of public void addCoffee() your onClick method definition should look like public void addCoffee(View v), where v is the widget you clicked on (in this case the Button). Take the same approach when updating other onClick methods. This requirement applies only to callbacks defined via android:onClick attribute in XML layout.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv01 = (TextView) findViewById(R.id.tv01);
public void addCoffee(View v) {
Total = Total + 2.20;
tv01.setText(""+Total+"");
}
}
and continue with another function with same way
Since you haven't posted the stack trace of the LogCat, I have no way of telling when in the code your error has been.
But it's likely to have occured before. These links contain similar stances and documentations about exceptions.
My Android App Keeps Crashing
https://developers.google.com/analytics/devguides/collection/android/v4/exceptions
https://developer.android.com/distribute/essentials/optimizing-your-app.html
Android App Crashes on Button Click
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.
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.
Right now I am making a simple alarm clock. It has to activities. One is main and the other is "test". On the main I have created one button to set an alarm. The button is called "new" and it lead to the second activity test. In test I have a time picker and two buttons. One called "cancel" which leads back to main activity. The other is "done" which should save the time that I have picked on the time picker. I have the done button working but I don't know how to do the rest. I am very new to programming. Right now I want to know how to safe the time that I have picked on the time picker so I can access it later on to compare it with the real time. I hope to save the time when I click the "done" button. Please be as specific as possible. I am really new to this. Thank you!!!!!!!!
below are the codes I have so far
activity_test.xml
<RelativeLayout 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: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=".Test" >
<TimePicker
android:id="#+id/timePicker1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="26dp" />
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/timePicker1"
android:layout_centerHorizontal="true"
android:layout_marginTop="51dp"
android:onClick="doneButton"
android:text="Done" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/done"
android:layout_below="#+id/done"
android:layout_marginTop="52dp"
android:onClick="cancelButton"
android:text="Cancel" />
</RelativeLayout>
Test.java
package com.example.alarmclock;
import java.util.Calendar;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TimePicker;
public class Test extends Activity implements View.OnClickListener{
Button cancelButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
cancelButton = (Button)findViewById(R.id.cancel);
cancelButton.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.test, menu);
return true;
}
private void cancelButton(View v){
Intent intent = new Intent(this, MainActivity.class);
startActivity(intent);
}
public void onClick (View v){
switch (v.getId()){
case R.id.done:
cancelButton(v);
break;
}
}
}