FATAL EXCEPTION: main,able to start activity ComponentInfo,java.lang.IndexOutOfBoundsException - java

I'm a beginner programmer and I've started a web crawler application from a weather site. When I run the application while the first two cities of each Activity are working normally, the others stops the application with the following message: "Current_Weather was interrupted. Please help me if there is a solution to my problem.
Error Message :
06-24 16:03:04.805 18004-18004/com.example.junior_marg.current_weather E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.junior_marg.current_weather, PID: 18004
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.junior_marg.current_weather/com.example.junior_marg.current_weather.Main14Activity}: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2927)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988)
at android.app.ActivityThread.-wrap14(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6682)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410)
Caused by: java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.get(ArrayList.java:411)
at com.example.junior_marg.current_weather.Main14Activity.onCreate(Main14Activity.java:94)
at android.app.Activity.performCreate(Activity.java:6942)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1126)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2880)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2988) 
at android.app.ActivityThread.-wrap14(ActivityThread.java) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1631) 
at android.os.Handler.dispatchMessage(Handler.java:102) 
at android.os.Looper.loop(Looper.java:154) 
at android.app.ActivityThread.main(ActivityThread.java:6682) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1520) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1410) 
Java Code:
package com.example.junior_marg.current_weather;
import android.os.StrictMode;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.IOException;
public class Main14Activity extends AppCompatActivity {
TextView mTextView_33;
TextView mTextView_34;
TextView mTextView_35;
TextView mTextView_36;
TextView mTextView_37;
TextView mTextView_38;
TextView mTextView_39;
TextView mTextView_40;
TextView mTextView_41;
TextView mTextView_42;
TextView mTextView_43;
TextView mTextView_44;
TextView mTextView_45;
TextView mTextView_46;
TextView mTextView_47;
TextView mTextView_48;
String therm_3;
String ygrasia_3;
String simdrosou_3;
String anemos_3;
String varom_3;
String simerinosy_3;
String ragd_3;
String trexkak_3;
String miniaiosy_3;
String ethsiosy_3;
String aisthpsix_3;
String yperithriakt_3;
String deiktisdis_3;
String hliakiakt_3;
String anatoli_3;
String disi_3;
String url;
Document doc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main14);
StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
StrictMode.setThreadPolicy(policy);
mTextView_33 = (TextView) findViewById(R.id.textview77);
mTextView_34 = (TextView) findViewById(R.id.textview79);
mTextView_35 = (TextView) findViewById(R.id.textview81);
mTextView_36 = (TextView) findViewById(R.id.textview83);
mTextView_37 = (TextView) findViewById(R.id.textview85);
mTextView_38 = (TextView) findViewById(R.id.textview87);
mTextView_39 = (TextView) findViewById(R.id.textview89);
mTextView_40 = (TextView) findViewById(R.id.textview91);
mTextView_41 = (TextView) findViewById(R.id.textview93);
mTextView_42 = (TextView) findViewById(R.id.textview95);
mTextView_43 = (TextView) findViewById(R.id.textview97);
mTextView_44 = (TextView) findViewById(R.id.textview99);
mTextView_45 = (TextView) findViewById(R.id.textview101);
mTextView_46 = (TextView) findViewById(R.id.textview103);
mTextView_47 = (TextView) findViewById(R.id.textview105);
mTextView_48 = (TextView) findViewById(R.id.textview107);
doc = null;
url = "http://penteli.meteo.gr/stations/larissa/";
try {
doc = Jsoup.connect(url).get();
} catch (IOException e) {
e.printStackTrace();
}
Element table_33 = doc.select("table").get(0);
Elements rows_33 = table_33.select("tr");
Element row_33 = rows_33.get(5);
Elements cols_33 = row_33.select("td");
therm_3 = cols_33.get(1).text() ;
mTextView_33.setText(therm_3);
Element row_34 = rows_33.get(6);
Elements cols_34= row_34.select("td");
ygrasia_3 = cols_34.get(1).text() ;
mTextView_34.setText(ygrasia_3);
Element row_35 = rows_33.get(7);
Elements cols_35 = row_35.select("td");
simdrosou_3 = cols_35.get(1).text() ;
mTextView_35.setText(simdrosou_3);
Element row_36 = rows_33.get(8);
Elements cols_36 = row_36.select("td");
anemos_3 = cols_36.get(1).text() ;
mTextView_36.setText(anemos_3);
Element row_37 = rows_33.get(9);
Elements cols_37 = row_37.select("td");
varom_3 = cols_37.get(1).text() ;
mTextView_37.setText(varom_3);
Element row_38 = rows_33.get(10);
Elements cols_38 = row_38.select("td");
simerinosy_3 = cols_38.get(1).text() ;
mTextView_38.setText(simerinosy_3);
Element row_39 = rows_33.get(11);
Elements cols_39 = row_39.select("td");
ragd_3 = cols_39.get(1).text() ;
mTextView_39.setText(ragd_3);
Element row_40 = rows_33.get(12);
Elements cols_40 = row_40.select("td");
trexkak_3 = cols_40.get(1).text() ;
mTextView_40.setText(trexkak_3);
Element row_41 = rows_33.get(13);
Elements cols_41 = row_41.select("td");
miniaiosy_3 = cols_41.get(1).text() ;
mTextView_41.setText(miniaiosy_3);
Element row_42 = rows_33.get(14);
Elements cols_42 = row_42.select("td");
ethsiosy_3 = cols_42.get(1).text() ;
mTextView_42.setText(ethsiosy_3);
Element row_43 = rows_33.get(15);
Elements cols_43 = row_43.select("td");
aisthpsix_3 = cols_43.get(1).text() ;
mTextView_43.setText(aisthpsix_3);
Element row_44 = rows_33.get(16);
Elements cols_44 = row_44.select("td");
yperithriakt_3 = cols_44.get(1).text() ;
mTextView_44.setText(yperithriakt_3);
Element row_45 = rows_33.get(17);
Elements cols_45 = row_45.select("td");
deiktisdis_3 = cols_45.get(1).text() ;
mTextView_45.setText(deiktisdis_3);
Element row_46 = rows_33.get(18);
Elements cols_46 = row_46.select("td");
hliakiakt_3 = cols_46.get(1).text() ;
mTextView_46.setText(hliakiakt_3);
Element row_47 = rows_33.get(19);
Elements cols_47 = row_47.select("td");
anatoli_3 = cols_47.get(1).text() ;
mTextView_47.setText(anatoli_3);
Element row_48 = rows_33.get(20);
Elements cols_48 = row_48.select("td");
disi_3 = cols_48.get(1).text() ;
mTextView_48.setText(disi_3);
}
}
XML Code :
<?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="com.example.junior_marg.current_weather.Main14Activity">
<TextView
android:id="#+id/textview76"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/c"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview77"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview76"
android:layout_marginLeft="20dp"
android:layout_marginStart="92dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview78"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/d"
android:layout_below="#+id/textview76"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview79"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview78"
android:layout_below="#+id/textview77"
android:layout_marginLeft="50dp"
android:layout_marginStart="133dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview80"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/h2"
android:layout_below="#+id/textview78"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview81"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview80"
android:layout_marginLeft="20dp"
android:layout_marginStart="77dp"
android:layout_marginTop="63dp" />
<TextView
android:id="#+id/textview82"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/i2"
android:layout_below="#+id/textview80"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview83"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview82"
android:layout_marginLeft="50dp"
android:layout_marginStart="130dp"
android:layout_marginTop="90dp" />
<TextView
android:id="#+id/textview84"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/j2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview82"/>
<TextView
android:id="#+id/textview85"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview84"
android:layout_below="#+id/textview83"
android:layout_marginLeft="10dp"
android:layout_marginStart="104dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview86"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/k2"
android:layout_below="#+id/textview84"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview87"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview86"
android:layout_below="#+id/textview85"
android:layout_marginLeft="50dp"
android:layout_marginStart="63dp"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textview88"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/l2"
android:layout_below="#+id/textview86"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview89"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview88"
android:layout_below="#+id/textview87"
android:layout_marginLeft="20dp"
android:layout_marginStart="94dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview90"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/m2"
android:layout_below="#+id/textview88"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview91"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview90"
android:layout_below="#+id/textview89"
android:layout_marginLeft="50dp"
android:layout_marginStart="36dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview92"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/n2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview90"/>
<TextView
android:id="#+id/textview93"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview92"
android:layout_below="#+id/textview91"
android:layout_marginLeft="20dp"
android:layout_marginStart="75dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview94"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/o2"
android:layout_below="#+id/textview92"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview95"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview94"
android:layout_below="#+id/textview93"
android:layout_marginLeft="50dp"
android:layout_marginStart="85dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview96"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/p2"
android:layout_below="#+id/textview94"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview97"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview96"
android:layout_below="#+id/textview95"
android:layout_marginLeft="20dp"
android:layout_marginStart="73dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview98"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/q2"
android:layout_below="#+id/textview96"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview99"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview98"
android:layout_below="#+id/textview97"
android:layout_marginLeft="50dp"
android:layout_marginStart="24dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview100"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/r2"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:layout_below="#+id/textview98"/>
<TextView
android:id="#+id/textview101"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview100"
android:layout_below="#+id/textview99"
android:layout_marginLeft="10dp"
android:layout_marginStart="52dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview102"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/s2"
android:layout_below="#+id/textview100"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview103"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview102"
android:layout_below="#+id/textview101"
android:layout_marginLeft="50dp"
android:layout_marginStart="45dp"
android:layout_marginTop="11dp" />
<TextView
android:id="#+id/textview104"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/t2"
android:layout_below="#+id/textview102"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview105"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview104"
android:layout_below="#+id/textview103"
android:layout_marginLeft="20dp"
android:layout_marginStart="122dp"
android:layout_marginTop="10dp" />
<TextView
android:id="#+id/textview106"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/u2"
android:layout_below="#+id/textview104"
android:layout_marginLeft="20dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"/>
<TextView
android:id="#+id/textview107"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textview106"
android:layout_below="#+id/textview105"
android:layout_marginLeft="50dp"
android:layout_marginStart="151dp"
android:layout_marginTop="10dp" />
</RelativeLayout>

I think your problem is in therm_3 = cols_33.get(1).text() ;. it seems that your cols_33 just has one element while you are trying to get it's second element. I hope your problem would be solved if you replace that line with this:
therm_3 = cols_33.get(0).text() ;

Related

Why do I get a null object reference on my RealtiveLayout.setBackgroundColor although I have initialized it

Here is my java code:
package com.example.project_final;
import static android.content.ContentValues.TAG;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.firestore.DocumentReference;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.EventListener;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.FirebaseFirestoreException;
import java.util.ArrayList;
public class history extends AppCompatActivity {
FirebaseFirestore Fstore;
String userID;
FirebaseAuth fAuth ;
int times;
int heart;
int avg_heart;
String date;
int count = 0;
TextView Data;
RelativeLayout [] L = new RelativeLayout[7];
TextView [] hr = new TextView[7];
TextView [] dates = new TextView[7];
ImageView [] img = new ImageView[7];
TextView last;
ArrayList<Integer> intheart = new ArrayList<>();
ArrayList<String> intdates = new ArrayList<>();
TextView average;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
Fstore = FirebaseFirestore.getInstance();
fAuth = FirebaseAuth.getInstance();
userID= fAuth.getUid();
DocumentReference documentReference = Fstore.collection("users").document(userID);
Data = findViewById(R.id.txtNodata);
Intent intent1 = getIntent();
String signup = intent1.getStringExtra("signup");
last = findViewById(R.id.txtlast);
average = findViewById(R.id.txtaverage);
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout"+ i+1, "id", this.getPackageName());
L[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("heartrate"+ i+1, "id", this.getPackageName());
hr[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("txtdate"+ i+1, "id", this.getPackageName());
dates[i] = findViewById(resourceId);
}
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("img"+ i+1, "id", this.getPackageName());
img[i] = findViewById(resourceId);
}
documentReference.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
#Override
public void onComplete(#NonNull Task<DocumentSnapshot> task) {
times = Math.toIntExact(task.getResult().getLong("times"));
avg_heart = Math.toIntExact(task.getResult().getLong("average_heart_rate"));
for (int i = 0 ; i <times ;i++){
intdates.add(task.getResult().getString("date" + i));
intheart.add(Math.toIntExact(task.getResult().getLong("heart"+i)));
}
}
});
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
if(!(intheart.isEmpty()||intdates.isEmpty())){
int count =0;
if(times>7){
times=7;
}
last.setText("Last "+ times +" Days history");
for(int i =0 ; i<intdates.size();i++){
if(1.2*avg_heart<intheart.get(i)||0.8*avg_heart<intheart.get(i)){
L[i].setBackgroundColor(R.drawable.color_gradient1);
img[i].setImageResource(R.drawable.sad);
}
else{
L[i].setBackgroundColor(R.drawable.gradient_color);
img[i].setImageResource(R.drawable.happy);
}
dates[i].setText(intdates.get(i));
hr[i].setText(intheart.get(i)+" Bpm");
L[i].setVisibility(View.VISIBLE);
}
}
else{
handler.postDelayed(this, 100);
}
}
}, 100);
}
}
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout 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:padding="20dp"
android:orientation="vertical"
tools:context=".history">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/txtlast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:text=""
android:textSize="30sp"
android:layout_gravity="center"
android:textColor="#000"
android:textStyle="bold"
/>
<TextView
android:layout_marginTop="20dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text=""
android:id="#+id/txtaverage"
android:textColor="#000"
android:layout_gravity="center"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
android:visibility="invisible"
>
<TextView
android:id="#+id/txtday1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/txtdate1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday1"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate1"
android:id="#+id/heartrate1"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img1"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:background="#drawable/color_gradient1"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 2"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday2"
/>
<TextView
android:id="#+id/txtdate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<TextView
android:id="#+id/heartrate2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate2"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img2"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout3"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 3"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday3"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate3"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate3"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate3"
/>
<ImageView
android:id="#+id/img3"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/color_gradient1"
android:visibility="invisible"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 4"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday4"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday4"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate4"
/>
<TextView
android:id="#+id/heartrate4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate4"
android:layout_marginTop="10dp"
android:textColor="#ffff" />
<ImageView
android:id="#+id/img4"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:id="#+id/layout5"
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 1"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday5"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate5"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate5"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate5"
/>
<ImageView
android:id="#+id/img5"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:id="#+id/layout6"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 6"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday6"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate6"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate6"
android:id="#+id/heartrate6"
android:layout_marginTop="10dp"
android:textColor="#ffff"
/>
<ImageView
android:id="#+id/img6"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="120dp"
app:cardCornerRadius="20dp"
android:layout_marginTop="20dp">
<RelativeLayout
android:visibility="invisible"
android:id="#+id/layout7"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/gradient_color"
android:padding="10dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Day 7"
android:textColor="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:id="#+id/txtday7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtday7"
android:textColor="#ffff"
android:layout_marginTop="10dp"
android:id="#+id/txtdate7"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/txtdate7"
android:layout_marginTop="10dp"
android:textColor="#ffff"
android:id="#+id/heartrate7"
/>
<ImageView
android:id="#+id/img7"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginLeft="220dp"
/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<TextView
android:id="#+id/txtNodata"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_gravity="center"
android:fontFamily="#font/lovelo"
android:gravity="center"
android:text="No data Available"
android:textColor="#color/colorRed"
android:textSize="20sp"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
When I run the code I get this error on a null object reference when I try to setbackground color inside the handler:
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.setBackgroundColor(int)'
The code should run fine since I initialized all the views. I've been stuck for a while I don't know where is the problem help.
I think you are not getting the name correct, so it's not initializing it, name must be a string as a whole :
for(int i = 0; i<=6 ;i++) {
int resourceId = this.getResources().getIdentifier("layout" + (i+1).toString(), "id", this.getPackageName());
L[i] = findViewById(resourceId);
}

FATAL EXCEPTION : main (Android std)

While I run this on my device it will crash and my logcat is as below:
2020-12-19 17:03:28.204 24798-24798/com.example.emergencynotificationhealthcare E/AndroidRuntime:
FATAL EXCEPTION: main
Process: com.example.emergencynotificationhealthcare, PID: 24798
java.lang.RuntimeException: Unable to start activity
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2799)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2870)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1573)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:156)
at android.app.ActivityThread.main(ActivityThread.java:6605)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:999)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:889)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void
android.view.View.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.example.emergencynotificationhealthcare.Login.onCreate(Login.java:42)
at android.app.Activity.performCreate(Activity.java:6964)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1123)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2752)
Here is my activity_register.xml:
<ImageView
android:layout_width="101dp"
android:layout_height="69dp"
android:transitionName="logo_logo"
android:src="#drawable/logo_new"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="-10dp"
android:fontFamily="#font/bungee"
android:text="Welcome"
android:transitionName="logo_text"
android:textColor="#ff8e88"
android:textSize="30sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register to start"
android:textSize="16sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
app:counterEnabled="true"
app:counterMaxLength="15"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:transitionName="user_tran"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Email"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/phoneNo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Phone Number"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:passwordToggleEnabled="true"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:transitionName="password_tran"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Type"
android:layout_marginLeft="5dp"
android:textSize="17dp"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User" />
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Guardian"/>
<Button
android:id="#+id/login_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="#ff8e88"
android:text="Register"
android:textColor="#fff"
android:transitionName="button_tran"/>
<Button
android:id="#+id/register_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Already have an account? LOGIN"
android:background="#00000000"
android:transitionName="login_register_tran"/>
Here is my Login.java:
Button callRegister, login_btn;
ImageView image;
TextView logoText, sloganText;
TextInputLayout username,password;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
callRegister = findViewById(R.id.register_screen);
image = findViewById(R.id.imageView);
logoText = findViewById(R.id.logo_name);
sloganText = findViewById(R.id.slogan_name);
username = findViewById(R.id.username);
password = findViewById(R.id.password);
login_btn = findViewById(R.id.login_btn);
callRegister.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Login.this,Register.class);
Pair[] pairs = new Pair[7];
pairs[0] = new Pair<View,String>(image,"logo_logo");
pairs[1] = new Pair<View,String>(logoText,"logo_text");
pairs[2] = new Pair<View,String>(sloganText,"logo_desc");
pairs[3] = new Pair<View,String>(username,"user_tran");
pairs[4] = new Pair<View,String>(password,"password_tran");
pairs[5] = new Pair<View,String>(login_btn,"button_tran");
pairs[6] = new Pair<View,String>(callRegister,"login_register_tran");
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.LOLLIPOP) {
ActivityOptions options = ActivityOptions.makeSceneTransitionAnimation(Login.this,pairs);
startActivity(intent, options.toBundle());
}
}
});
}
Here is my activity_login.xml
<ImageView
android:layout_width="101dp"
android:layout_height="69dp"
android:src="#drawable/logo_new"
android:transitionName="logo_logo"/>
<TextView
android:id="#+id/logo_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/bungee"
android:text="Hi, Welcome Back!"
android:transitionName="logo_text"
android:textColor="#ff8e88"
android:textSize="40sp"/>
<TextView
android:id="#+id/slogan_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign in to continue"
android:transitionName="logo_desc"
android:textSize="18sp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="20dp"
android:orientation="vertical">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/username"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/password"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
app:passwordToggleEnabled="true">
<com.google.android.material.textfield.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword" />
</com.google.android.material.textfield.TextInputLayout>
<CheckBox
style="#style/Widget.AppCompat.CompoundButton.CheckBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember Me" />
<Button
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="3dp"
android:background="#00000000"
android:elevation="0dp"
android:text="Forget Password" />
<Button
android:id="#+id/btnLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:background="#ff8e88"
android:text="Log In"
android:textColor="#fff" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="5dp"
android:background="#00000000"
android:elevation="0dp"
android:fontFamily="#font/antic"
android:text="New Member? REGISTER"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
Did you inflate the correct layout file? activity_register is the name of the file you said, but your code uses
setContentView(R.layout.activity_login);.
Try replacing that with setContentView(R.layout.activity_register); if that's not the case.

It does not total the value given by total=i1+i2+i3+i4+i5; t17.setText(String.valueOf(total));it always show 0 at t17

java
there is problem in total.I have already tried try and catch.It always
show 0 at t17.looks like it does not set the total value.
It does not total the value given by total=i1+i2+i3+i4+i5;
t17.setText(String.valueOf(total));it always show 0 at t17
package com.example.myproject1;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.TextView;
public class Bill extends AppCompatActivity {
TextView t,t1,t2,t3,t4,t5,t6,t7,t8,t9,t10,t11,t12,t13,t14,t15,t16,t17;
Button b,b1;
int total;
int i1,i2,i3,i4,i5;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill);
t=findViewById(R.id.textView);
t1=findViewById(R.id.textView5);
t2=findViewById(R.id.textView10);
t3=findViewById(R.id.textView15);
t4=findViewById(R.id.textView6);
t5=findViewById(R.id.textView11);
t6=findViewById(R.id.textView16);
t7=findViewById(R.id.textView7);
t8=findViewById(R.id.textView12);
t9=findViewById(R.id.textView17);
t10=findViewById(R.id.textView8);
t11=findViewById(R.id.textView13);
t12=findViewById(R.id.textView18);
t13=findViewById(R.id.textView9);
t14=findViewById(R.id.textView14);
t15=findViewById(R.id.textView19);
t16=findViewById(R.id.textView20);
t17=findViewById(R.id.textView21);
b=findViewById(R.id.button22);
b1=findViewById(R.id.button23);
Intent obj2 = getIntent();
String r = obj2.getStringExtra("k");
t.setText(r);
String res = obj2.getStringExtra("key");
String res1 = obj2.getStringExtra("key2");
String res2 = obj2.getStringExtra("key3");
String res3 = obj2.getStringExtra("key4");
String res4 = obj2.getStringExtra("key5");
String res5 = obj2.getStringExtra("key6");
String res6 = obj2.getStringExtra("key7");
String res7 = obj2.getStringExtra("key8");
String res8 = obj2.getStringExtra("key9");
String res9 = obj2.getStringExtra("key10");
String res10 = obj2.getStringExtra("key11");
String res11 = obj2.getStringExtra("key12");
String res12 = obj2.getStringExtra("key13");
String res13 = obj2.getStringExtra("key14");
String res14 = obj2.getStringExtra("key15");
t1.setText(res);
t2.setText(res1);
t3.setText(res2);
t4.setText(res3);
t5.setText(res4);
t6.setText(res5);
t7.setText(res6);
t8.setText(res7);
t9.setText(res8);
t10.setText(res9);
t11.setText(res10);
t12.setText(res11);
t13.setText(res12);
t14.setText(res13);
t15.setText(res14);
try{
if (res2 != null) {
i1 = Integer.parseInt(res2);
} else {
i1 = 0;
}
if (res5 != null) {
i2 = Integer.parseInt(res5);
} else {
i2 = 0;
}
if (res8 != null) {
i3 = Integer.parseInt(res8);
} else {
i3 = 0;
}
if (res11 != null) {
i4 = Integer.parseInt(res11);
} else {
i4 = 0;
}
if (res14 != null) {
i5 = Integer.parseInt(res14);
} else {
i5 = 0;
}
}catch (NumberFormatException e){
}
t16.setText("Total Price:");
total=i1+i2+i3+i4+i5;
t17.setText(String.valueOf(total));
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent obj = new Intent(Bill.this, Menu.class);
startActivity(obj);
}
});
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent obj1 = new Intent(Bill.this, MainActivity.class);
startActivity(obj1);
}
});
}
}
XML
the xml of my code is given as below though it has nothing to do with
my coding part.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:background="#drawable/bill4"
tools:context=".Bill">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="224dp"
android:layout_marginTop="5dp"
android:background="#92278F"
android:gravity="center"
android:text="user"
android:textColor="#0066FF"
android:textSize="24sp"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="64dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="20dp"
android:background="#CD81DB"
android:fontFamily="#font/joti_one"
android:text="Bill"
android:textColor="#111111"
android:textSize="#android:dimen/app_icon_size"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Item"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="13dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Quantity"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="60dp"
android:background="#92278F"
android:gravity="center"
android:paddingLeft="25dp"
android:paddingRight="25dp"
android:text="Price"
android:textColor="#0066FF"
android:textSize="24dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="18dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView14"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="120dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView15"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView16"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView17"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView18"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView19"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textColor="#111111"
android:layout_marginTop="10dp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/textView20"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:textColor="#111111"
android:text=""
android:textSize="14dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/textView21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="200dp"
android:layout_marginTop="20dp"
android:textColor="#111111"
android:textSize="14dp"
android:textStyle="bold"
android:text="" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button22"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="25dp"
android:layout_marginTop="20dp"
android:background="#92278F"
android:textColor="#0066FF"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
android:text="Cancel" />
<Button
android:id="#+id/button23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="165dp"
android:layout_marginTop="20dp"
android:background="#92278F"
android:textColor="#0066FF"
android:textStyle="bold"
app:fontFamily="#font/merienda_one"
android:text="Order" />
</LinearLayout>
</LinearLayout>
I have used Sharedpreferences instead of intent however it is possible
from both.
Data recieving class
public class class_name extends AppCompatActivity {
SharedPreferences sharedPref;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bill);
sharedPref=this.getSharedPreferences("calis",Context.MODE_PRIVATE);
String res = sharedPref.getString("k1",null);
String res1 = sharedPref.getString("k2",null);
String res2 = sharedPref.getString("k3",null);
Integer res15 = sharedPref.getInt("k4",0);
Integer vres15 = sharedPref.getInt("k5",0);
int total2 = res15+vres15;
t17.setText(String.valueOf(total2));
}
}
Data sending class
public class class_name2 extends Fragment {
SharedPreferences sp;
sp=this.getActivity().getSharedPreferences("calis", Context.MODE_PRIVATE);
SharedPreferences.Editor editor=sp.edit();
editor.putString("k1",value1);
editor.putInt("k4", value2);
editor.commit();
}

Only shows one tetview rating

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.

How to use spinners in android?

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.

Categories

Resources