This question already has answers here:
Resource not found TextView
(2 answers)
Closed 7 years ago.
Hi i am new to android development and i couldn't execute method when a button is clicked.I retyped the code as in the tutorial but it ended up lot of errors. Check the code below,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basketball Score Game"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="25sp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:paddingLeft="70dp"
android:id="#+id/header"
/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team A"
android:layout_marginLeft="50dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:id="#+id/score_a"
android:layout_marginLeft="90dp"
android:layout_marginTop="10dp"
android:onClick="display_score_a"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:onClick="three_pts_a"
android:padding="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:text="Inside the Ring"
android:layout_marginLeft="40dp"
android:padding="10dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Free Throw"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Team B"
android:layout_marginLeft="250dp"
android:layout_marginTop="80dp"
android:textSize="30sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="50sp"
android:layout_marginLeft="290dp"
android:layout_marginTop="10dp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff6000"
android:text="Outside The Ring"
android:layout_marginLeft="230dp"
android:padding="10dp"
android:layout_marginTop="40dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:text="Inside the Ring"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:padding="10dp"
/>
<Button
android:layout_width="wrap_content"
android:background="#ff6000"
android:layout_height="wrap_content"
android:layout_marginLeft="230dp"
android:layout_marginTop="10dp"
android:text="Free Throw"
android:padding="10dp"
/>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reset"
android:background="#ff6000"
android:layout_gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
****Mainactivity.java****
package android.mytest;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int total_pts1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void three_pts_a(View view)
{
total_pts1 = total_pts1 + 3;
display_score_a(total_pts1);
}
private void display_score_a(int number) {
TextView num = (TextView) findViewById(
R.id.score_a);
num.setText(number);
}
}
its because of the integer variable.you have to parse it to a string or change that line to num.setText(String.valueOf(number));
Related
I have this fragment with 8 card views and 8 images but when I use the app on my phone it lags when i'm on this fragment and this fragment alone, anyone know why that is?
Each card directs to a different activity on click so maybe that is what is causing the lag?
Xml:
<?xml version="1.0" encoding="utf-8"?>
\<ScrollView 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:background="#color/backgroundBlack"
xmlns:app="http://schemas.android.com/apk/res-auto"\>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Inventario"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
</RelativeLayout>
<GridLayout
android:columnOrderPreserved="false"
android:alignmentMode="alignMargins"
android:layout_marginTop="17dp"
android:rowCount="1"
android:columnCount="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardMaquinas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="#+id/receiptImage"
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/maquina"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Maquinas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardTintas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/ink"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/black"
android:text="Tintas"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardVasos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/inkcup"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:layout_gravity="center"
android:text="Vasos de tinta"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardAgujas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/agujas"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Agujas y cartuchos"
android:layout_gravity="center"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardFuentes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/fuentes"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Fuentes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPiel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/piel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Piel sintetica"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPuntas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/cartuchos" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tubos y puntas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardGuantes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/guantes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Guantes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:visibility="invisible">
</Button>
</LinearLayout>
Fragment:
package com.example.tattostudio.ui.home;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.cardview.widget.CardView;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.ViewModelProvider;
import com.example.tattostudio.R;
import com.example.tattostudio.databinding.FragmentHomeBinding;
import com.example.tattostudio.inventariostuff.AgujasInventario;
import com.example.tattostudio.inventariostuff.FuentesInventario;
import com.example.tattostudio.inventariostuff.GuantesInventario;
import com.example.tattostudio.inventariostuff.MaquinasInventario;
import com.example.tattostudio.inventariostuff.PielInventario;
import com.example.tattostudio.inventariostuff.PuntasInventario;
import com.example.tattostudio.inventariostuff.TintasActivity;
import com.example.tattostudio.inventariostuff.VasosInventario;
public class HomeFragment extends Fragment implements View.OnClickListener {
private FragmentHomeBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
HomeViewModel homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
return root;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
androidx.cardview.widget.CardView card1, card2, card3, card4, card5, card6, card7, card8;
card1 = getView().findViewById(R.id.cardMaquinas);
card2 = getView().findViewById(R.id.cardTintas);
card3 = getView().findViewById(R.id.cardVasos);
card4 = getView().findViewById(R.id.cardAgujas);
card5 = getView().findViewById(R.id.cardFuentes);
card6 = getView().findViewById(R.id.cardPiel);
card7 = getView().findViewById(R.id.cardPuntas);
card8 = getView().findViewById(R.id.cardGuantes);
card1.setOnClickListener(this);
card2.setOnClickListener(this);
card3.setOnClickListener(this);
card4.setOnClickListener(this);
card5.setOnClickListener(this);
card6.setOnClickListener(this);
card7.setOnClickListener(this);
card8.setOnClickListener(this);
}
public void onClick(View v){
Intent i;
switch (v.getId()){
case R.id.cardMaquinas:
i = new Intent(getActivity(), MaquinasInventario.class);
startActivity(i);
break;
case R.id.cardTintas:
i = new Intent(getActivity(), TintasActivity.class);
startActivity(i);
break;
case R.id.cardVasos:
i = new Intent(getActivity(), VasosInventario.class);
startActivity(i);
break;
case R.id.cardAgujas:
i = new Intent(getActivity(), AgujasInventario.class);
startActivity(i);
break;
case R.id.cardFuentes:
i = new Intent(getActivity(), FuentesInventario.class);
startActivity(i);
break;
case R.id.cardPiel:
i = new Intent(getActivity(), PielInventario.class);
startActivity(i);
break;
case R.id.cardPuntas:
i = new Intent(getActivity(), PuntasInventario.class);
startActivity(i);
break;
case R.id.cardGuantes:
i = new Intent(getActivity(), GuantesInventario.class);
startActivity(i);
break;
}
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
}
I tried using smaller images but I suspect the lag comes from the code being unoptimized, i'm a begginer developer on android so i'm not really sure what can I do to optimize it and make it run smooth on my phone
Issue with your ScrollView becasuse you closed Scrollview at top without including other UI into it.It should close at the end of XML file.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:background="#color/backgroundBlack"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_marginTop="30dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:text="Inventario"
android:textColor="#color/white"
android:textSize="18dp"
android:layout_centerHorizontal="true"
android:textStyle="bold" />
</RelativeLayout>
<GridLayout
android:columnOrderPreserved="false"
android:alignmentMode="alignMargins"
android:layout_marginTop="17dp"
android:rowCount="1"
android:columnCount="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:id="#+id/cardMaquinas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:id="#+id/receiptImage"
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/maquina"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Maquinas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardTintas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/ink"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textColor="#color/black"
android:text="Tintas"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardVasos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/inkcup"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:layout_gravity="center"
android:text="Vasos de tinta"
android:textSize="13dp"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardAgujas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:elevation="60dp"
app:cardCornerRadius="12dp"
android:layout_margin="12dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/agujas"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Agujas y cartuchos"
android:layout_gravity="center"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardFuentes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:clickable="true"
android:src="#drawable/fuentes"
tools:ignore="SpeakableTextPresentCheck" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Fuentes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPiel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/piel" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Piel sintetica"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardPuntas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/cartuchos" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Tubos y puntas"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/cardGuantes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:elevation="60dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:padding="16dp">
<ImageView
android:layout_width="100dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:src="#drawable/guantes" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Guantes"
android:textColor="#color/black"
android:textSize="13dp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="100dp"
android:visibility="invisible">
</Button>
</LinearLayout>
</ScrollView>
I have been trying to use getResources().getIdentifier() methods to assign ids to an array of images. However, when I consoled the array, I only got 2 valid elements and the rest was null. Could somebody please explain to me why after the onCreate method is executed, the program only consoles the values of the first 2 loops and returns null values afterward?
Currently: [ImageView0, ImageView1, null, null, null, null, null, null, null]
What should I do to make it fully loop through the array and return an array with valid values of [ImageView1, ImageView2, ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8] instead ?
Many thanks! I am appreciated any help!
INPUT
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.widget.ImageView;
import java.util.Arrays;
public class MainActivity extends AppCompatActivity {
ImageView[] computerImages;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
computerImages = new ImageView[9];
for (int i=0; i<computerImages.length; i++){
String computerImageID = "ImageView"+i;
int resID = getResources().getIdentifier(computerImageID,"id",getPackageName());
computerImages[i] = findViewById(resID);
} Log.i("ON CREATE", Arrays.toString(computerImages));
}
OUTPUT
I/ON CREATE: [android.support.v7.widget.AppCompatImageView{fe40fe0 VFED..C.. ......ID 0,0-0,0 #7f070003 app:id/ImageView0}, android.support.v7.widget.AppCompatImageView{8313299 VFED..C.. ......ID 0,0-0,0 #7f070004 app:id/ImageView1}, null, null, null, null, null, null, null]
XML LAYOUT
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="horizontal">
<Button
android:id="#+id/button"
android:layout_width="172dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Computer" />
<Button
android:id="#+id/button2"
android:layout_width="171dp"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:text="Player 02" />
</LinearLayout>
<GridLayout 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:id="#+id/gridLayout"
android:layout_width="match_parent"
android:layout_height="360dp"
android:layout_alignParentRight="true"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#drawable/board"
android:columnCount="3"
android:rowCount="3">
<ImageView
android:id="#+id/imageView0"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="0" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="0dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="1" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="15dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="2" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="3" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="1dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="4" />
<ImageView
android:id="#+id/imageView5"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="5" />
<ImageView
android:id="#+id/imageView6"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="6" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="1dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="7" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="95dp"
android:layout_height="95dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="40dp"
android:layout_marginTop="16dp"
android:alpha="0"
android:onClick="clickToPlay"
android:tag="8" />
</GridLayout>
<Button
android:id="#+id/playAgainButton"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:onClick="clickReset"
android:text="Play Again"
android:textSize="20sp" />
<TextView
android:id="#+id/winnerMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#android:color/holo_orange_dark"
android:padding="5dp"
android:text="TextView"
android:textColor="#android:color/white"
android:textSize="25sp"
android:visibility="invisible" />
</RelativeLayout>
It returning null because "ImageView3, ImageView4, ImageView5, ImageView6, ImageView7, and ImageView8" views are not present in resource file. Only ImageView0, ImageView1 are present in resource file.
]2]2
this is NamazActivity.java In java class i just add cardview click listener and open a toast message. Every click listener shows the iteam ID with simple text
package com.example.shakeelmughal.assanislam;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.CardView;
import android.view.MenuItem;
import android.view.View;
import android.widget.GridLayout;
import android.widget.Toast;
public class NamazActivity extends AppCompatActivity {
GridLayout gridLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_namaz);
//back button
if(getSupportActionBar()!= null)
{
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
gridLayout = findViewById(R.id.gridlayout);
setSingleClick(gridLayout);
}
//function for going back to previous activity
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId() == android.R.id.home)
finish();
return super.onOptionsItemSelected(item);
}
private void setSingleClick(GridLayout gridLayout) {
for (int i = 0; i < gridLayout.getChildCount(); i++) {
CardView cardView = (CardView) gridLayout.getChildAt(i);
final int s = i;
cardView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(NamazActivity.this, "Item " + s + " Clicked", Toast.LENGTH_SHORT).show();
}
});
}
}
}
this is nazamactivity.xml In XML class i use gridlayout and inside it i use cardview with a text view and a image view. I have make 3 card in it...
<?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"
tools:context="com.example.shakeelmughal.assanislam.NamazActivity"
android:orientation="vertical"
android:weightSum="5">
<RelativeLayout
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/textGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:textColor="#000"
android:layout_centerInParent="true"
android:text="نماز"/>
</RelativeLayout>
<GridLayout
android:id="#+id/gridlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="4"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3">
<!-- Row 1 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/tahart"
android:paddingRight="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text= " Ùرض نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25sp"
android:textColor="#000"
android:text="5"
android:paddingLeft="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/namaz"
android:paddingRight="70dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="واجب نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:text="3"
android:textSize="25sp"
android:paddingLeft="10dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="80dp"
android:src="#drawable/namaz" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ù†ÙÙ„ نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp"
android:paddingRight="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textSize="25sp"
android:textColor="#000"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
Try this Make ScrollView as your parent layout it will work
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textGrid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="نماز"
android:textColor="#000"
android:textSize="40sp" />
</RelativeLayout>
<GridLayout
android:id="#+id/gridlayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="4"
android:alignmentMode="alignMargins"
android:columnCount="1"
android:columnOrderPreserved="false"
android:padding="14dp"
android:rowCount="3">
<!-- Row 1 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingRight="70dp"
android:src="#drawable/ic_message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" Ùرض نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="5"
android:textColor="#000"
android:textSize="25sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 2 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingRight="70dp"
android:src="#drawable/ic_message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="واجب نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:text="3"
android:textColor="#000"
android:textSize="25sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
<!-- Row 3 -->
<!-- Col 1 -->
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_columnWeight="1"
android:layout_marginBottom="15dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:layout_rowWeight="1"
app:cardCornerRadius="8dp"
app:cardElevation="8dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:paddingLeft="10dp"
android:paddingRight="80dp"
android:src="#drawable/ic_message" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="20dp"
android:text="Ù†ÙÙ„ نمازیں"
android:textAlignment="center"
android:textColor="#000"
android:textSize="25sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:textColor="#000"
android:textSize="25sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
My project won't open on my device, it say's "Unfortunately,myApp has stopped".
this is my manifest:
this is my activity java :
package bismillah.project;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.content.Intent;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TextView;
public class Project2Activity extends Activity {
public static int a,b,c;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//coding start here!!
//input data
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
//button proses
Button button1=(Button)findViewById(R.id.Button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Spinner bibit = (Spinner)findViewById(R.id.spinner1);
TextView biaya = (TextView)findViewById(R.id.textView6);
if(bibit.getSelectedItem().toString().equals("Cabai Rp.100")){
a=pekerja*(lama*upah)+(jumlah*100);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Tomat Rp.150")){
a=pekerja*(lama*upah)+(jumlah*150);
biaya.setText("Biaya Rp." + a);
if(bibit.getSelectedItem().toString().equals("Timun Rp.200")){
a=pekerja*(lama*upah)+(jumlah*200);
biaya.setText("Biaya Rp." + a);
}
}
}
}
});
//button reset
Button button2=(Button)findViewById(R.id.Button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
TextView biaya = (TextView)findViewById(R.id.textView6);
EditText pek=(EditText)findViewById(R.id.editText1);
EditText lam=(EditText)findViewById(R.id.editText2);
EditText up=(EditText)findViewById(R.id.editText3);
EditText jum=(EditText)findViewById(R.id.editText4);
pek.setText("");
lam.setText("");
up.setText("");
jum.setText("");
biaya.setText("");
}
});
//button pindah activity
Button button3=(Button)findViewById(R.id.Button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i =new Intent(getApplicationContext(),activity2.class);
startActivity(i);
}
});
}
}
and this is my main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/bg"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="#string/hello"
android:textColor="#color/warna"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:entries="#array/list" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textColor="#color/warna2"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
I tried your code and I had to delete some stuff from your XML file because it was creating some bugs. It might be related to me but here's your XML I was able to use:
But please do not change your XML file before trying the solution below. If it still doesn't work, you should consider the elements I removed.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="hello"
android:textSize="20dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="Jumlah Pekerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Lama Kerja :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Upah :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jenis Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:text="Jumlah Bibit :"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:text="Proses" />
<Button
android:id="#+id/Button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Reset" />
<Button
android:id="#+id/Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hasil Panen" />
</LinearLayout>
</LinearLayout>
Next I created your Project2Activity java class and activity2 (pretty much empty since you gave no info about it).
So when trying to run your application you get the following error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.application.so/com.example.application.so.Project2Activity}: java.lang.NumberFormatException: For input string: ""
and it is at :
at com.example.application.so.Project2Activity.onCreate(Project2Activity.java:24)
now if we consider this lines 23 and 24:
String value=pek.getText().toString(); // line 23
final int pekerja=Integer.parseInt(value); // line 24
The error is that you are trying to read (line 23) your editText Value at the creation of the activity, at that time, your editText is still blank which means value will be equal to "" (empty string).
What you want to do instead is to put your lines from 22 to 33 (the ones below) in a button click event, I tried putting them in your button 1 click event and your app doesn't crash any more:
EditText pek=(EditText)findViewById(R.id.editText1);
String value=pek.getText().toString();
final int pekerja=Integer.parseInt(value);
EditText lam=(EditText)findViewById(R.id.editText2);
String value1=lam.getText().toString();
final int lama=Integer.parseInt(value1);
EditText up=(EditText)findViewById(R.id.editText3);
String value2=up.getText().toString();
final int upah=Integer.parseInt(value2);
EditText jum=(EditText)findViewById(R.id.editText4);
String value3=jum.getText().toString();
final int jumlah=Integer.parseInt(value3);
So to summarize, the problem is that you are extracting empty strings from your editTexts and trying to make integers out of them. Instead try making that all reading and integer extracting process on the click of a button where your really need these inputs. just like get the inputs and the final moment.
You should pay attention to your logcat (error messages), it is quite helpful.
What you can also do is to verify that your editTexts are not empty while trying to extract strings from them and raise exception or Toast if they are empty. Nothing too complicated just something like:
if (value.equals("")):
// editText is empty so do something
You can refer to this link for further understanding of how to know when an editText is empty.
I have a slight problem that I have been stuck on. I have narrowed it down and it seems that my application is crashing on create when i am trying to find a text view. I do not understand why, maybe i have been looking at this to long and dont see the obvious.
Any help would be appreciated.
Thanks.
public class FinalActivity extends Activity {
EditText fullName;
EditText serialNumber;
EditText numberOfTicket;
EditText fullCost;
EditText discount;
EditText totalCostOfTickets;
EditText dinnerYesNo;
EditText numberForDinner;
EditText dinnerCost;
EditText vatTv;
EditText totalCost;
SharedPreferences thePrefs;// Shared preferences variable
SharedPreferences.Editor editor;// New editor to add to shared preferences
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_final);
thePrefs = getSharedPreferences(LoginActivity.FIRSTTIMEPREFS, 0);
editor = thePrefs.edit();
fullName = (EditText)findViewById(R.id.personsNameTv);
/*serialNumber = (EditText)findViewById(R.id.serialNumberTv);
numberOfTicket = (EditText)findViewById(R.id.numberOfTicketTv);
fullCost = (EditText)findViewById(R.id.fullCostBeforeTv);
discount = (EditText)findViewById(R.id.discountAmountTV);
totalCostOfTickets = (EditText)findViewById(R.id.totalTicketCostEt);
dinnerYesNo = (EditText)findViewById(R.id.dinnerAvailedTv);
numberForDinner = (EditText)findViewById(R.id.numberForDinnerTv);
dinnerCost = (EditText)findViewById(R.id.dinnerCostTv);
vatTv = (EditText)findViewById(R.id.vatTv);
totalCost = (EditText)findViewById(R.id.totalCostTv);*/
//fullName.setText (thePrefs.getString(PersonalInfoActivity.FULLNAME, ""));
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#404040"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<TextView
android:id="#+id/mainTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginTop="15dp"
android:text="#string/ticketSummary"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/promptTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="5dp"
android:text="#string/confirmCorrectDetails"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<ScrollView
android:id="#+id/containerLayout"
android:layout_width="200dp"
android:layout_height="300dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/redborder" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:paddingBottom="30dp"
android:paddingLeft="15dp"
android:paddingRight="10dp"
android:paddingTop="15dp" >
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/serialNumberTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/personsNameTv"
android:layout_marginTop="10dp"
android:text="#string/serialNumber"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberOfTicketTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/serialNumberTv"
android:layout_marginTop="10dp"
android:text="#string/numberOfTickets"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/fullCostBeforeTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberOfTicketTv"
android:layout_marginTop="10dp"
android:text="#string/fullCostBeforeDiscount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/discountAmountTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/fullCostBeforeTv"
android:layout_marginTop="10dp"
android:text="#string/amountOfDisount"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalTicketCostEt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/discountAmountTV"
android:layout_marginTop="10dp"
android:text="#string/totalTicketCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerAvailedTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/totalTicketCostEt"
android:layout_marginTop="10dp"
android:text="#string/dinnerAvailed"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/numberForDinnerTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerAvailedTv"
android:layout_marginTop="10dp"
android:text="#string/numberForDinner"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/dinnerCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/numberForDinnerTv"
android:layout_marginTop="10dp"
android:text="#string/dinnerCost"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/vatTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dinnerCostTv"
android:layout_marginTop="10dp"
android:text="#string/vat"
android:textColor="#FFFFFF"
android:textSize="15sp" />
<TextView
android:id="#+id/totalCostTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vatTv"
android:layout_marginTop="10dp"
android:text="#string/totalCost"
android:textStyle="bold"
android:textColor="#FFFFFF"
android:textSize="15sp" />
</RelativeLayout>
</ScrollView>
<Button
android:id="#+id/confirmBtn"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_below="#+id/containerLayout"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="15dp"
android:background="#drawable/buttonshape"
android:onClick="confirmTicketOptions"
android:text="#string/confirm"
android:textStyle="bold" />
</LinearLayout>
There are two possible things possible. Either you want all Textviews or all EditText.
If you want all Textview then replace all your EditTextin java file with Textview.
Textview fullName;
fullName = (Textview )findViewById(R.id.personsNameTv);
If you want all EditText then replace all Textview in xml with EditText.
<EditText
android:id="#+id/personsNameTv"
.../>
In XML you are using Text-view and in java file you define it Edit-text
<TextView
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />
Replace with
<EditText
android:id="#+id/personsNameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fullName"
android:textColor="#FFFFFF"
android:textSize="15sp" />