For some reason I am experiencing massive lag when I try to use the input fields and buttons in my app. The lag is present both in the emulator that comes with Android Studio and my own One Plus Two. I really don't have much code at all so I find this very strange. The app is also using about 150Mb of RAM despite the images used are below 1Mb in total size. The app sometimes even stop responding. I am new to Android development but familiar with programming.
activity_main.xml
<?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: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:background="#drawable/bg">
<ImageView
android:contentDescription="#string/logoCS"
android:layout_width="125dp"
android:layout_height="125dp"
android:id="#+id/logo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:src="#drawable/logo" />
<Button
android:layout_width="145dp"
android:layout_height="50dp"
android:text="#string/sign_in_with"
android:id="#+id/facebookButton"
android:layout_below="#+id/logo"
android:layout_alignStart="#+id/emailField"
android:layout_marginTop="20dp"
android:textAlignment="viewStart"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textSize="12sp"
android:background="#color/signature_gray" />
<Button
android:layout_width="145dp"
android:layout_height="50dp"
android:id="#+id/googleButton"
android:text="#string/sign_in_with"
android:src="#drawable/google"
android:layout_below="#+id/logo"
android:layout_alignTop="#+id/facebookButton"
android:adjustViewBounds ="true"
android:layout_alignEnd="#+id/emailField"
android:textAlignment="viewStart"
android:paddingStart="10dp"
android:paddingEnd="0dp"
android:textSize="12sp"
android:background="#color/signature_gray" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/or"
android:textColor="#color/signature_gray"
android:id="#+id/orTextView"
android:layout_marginTop="20dp"
android:layout_below="#+id/googleButton"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="320dp"
android:layout_height="50dp"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/emailField"
android:layout_below="#id/orTextView"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#color/white"
android:hint="#string/email"
android:textColorHint="#color/signature_gray"
android:textColor="#color/signature_gray"
android:paddingEnd="10dp"
android:paddingStart="50dp"
android:maxLines="1" />
<EditText
android:layout_width="320dp"
android:layout_height="50dp"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/passwordField"
android:layout_marginTop="10dp"
android:layout_below="#+id/emailField"
android:layout_alignStart="#+id/emailField"
android:background="#color/white"
android:hint="#string/password"
android:textColorHint="#color/signature_gray"
android:textColor="#color/signature_gray"
android:paddingEnd="10dp"
android:paddingStart="50dp"
android:maxLines="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/forgot_password"
android:textColor="#color/white"
android:id="#+id/forgotPasswordText"
android:layout_below="#+id/passwordField"
android:layout_alignEnd="#+id/passwordField"
android:layout_marginTop="5dp"
android:onClick="forgotPassword" />
<TextView
android:text="#string/sing_in_error"
android:layout_width="320dp"
android:id="#+id/errorText"
android:layout_marginTop="10dp"
android:textAlignment="center"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_below="#+id/forgotPasswordText"
android:layout_alignEnd="#+id/forgotPasswordText"
android:textColor="#android:color/holo_red_light"
android:visibility="gone" />
<ImageView
android:contentDescription="#string/userIconCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/userIcon"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_above="#+id/passwordField"
android:layout_alignStart="#+id/emailField"
android:background="#drawable/user" />
<ImageView
android:contentDescription="#string/pwIconCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/passwordIcon"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_above="#+id/signInButton"
android:layout_alignStart="#+id/passwordField"
android:background="#drawable/password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="#string/not_a_member"
android:id="#+id/notMemberField"
android:textColor="#color/white"
android:layout_marginTop="20dp"
android:layout_below="#+id/signInButton"
android:layout_alignEnd="#+id/orTextView"
android:layout_marginEnd="6dp" />
<TextView
android:text="#string/register_now"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/registerText"
android:textColor="#color/white"
android:onClick="register"
android:layout_alignBaseline="#+id/notMemberField"
android:layout_alignBottom="#+id/notMemberField"
android:layout_marginStart="-3dp"
android:layout_toEndOf="#+id/notMemberField" />
<Button
android:layout_width="320dp"
android:layout_height="50dp"
android:id="#+id/signInButton"
android:layout_marginTop="65dp"
android:background="#color/signature"
android:text="#string/sing_in"
android:textColor="#color/white"
android:onClick="signIn"
android:layout_below="#+id/passwordField"
android:layout_alignEnd="#+id/passwordField" />
<ImageView
android:contentDescription="#string/googleLogoCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/googleImage"
android:elevation="2dp"
android:background="#drawable/google"
android:layout_alignTop="#+id/googleButton"
android:layout_alignEnd="#+id/googleButton"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp" />
<ImageView
android:contentDescription="#string/facebookLogoCS"
android:layout_width="30dp"
android:layout_height="30dp"
android:id="#+id/facebookImage"
android:elevation="2dp"
android:background="#drawable/facebook"
android:layout_alignTop="#+id/facebookButton"
android:layout_alignEnd="#+id/facebookButton"
android:layout_marginTop="10dp"
android:layout_marginEnd="15dp" />
</RelativeLayout>
MainActivity.java
import android.app.Activity;
import android.graphics.Paint;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends Activity {
private Button facebookButton;
private Button googleButton;
private EditText emailField;
private EditText passwordField;
private Button loginButton;
private TextView registerText;
private TextView errorText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
facebookButton = (Button) findViewById(R.id.facebookButton);
googleButton = (Button) findViewById(R.id.googleButton);
emailField = (EditText) findViewById(R.id.emailField);
passwordField = (EditText) findViewById(R.id.passwordField);
loginButton = (Button) findViewById(R.id.signInButton);
registerText = (TextView) findViewById(R.id.registerText);
errorText = (TextView) findViewById(R.id.errorText);
registerText.setPaintFlags(registerText.getPaintFlags() | Paint.UNDERLINE_TEXT_FLAG);
}
}
Related
When I'm trying to transfer data between activities, I can't get my message and the app keeps crashing,
It show me 'app keeps stopping'
Code in MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
Intent outIntent;
EditText edtPhone;
EditText edtMessage;
Button btnNext;
String tempText="";
public static final String PHONE = "PHONE";
public static final String MESSAGE = "MESSAGE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnNext = (Button) findViewById(R.id.btnNext);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View V) {
outIntent = new Intent(MainActivity.this, ActivityTwo.class);
edtPhone = (EditText) findViewById(R.id.edtPhone);
edtMessage = (EditText) findViewById(R.id.edtMessage);
tempText = edtPhone.getText().toString();
outIntent.putExtra(PHONE,tempText);
tempText = edtMessage.getText().toString();
outIntent.putExtra(MESSAGE,tempText);
startActivity(outIntent);
}
});
}
public void closeMethod(View view) {
finish();
}
}
ActivityTwo.java
package com.example.fir;
import android.content.Intent;
import android.os.Bundle;
import androidx.appcompat.app.AppCompatActivity;
import android.widget.TextView;
public class ActivityTwo extends AppCompatActivity {
Intent incomingIntent;
TextView txtPhone;
TextView txtMessage;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_two);
incomingIntent = getIntent();
txtPhone = (TextView) findViewById(R.id.txtPhone);
txtMessage = (TextView) findViewById(R.id.txtMessage);
txtPhone.setText(incomingIntent.getStringExtra(MainActivity.PHONE));
txtMessage.setText(incomingIntent.getStringExtra(MainActivity.MESSAGE));
}
}
Activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
Activitytwo.xml
////
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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" >
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="32dp"
android:text="Phone" />
<EditText
android:id="#+id/editTextPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="60dp"
android:ems="10"
android:hint="Phone"
android:inputType="phone"
android:minHeight="48dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="124dp"
android:text="Message" />
<EditText
android:id="#+id/editTextTextPersonName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="0dp"
android:layout_marginTop="156dp"
android:layout_marginEnd="3dp"
android:ems="10"
android:hint="Message"
android:inputType=""
android:minHeight="48dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:orientation="horizontal">
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Next" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="closeMethod"
android:text="CLose" />
</LinearLayout>
</RelativeLayout>
The issue is when I don't put any value into the firstEditText or secondEditText or both of them and click on any button then the app crashes and a pop up shows "myapp keeps stopping".
I cannot get the text and am unsure of why the app keeps crashing.
you have defined your Edit texts in the first activity with id's named
edtPhone
edtMessage
but there was no edtPhone or edtMessage in your xml.
this issue also repeats in your second activity
you have defined txtPhone and txtMessage
but again there are no such ids in your second activity XML.
keep in mind that these might not be the whole problem as you didn't post any log for the errors. but if there were anything else, ask and we'll help you
I am developing an app to help people know when it is time for a phone upgrade. I ran into a little problem along the way, mainly when trying to test the functionality of the code. I rated all 6 categories and pressed the continue button. Only the rating for the "Game" shows up. I don't know what is going on since I have all six textviews included.
Shows only one rating
Java:
package com.inducesmile.phoneupgrade;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;
public class Page1 extends Activity {
TextView tvBatt, tvPerf, tvAttr, tvCam, tvVal, tvGame;
RatingBar rateBattery, ratePerformance, rateCamera, rateValue, rateGaming, rateAttractiveness;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page1);
Init();
}
public void Init() {
tvBatt = (TextView) findViewById(R.id.tvBatt);
tvPerf = (TextView) findViewById(R.id.tvPerf);
tvAttr = (TextView) findViewById(R.id.tvAttr);
tvCam = (TextView) findViewById(R.id.tvCam);
tvVal = (TextView) findViewById(R.id.tvVal);
tvGame = (TextView) findViewById(R.id.tvGame);
rateBattery = (RatingBar) findViewById(R.id.rateBatt);
rateCamera = (RatingBar) findViewById(R.id.rateCam);
rateGaming = (RatingBar) findViewById(R.id.rateGame);
rateValue = (RatingBar) findViewById(R.id.rateValue);
ratePerformance = (RatingBar) findViewById(R.id.Performance);
rateAttractiveness = (RatingBar) findViewById(R.id.rateAttr);
}
public void onButtonClick(View v) {
if (v.getId() == R.id.btnCont) {
String strBatt = String.valueOf(rateBattery.getRating());
tvBatt.setText(strBatt);
String strAttr = String.valueOf(rateAttractiveness.getRating());
tvAttr.setText(strAttr);
String strCam = String.valueOf(rateCamera.getRating());
tvCam.setText(strCam);
String strPerf = String.valueOf(ratePerformance.getRating());
tvPerf.setText(strPerf);
String strGame = String.valueOf(rateGaming.getRating());
tvGame.setText(strGame);
String strVal = String.valueOf(rateValue.getRating());
tvVal.setText(strVal);
}
}
}
xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onButtonClick"
android:background="#FFF000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="How Important Are These Features to You?"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"
android:textColor="#000000"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Continue"
android:textColor="#FFFFFF"
android:id="#+id/btnCont"
android:background="#drawable/buttonshape"
android:onClick="onButtonClick"
android:layout_below="#+id/Performance"
android:layout_centerHorizontal="true"
android:layout_marginTop="34dp" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateBatt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:stepSize="0.5"
android:layout_below="#+id/textView2" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateGame"
android:stepSize="0.5"
android:layout_below="#+id/rateBatt"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateCam"
android:layout_alignLeft="#+id/rateGame"
android:layout_alignStart="#+id/rateGame"
android:stepSize="0.5"
android:layout_below="#+id/rateGame" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateAttr"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:stepSize="0.5"
android:layout_below="#+id/rateCam" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/rateValue"
android:layout_below="#+id/rateAttr"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Performance"
android:layout_below="#+id/rateValue"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Battery Life:"
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="21dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Gaming:"
android:id="#+id/textView4"
android:layout_alignTop="#+id/rateGame"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Camera:"
android:id="#+id/textView5"
android:layout_below="#+id/rateGame"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Attractiveness:"
android:id="#+id/textView6"
android:layout_marginTop="14dp"
android:layout_alignTop="#+id/rateAttr"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Value:"
android:id="#+id/textView7"
android:layout_alignTop="#+id/rateValue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Performance:"
android:id="#+id/textView8"
android:layout_below="#+id/rateValue"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvBatt"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvGame"
android:layout_alignBottom="#+id/textView4"
android:layout_toRightOf="#+id/textView4"
android:layout_toEndOf="#+id/textView4"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvCam"
android:layout_alignBottom="#+id/textView5"
android:layout_toRightOf="#+id/textView5"
android:layout_toEndOf="#+id/textView5"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvAttr"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvVal"
android:layout_alignBottom="#+id/textView7"
android:layout_toRightOf="#+id/textView7"
android:layout_toEndOf="#+id/textView7"
android:text="____" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tvPerf"
android:layout_alignBottom="#+id/textView8"
android:layout_toRightOf="#+id/textView8"
android:layout_toEndOf="#+id/textView8"
android:text="____" />
Your code is fine, try rebuild/clean the project.
If you're using Android Studio, go to Build -> Clean Project and then Build -> Rebuild Project.
This question already has answers here:
Null pointer Exception - findViewById()
(12 answers)
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 7 years ago.
The error is
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
my java is
package com.doctorfinderapp.doctorfinder;
import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.RelativeLayout;
public class DoctorProfileActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
//get data from previous activity
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, "Invia una mail a Dottore", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
//vado sul feed
RelativeLayout feedback = (RelativeLayout) findViewById(R.id.feedback);
feedback.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(DoctorProfileActivity.this, FeedbackItemActivity.class);
startActivity(intent);
}
});
}
}
xml code where is declared the function "feedbackfunction"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView 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"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:padding="#dimen/activity_horizontal_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.letsnurture.ln_202.coordinatorlayoutpart1.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="74dp"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin"
android:layout_weight="0.61">
<ImageView
android:id="#+id/ivContactItem1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_account_circle_black_24dp" />
<TextView
android:id="#+id/tvNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem1"
android:layout_toRightOf="#id/ivContactItem1"
android:text="Lorenzo Gitto"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<ToggleButton
android:id="#+id/chkState1"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_account_circle_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_info_black_24dp"
android:visibility="visible" />
<TextView
android:id="#+id/tvNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem2"
android:layout_toRightOf="#id/ivContactItem2"
android:text="Chirurgia Toracica"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber2"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem2"
android:layout_toRightOf="#id/ivContactItem2"
android:text="Chirurgia Vascolare"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState2"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_info_white_24dp"
android:checked="false"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="#layout/item_divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_book_black_24dp" />
<TextView
android:id="#+id/tvNumber3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem3"
android:layout_toLeftOf="#+id/chkState3"
android:layout_toRightOf="#id/ivContactItem3"
android:layout_toStartOf="#+id/chkState3"
android:ellipsize="end"
android:maxLines="1"
android:text="Anni di esperienza: "
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber3"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem3"
android:layout_toRightOf="#id/ivContactItem3"
android:text="11"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState3"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_info_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_explore_black_24dp"
android:visibility="visible" />
<TextView
android:id="#+id/tvNumber4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem4"
android:layout_toLeftOf="#+id/chkState4"
android:layout_toRightOf="#id/ivContactItem4"
android:layout_toStartOf="#+id/chkState4"
android:ellipsize="end"
android:maxLines="1"
android:text="Luogo di lavoro"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber4"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem4"
android:layout_toRightOf="#id/ivContactItem4"
android:text="Via del Porto, 21"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState4"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_info_white_24dp"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="#layout/item_divider" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:onClick="feedbackfunction"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_exit_to_app_white_24dp" />
<TextView
android:id="#+id/tvNumber5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem5"
android:layout_toLeftOf="#+id/chkState5"
android:layout_toRightOf="#id/ivContactItem5"
android:layout_toStartOf="#+id/chkState5"
android:ellipsize="end"
android:maxLines="1"
android:text="Punteggio Feedback"
android:clickable="true"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121"
/>
<ToggleButton
android:id="#+id/chkState5"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_book_white_24dp"
android:checked="true"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"
android:clickable="true"
android:padding="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/ivContactItem6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:src="#drawable/ic_book_black_24dp"
android:visibility="visible" />
<TextView
android:id="#+id/tvNumber6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_toEndOf="#id/ivContactItem6"
android:layout_toLeftOf="#+id/chkState6"
android:layout_toRightOf="#id/ivContactItem6"
android:layout_toStartOf="#+id/chkState6"
android:ellipsize="end"
android:maxLines="1"
android:text="Ulteriori info"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#212121" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/tvNumber6"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="8dp"
android:layout_toEndOf="#id/ivContactItem6"
android:layout_toRightOf="#id/ivContactItem6"
android:maxLines="10"
android:text="Vai Su linkedin per vedere le mie info e competenze...http://www.linkedin.com/NomeDottore "
android:textAppearance="#style/TextAppearance.AppCompat.Small"
android:textColor="#727272" />
<ToggleButton
android:id="#+id/chkState6"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:background="#drawable/ic_exit_to_app_white_24dp"
android:padding="6dp"
android:textOff=""
android:textOn="" />
</RelativeLayout>
<include layout="#layout/item_divider" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/activity_horizontal_margin">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Prenota appuntamento"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:text="Videochiama"
/>
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Can someone help me?? Thanks to all
You don't have any RelativeLayout with id equals feedback.
RelativeLayout feedback = (RelativeLayout) findViewById(R.id.feedback);
So you feedback object is null and you can't execute onClick event on a null object
Solution
Set the RelativeLayout id = feedback
Looks like you're not setting the #id for that RelativeLayout:
android:id="#+id/feedback"
My question is different from other questions previously asked on this site on the same topic. The answer as to how we can store text from EditText field to a string was given as
EditText input = (EditText) findViewById(R.id.inputId);
String str = input.getText().toString();
my question is a bit different.
my java main activity is:
package com.example.fahadsaleem.xyz;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import org.w3c.dom.Text;
public class MainActivity extends AppCompatActivity {
public String str;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView calc_monday = (TextView) findViewById(R.id.monday_calc);
calc_monday.setOnClickListener(
new Button.OnClickListener(){
public void onClick(View v){
CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.show();
TextView text1 = (TextView) cdd.findViewById(R.id.Subject_ID);
text1.setText(str);
TextView text2 = (TextView) cdd.findViewById(R.id.Room_ID);
text2.setText("6 (SEECS)");
TextView text3 = (TextView) cdd.findViewById(R.id.Time_ID);
text3.setText("09:00am 09:50am");
}
}
);
calc_monday.setOnLongClickListener(
new Button.OnLongClickListener() {
public boolean onLongClick(View v) {
kj monday_calc = new kj(MainActivity.this);
monday_calc.show();
return true;
}
}
);
}
}
The kj class has the following code:
package com.example.fahadsaleem.xyz;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
/**
* Created by Fahad Saleem on 12/22/2015.
*/
public class kj extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button back;
public kj(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.setting_dialog);
back = (Button) findViewById(R.id.set_btnn);
back.setOnClickListener(this);
}
#Override
public void onClick(View v){
switch (v.getId()){
case R.id.set_btnn:
MainActivity police = new MainActivity();
EditText set_monday_calc = (EditText) findViewById(R.id.set_Subject_ID);
// Store EditText in Variable
police.str = set_monday_calc.getText().toString();
dismiss();
break;
default:
break;
}
dismiss();
}
}
Basically the app's interface has a time table of subjects. When you click on a subject name, a dialog box appears showing 3 things: Subject name, room number and time period.
Now what I want is I should be able to change the 3 things by making a long click on the subject name. I am trying to use this button in class kj to achieve that:
case R.id.set_btnn:
MainActivity police = new MainActivity();
EditText set_monday_calc = (EditText) findViewById(R.id.set_Subject_ID);
// Store EditText in Variable
police.str = set_monday_calc.getText().toString();
dismiss();
this variable str is declared in MainActivity java file. When the user enters the text on the subject name and clicks on this button, the text should be stored in the str variable.
But this is not happening. When I start writing any text on the EditText field on my interface which looks like this:
The following error is seen in the log:
then when I click Go Back button which should set the text I wrote to variable str, the following error is seen in the log:
Later on when I single click on the subject, what I wrote before I clicked on the button is not being showed, rather a blank space is being shown as was declared by this line in mainactivity java file:
public String str;
Like this:
What is the problem? Here are the rest of the files:
CustomDialogClass:
package com.example.fahadsaleem.xyz;
import android.app.Activity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
/**
* Created by Fahad Saleem on 12/22/2015.
*/
public class CustomDialogClass extends Dialog implements android.view.View.OnClickListener {
public Activity c;
public Dialog d;
public Button back;
public CustomDialogClass(Activity a) {
super(a);
// TODO Auto-generated constructor stub
this.c = a;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.custom_dialog);
back = (Button) findViewById(R.id.btnn);
back.setOnClickListener(this);
}
#Override
public void onClick(View v){
switch (v.getId()){
case R.id.btnn:
dismiss();
break;
default:
break;
}
dismiss();
}
}
activity_main.xml:
<?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: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="com.example.fahadsaleem.xyz.MainActivity">
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/gridLayout">
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Thursday"
android:id="#+id/textView5"
android:layout_row="0"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="Time"
android:id="#+id/textView"
android:layout_row="0"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Friday"
android:id="#+id/textView6"
android:layout_row="0"
android:layout_column="6"
android:layout_gravity="center"
android:gravity="center" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="900-950"
android:id="#+id/textView7"
android:layout_row="1"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:background="#daf8a6" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Monday"
android:id="#+id/textView2"
android:layout_row="0"
android:layout_column="1"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Tuesday"
android:id="#+id/textView3"
android:layout_row="0"
android:layout_column="2"
android:gravity="center"
android:layout_gravity="center" />
<TextView
android:layout_width="100dp"
android:layout_height="27dp"
android:text="Wednesday"
android:id="#+id/textView4"
android:layout_row="0"
android:layout_column="3"
android:layout_gravity="center|fill"
android:gravity="center"/>
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Calculus"
android:id="#+id/monday_calc"
android:layout_row="1"
android:layout_column="1"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:clickable="true"
android:background="#dbfbb8" />
<TextView
android:layout_width="80dp"
android:layout_height="50dp"
android:text="Programming"
android:id="#+id/Tuesday_Prog"
android:layout_row="1"
android:layout_column="2"
android:layout_rowSpan="2"
android:layout_gravity="center|fill"
android:gravity="center"
android:textSize="13dp" />
<TextView
android:layout_width="100dp"
android:layout_height="27dp"
android:text="Library"
android:id="#+id/textView25"
android:layout_row="1"
android:layout_column="3"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Islamiat"
android:id="#+id/Thursday_Islamiat"
android:layout_row="1"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Library"
android:id="#+id/poqwe"
android:layout_row="1"
android:layout_column="6"
android:layout_gravity="center"
android:gravity="center" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="1000-1050"
android:id="#+id/textView8"
android:layout_row="2"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:background="#e0fcbb" />
<TextView
android:layout_width="80dp"
android:layout_height="75dp"
android:text="ICT Lab"
android:id="#+id/Monday_ICT"
android:layout_row="2"
android:layout_column="1"
android:layout_rowSpan="3"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="100dp"
android:layout_height="27dp"
android:text="Calculus"
android:id="#+id/Wed_calc"
android:layout_row="2"
android:layout_column="3"
android:gravity="center"
android:layout_gravity="bottom" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Calculus"
android:id="#+id/Thursday_calc"
android:layout_row="2"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Islamiat"
android:id="#+id/fri_islamiat"
android:layout_row="2"
android:layout_column="6"
android:layout_gravity="center"
android:gravity="center" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="1100-1150"
android:id="#+id/textView9"
android:layout_row="3"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:background="#daf8a6" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Library"
android:id="#+id/textView21"
android:layout_row="3"
android:layout_column="2"
android:gravity="center"
android:layout_gravity="bottom" />
<TextView
android:layout_width="100dp"
android:layout_height="27dp"
android:text="Tutorial"
android:id="#+id/textView27"
android:layout_row="3"
android:layout_column="3"
android:layout_gravity="left|bottom"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DM"
android:id="#+id/Thursday_DM"
android:layout_row="3"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Comm. Skills"
android:id="#+id/fri_comm"
android:layout_row="3"
android:layout_column="6"
android:layout_gravity="center"
android:gravity="center"
android:textSize="13dp" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="1200-1250"
android:id="#+id/textView10"
android:layout_row="4"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:background="#e0fcbb" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Comm. Skills"
android:id="#+id/Tuesday_Comm"
android:layout_row="4"
android:layout_column="2"
android:layout_gravity="center|fill"
android:gravity="center"
android:textSize="13dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DM"
android:id="#+id/Wed_DM"
android:layout_row="4"
android:layout_column="3"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tutorial"
android:id="#+id/textView33"
android:layout_row="4"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Programming"
android:id="#+id/fri_Prog"
android:layout_row="4"
android:layout_column="6"
android:layout_gravity="center"
android:gravity="center"
android:textSize="13dp" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="Lunch + Prayer Break"
android:id="#+id/textView17"
android:layout_row="5"
android:layout_column="0"
android:layout_gravity="center|fill"
android:gravity="center"
android:layout_columnSpan="7"
android:background="#d9f2f1" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="1400-1450"
android:id="#+id/textView13"
android:layout_row="6"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:background="#daf8a6" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="DM"
android:id="#+id/Monday_DM"
android:layout_row="6"
android:layout_column="1"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="50dp"
android:text="ICT Lecture"
android:id="#+id/Tuesday_ICT"
android:layout_row="6"
android:layout_column="2"
android:layout_gravity="center|fill"
android:gravity="center"
android:layout_rowSpan="2" />
<TextView
android:layout_width="100dp"
android:layout_height="75dp"
android:text="Programming Lab"
android:id="#+id/Wed_Prog"
android:layout_row="6"
android:layout_column="3"
android:layout_rowSpan="3"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Library"
android:id="#+id/textView34"
android:layout_row="6"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Library"
android:id="#+id/textView41"
android:layout_row="6"
android:layout_column="6"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="1500-1550"
android:id="#+id/textView12"
android:layout_row="7"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:background="#e0fcbb" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Library"
android:id="#+id/textView18"
android:layout_row="7"
android:layout_column="1"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Library"
android:id="#+id/textView35"
android:layout_row="7"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Library"
android:id="#+id/textView42"
android:layout_row="7"
android:layout_column="6"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="120dp"
android:layout_height="27dp"
android:text="1600-1650"
android:id="#+id/textView14"
android:layout_row="8"
android:layout_column="0"
android:textAlignment="center"
android:layout_gravity="center|fill"
android:gravity="center"
android:background="#daf8a6" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Library"
android:id="#+id/textView19"
android:layout_row="8"
android:layout_column="1"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="80dp"
android:layout_height="27dp"
android:text="Library"
android:id="#+id/textView24"
android:layout_row="8"
android:layout_column="2"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Library"
android:id="#+id/textView36"
android:layout_row="8"
android:layout_column="5"
android:layout_gravity="center|fill"
android:gravity="center" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Library"
android:id="#+id/textView43"
android:layout_row="8"
android:layout_column="6"
android:layout_gravity="center|fill"
android:gravity="center" />
</GridLayout>
setting_dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width= "400dp"
android:layout_height="140dp"
android:background="#3E80B4"
android:orientation="vertical" >
<TextView
android:id="#+id/set_txt_dia"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Subject Name: "
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold"
android:layout_above="#+id/set_textView44"
android:layout_toRightOf="#+id/relativeLayout"
android:layout_toEndOf="#+id/relativeLayout"
android:layout_marginLeft="14dp" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#3E80B4"
android:orientation="horizontal"
android:id="#+id/relativeLayout">
</RelativeLayout>
<Button
android:id="#+id/set_btnn"
android:layout_width="83dp"
android:layout_height="46dp"
android:background="#android:color/white"
android:clickable="true"
android:text="Go Back"
android:textColor="#5DBCD2"
android:textStyle="bold"
android:layout_marginRight="37dp"
android:layout_marginEnd="37dp"
android:layout_alignTop="#+id/set_Subject_ID"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="25dp" />
<TextView
android:id="#+id/set_textView15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Time Period: "
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold"
android:layout_centerVertical="true"
android:layout_alignLeft="#+id/set_textView44"
android:layout_alignStart="#+id/set_textView44" />
<TextView
android:id="#+id/set_textView44"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter Location: "
android:textColor="#android:color/white"
android:textSize="15dp"
android:textStyle="bold"
android:layout_above="#+id/set_textView15"
android:layout_alignLeft="#+id/set_txt_dia"
android:layout_alignStart="#+id/set_txt_dia" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/set_Subject_ID"
android:textSize="16dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="152dp"
android:layout_marginTop="5dp" />
<EditText
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/set_Time_ID"
android:textColor="#ffffff"
android:textSize="16dp"
android:layout_marginLeft="139dp"
android:layout_marginTop="44dp" />
<EditText
android:layout_width="70dp"
android:layout_height="wrap_content"
android:id="#+id/set_Room_ID"
android:textSize="16dp"
android:textColor="#ffffff"
android:layout_marginLeft="114dp"
android:layout_marginTop="24dp" />
I did not post the whole of java main activity, I only posted that which is relevant.
I haven't noticed you're using any database. I see that in your app you need to share data between classes, so MUST HAVE is to use SharedPreferences standard database:
Read please this:
How to use SharedPreferences in Android to store, fetch and edit values
You tried to get a string value which was temporary and available only for specific class. That's your problem. Solution above.
Hope it help
In onClick you are creating a new instance with which you don't really seem to be doing anything.
If you were to have in you kj class something like:
public MainActivity c;
...
public kj(MainActivity a) {
and then in onClick(View v) under "case R.id.set_btnn":
c.str = set_monday_calc.getText().toString();
you would manage to update str.
I am new to android. I am trying to make simple unit converter. The layout contains EditText for input, two spinners (to and from), an ImageButton and EditText to display answer.
I want to add values to spinners and when these values are selected i want to perform action.
My xml code:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".BitsBytes" >
<Spinner
android:id="#+id/SpinnerFrom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_alignRight="#+id/inputvalue"
android:layout_below="#+id/textView2"
android:entries="#array/bitsbytesfrom"
android:prompt="#string/from_prompt" />
<Spinner
android:id="#+id/SpinnerTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_alignRight="#+id/SpinnerFrom"
android:layout_below="#+id/textView3"
android:entries="#array/bitsbytesto"
android:prompt="#string/to_prompt" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/SpinnerFrom"
android:layout_below="#+id/SpinnerFrom"
android:layout_marginTop="16dp"
android:text="To:"
android:textColor="#000000"
android:textSize="20dp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="16dp"
android:gravity="center"
android:text="Value: "
android:textSize="20dp" />
<EditText
android:id="#+id/inputvalue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView6"
android:layout_alignBottom="#+id/textView6"
android:layout_toRightOf="#+id/textView6"
android:ems="10"
android:hint="Enter value"
android:inputType="numberDecimal" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView6"
android:layout_below="#+id/inputvalue"
android:layout_marginTop="16dp"
android:text="From:"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="19dp"
android:text="Bits/Bytes Conversion"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="25dp" />
<ImageButton
android:id="#+id/convertButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/SpinnerTo"
android:layout_centerHorizontal="true"
android:background="#00000000"
android:src="#drawable/btnconvert"
android:clickable="true"/>
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/answer"
android:layout_centerHorizontal="true"
android:text="Answer"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="28dp"
android:ems="10"
android:inputType="numberDecimal" />
</RelativeLayout>
My java code:
package com.easy.convert;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Spinner;
import android.widget.TextView;
public class BitsBytes extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.bitsbytes);
ImageButton ib = (ImageButton) findViewById(R.id.convertButton);
ib.setOnClickListener(new View.OnClickListener() {
EditText etValue = (EditText) findViewById(R.id.inputvalue);
Spinner spBitsBytesFrom = (Spinner) findViewById(R.id.SpinnerFrom);
Spinner spBitsBytesTo = (Spinner) findViewById(R.id.SpinnerTo);
String txtFromSpinner = spBitsBytesFrom.getSelectedItem().toString();
String txtToSpinner = spBitsBytesTo.getSelectedItem().toString();
TextView Answer = (TextView) findViewById(R.id.answer);
public void onClick(View v)
{
if (txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
{
Double value = Double.parseDouble(etValue.getText().toString());
Double answer = (double) 0;
answer = value/8;
String stringAnswer = Double.toString(answer);
Answer.setText(stringAnswer);
}
}
});
}
}
I am filling the spinners using string array. I am not able to figure out whats wrong in my code because there is no error in logcat.
Change this,
(txtFromSpinner.equals("Bits") && txtToSpinner.equals("Bytes"))
to
if((txtFromSpinner.getSelectedItem().toString().equals("Bits")) && (txtToSpinner.getSelectedItem().toString().equals("Bytes")))
this will get the text from the item selected in the spinner.