I've made a checklist app and when the user add's the check to the app I get the below error and a crash. Can someone provide some advice to how to fix this? As i'm using fragments, i've created a second Java file and refernced that to the XML as you cannot have more then one class in a single Java, i've tried merging them but get lots of errors and cannot use the public class 'extends' more then once.
Here's the error
Heres the XML
<TextView
android:id="#+id/text_checklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Checklist"
android:textAlignment="center"
android:textAllCaps="false"
android:textSize="24sp"
android:textStyle="bold"
android:typeface="normal"
app:fontFamily="#font/basic"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.491"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/text_checklist"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="369dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="13dp"
android:ems="10"
android:hint="#string/placeholder"
android:inputType="textPersonName" />
<Button
android:id="#+id/check_list_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="57dp"
android:onClick="addButton2"
android:text="#string/buttonText" />
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginLeft="0dp"
android:layout_marginTop="107dp" />
Heres the 1st Java (Fragment)
checklistViewModel =
ViewModelProviders.of(this).get(com.example.csd301_project_trackday_timing_dh.ui.checklist.ChecklistViewModel.class);
View root = inflater.inflate(R.layout.fragment_checklist, container, false);
final TextView textView = root.findViewById(R.id.text_checklist);
checklistViewModel.getText().observe(this, new Observer<String>() {
#Override
public void onChanged(#Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
Heres the 2nd Java (CheckList)
Button btn_checklist;
EditText inputText;
ListView listView;
ArrayList<String> list;
public void addButton2(View view){
String text = inputText.getText().toString();
list.add(text);
ArrayAdapter adapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1,list);
listView.setAdapter(adapter);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_checklist);
btn_checklist= findViewById(R.id.check_list_button);
inputText=findViewById(R.id.inputText);
listView=findViewById(R.id.listView);
list = new ArrayList<>();
}
}
Related
Trying to make two login, one for admin, one for user. They share the same layout, but admin has additional TextFields and Buttons to add data to the menu. So when logging in with user credentials, the TextFields and Buttons will be setVisibility(View.GONE), but it's not working at all, in fact it's showing up like setVisibility(View.GONE) was added at all. Any advice would be appreciated.
loginpage.java
DatabaseHelper myDB;
EditText LoginEMail;
EditText LoginPassword;
Button LoginBtn;
LinearLayout linearLayout;
View add_image;
View add_name;
View add_desc;
View add_data;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
LoginEMail = findViewById(R.id.LoginEMail);
LoginPassword = findViewById(R.id.LoginPassword);
LoginBtn = findViewById(R.id.LoginBtn);
myDB = new DatabaseHelper(this);
linearLayout = findViewById(R.id.linearLayout);
linearLayout = new LinearLayout(this);
add_image = new View(this);
add_name = new View(this);
add_desc = new View(this);
add_data = new View(this);
}
public void Login(View view) {
Intent intent = new Intent(loginpage.this, MenuSelection.class);
if (LoginEMail.getText().toString().equals("admin") && (LoginPassword.getText().toString().equals("admin"))) {
startActivity(intent);
linearLayout.setVisibility(View.VISIBLE);
}
else if(LoginEMail.getText().toString().equals("user") && (LoginPassword.getText().toString().equals("user"))) {
startActivity(intent);
linearLayout.setVisibility(View.GONE);
add_image.setVisibility(View.GONE);
add_name.setVisibility(View.GONE);
add_desc.setVisibility(View.GONE);
add_data.setVisibility(View.GONE);
}
else
Toast.makeText(loginpage.this, "Incorrect E-mail or Password.", Toast.LENGTH_SHORT).show();
}
loginpage.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/LoginEMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="E-Mail"
android:inputType="textEmailAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.204" />
<EditText
android:id="#+id/LoginPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Password"
android:inputType="textPassword"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.495"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.338" />
<Button
android:id="#+id/LoginBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.502"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.602"
android:onClick="Login"/>
</androidx.constraintlayout.widget.ConstraintLayout>
menu_selection.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<ListView
android:id="#+id/menu_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0">
<ImageButton
android:id="#+id/add_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:src="#color/black" />
<EditText
android:id="#+id/add_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Name" />
<EditText
android:id="#+id/add_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Description" />
<Button
android:id="#+id/add_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Instead of add_image = new View(this); you need to associate the variable add_image to the view in your XML layout using the findViewById(R.id.add_image) method. Similarly for the other 3 views that you have initialized this way.
You also need to delete the line linearLayout = new LinearLayout(this);. The previous line setting this variable is correct.
I have two EditText in MyActivity. Here I have provided UI XML.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button_insta"
android:layout_width="135dp"
android:layout_height="0dp"
android:layout_marginBottom="33dp"
android:text="Download"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<EditText
android:id="#+id/editUrli"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="64dp"
android:layout_marginBottom="18dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textColor="#0C0C0C"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView2"
android:layout_width="177dp"
android:layout_height="39dp"
android:layout_marginEnd="81dp"
android:layout_marginRight="81dp"
android:text="Paste Instagram video link to download "
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="#+id/imageView3"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView3"
app:layout_constraintTop_toTopOf="#+id/imageView3" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="78dp"
android:layout_height="43dp"
android:layout_marginStart="41dp"
android:layout_marginLeft="41dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="18dp"
android:layout_marginRight="18dp"
android:src="#drawable/insta"
app:layout_constraintEnd_toStartOf="#+id/textView2"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/card"
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_margin="8dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="MissingConstraints">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/editUrl"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="64dp"
android:layout_marginBottom="18dp"
android:ems="10"
android:inputType="textPersonName"
android:text=""
android:textColor="#0C0C0C"
app:layout_constraintBottom_toTopOf="#+id/button_download"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_download"
android:layout_width="135dp"
android:layout_height="0dp"
android:layout_marginBottom="33dp"
android:text="Download"
android:onClick="onClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<ImageView
android:id="#+id/fblogo"
android:layout_width="95dp"
android:layout_height="39dp"
android:layout_marginStart="16dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="13dp"
android:layout_marginEnd="18dp"
android:layout_marginRight="18dp"
android:layout_marginBottom="13dp"
android:contentDescription="TODO"
android:src="#drawable/fb"
app:layout_constraintBottom_toTopOf="#+id/editUrl"
app:layout_constraintEnd_toStartOf="#+id/textView"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="75dp"
android:layout_marginRight="75dp"
android:layout_marginBottom="84dp"
android:text="Paste FB video link to download"
android:textStyle="bold"
app:layout_constraintBottom_toTopOf="#+id/button_download"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/fblogo"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.card.MaterialCardView>
In my Activity java code has this button click code
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_download);
inputURl = findViewById(R.id.editUrl);
editUrlinsta = findViewById(R.id.editUrli);
BtnDownload = (Button) findViewById(R.id.button_download);
downloadinsta = (Button) findViewById(R.id.button_insta);
BtnDownload.setOnClickListener(this);
downloadinsta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.button_download:
try{
final FacebookDownloader downloaders = new FacebookDownloader(Download.this,inputURl.getText().toString());
downloaders.DownloadVideo();
inputURl.getText().clear();
} catch(Exception e) {}
break;
case R.id.button_insta:
final InstaDownloader downloaderInsta = new InstaDownloader(Download.this,editUrlinsta.getText().toString());
downloaderInsta.DownloadVideo();
editUrlinsta.getText().clear();
break;
default:
break;
}
}
The problem is The first button which I have named button_download is working as expected. But the second button button_insta not working as expected. what I want to do, is When one of the buttons clicks, getting data from EditText. The button_download works fine. the button_insta is not working fine.
I couldn't get value from editUrli when the button click. But same code works for button_download and editUrl
Try to separate those button click function like so :
BtnDownload.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
try{
final FacebookDownloader downloaders = new FacebookDownloader(Download.this,inputURl.getText().toString());
downloaders.DownloadVideo();
inputURl.getText().clear();
} catch(Exception e) {}
}
});
downloadinsta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
final InstaDownloader downloaderInsta = new InstaDownloader(Download.this,editUrlinsta.getText().toString());
downloaderInsta.DownloadVideo();
editUrlinsta.getText().clear();
}
});
My MainActivity
public class MainActivity extends AppCompatActivity {//My MainActivity
ArrayList<Response> responses = new ArrayList<>();
private String BASE_URL = "https://covid-193.p.rapidapi.com/";
RecyclerView recyclerView;
MyRecycleViewAdapter myRecycleViewAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
recyclerView = findViewById(R.id.recycleViewCovid);
Retrofit.Builder builder = new Retrofit.Builder()
.baseUrl(BASE_URL)
.addConverterFactory(GsonConverterFactory.create());//this is convert json
Retrofit retrofit = builder.build();
Covid19WebApi covid19WebApi = retrofit.create(Covid19WebApi.class);
Call<Covid19Model> call = covid19WebApi.getData();//this is call api interfacee method
call.enqueue(new Callback<Covid19Model>() {
#Override
public void onResponse(Call<Covid19Model> call, retrofit2.Response<Covid19Model> response) {
responses = (ArrayList<Response>) response.body().getResponse();//This is my Api response 232 items
LinearLayoutManager layoutManager = new LinearLayoutManager(MainActivity.this);
recyclerView.setLayoutManager(layoutManager);
myRecycleViewAdapter = new MyRecycleViewAdapter(responses);//My Adapter passing data in my Adapter
recyclerView.setAdapter(myRecycleViewAdapter);
}
#Override
public void onFailure(Call<Covid19Model> call, Throwable t) {
Toast.makeText(MainActivity.this,t.getLocalizedMessage(),Toast.LENGTH_LONG).show();//this is toast message failure
}
});
}
My Adapter
public class MyRecycleViewAdapter extends RecyclerView.Adapter<MyRecycleViewAdapter.ViewHolder>
{
private ArrayList<Response> responseArrayList;
public MyRecycleViewAdapter ( ArrayList<Response> responseArrayList) {
this.responseArrayList = responseArrayList;//Response data items in My Adapter
}
#NonNull
#Override
public MyRecycleViewAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(parent.getContext());//Api passing data added this layout
View view = layoutInflater.inflate(R.layout.row_layout,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull MyRecycleViewAdapter.ViewHolder holder, int position) {
holder.setData(responseArrayList.get(position));//Api Items List
}
#Override
public int getItemCount() {
return responseArrayList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
TextView continent;
TextView country;
TextView population;
TextView casesNew;
TextView caseActive;
TextView caseCritical;
TextView caseRecovered;
TextView caseM_pop;
TextView caseTotal;
TextView deathsNew;
TextView deathsM_pop;
TextView deathsTotal;
TextView testsM_pop;
TextView testsTotal;
TextView day;
TextView time;
TextView Deaths;
TextView Tests;
TextView Cases;
public ViewHolder(#NonNull View itemView) {//My ViewHolder Connect Api data and View Layout TextView
super(itemView);
continent = itemView.findViewById(R.id.continentName);//My layout Textview id
country = itemView.findViewById(R.id.countryName);
population =itemView.findViewById(R.id.populationName);
casesNew = itemView.findViewById(R.id.CaseNewName);
caseActive = itemView.findViewById(R.id.casesActiveName);
caseCritical=itemView.findViewById(R.id.criticalName);
caseRecovered=itemView.findViewById(R.id.recoveredName);
caseM_pop = itemView.findViewById(R.id.M_popName);
caseTotal = itemView.findViewById(R.id.totalCases);
deathsNew = itemView.findViewById(R.id.deathsNewName);
deathsM_pop = itemView.findViewById(R.id.deaths1M_pop);
deathsTotal = itemView.findViewById(R.id.totalDeaths);
testsM_pop = itemView.findViewById(R.id.tests1M_pop);
testsTotal = itemView.findViewById(R.id.testsTotal);
day = itemView.findViewById(R.id.dayName);
time = itemView.findViewById(R.id.timeName);
Cases = itemView.findViewById(R.id.Cases);
Deaths = itemView.findViewById(R.id.Deaths);
Tests = itemView.findViewById(R.id.Tests);
}
private void setData(Response response){
Response res = response;
continent.setText(res.getContinent());//Debugging this TextView Api data is null
country.setText(res.getCountry());
population.setText(res.getPopulation());
casesNew.setText(res.getCases().getNew());
caseActive.setText(res.getCases().getActive());
caseCritical.setText(res.getCases().getCritical());
caseRecovered.setText(res.getCases().getRecovered());
caseM_pop.setText(res.getCases().get1MPop());
caseTotal.setText(res.getCases().getTotal());
deathsM_pop.setText(res.getDeaths().get1MPop());
deathsNew.setText(res.getDeaths().getNew());
deathsTotal.setText(res.getDeaths().getTotal());
testsM_pop.setText(res.getTests().get1MPop());
testsTotal.setText(res.getTests().getTotal());
day.setText(res.getDay());
time.setText(res.getTime());
Tests.setText(res.getTests().toString());
Cases.setText(res.getCases().toString());
Deaths.setText(res.getDeaths().toString());
}
}
}
My Problems
Please Help me What is the Problem?
I pull the data with the Api, but when I debug, I see that the data comes to My MainActivity and is added to the Adapter from there, but when the data reaches the Viewholder, it becomes null when set to the TextView. I don't understand what the problem is.
My Layout
<?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"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#8BC34A"
android:orientation="vertical">
<TextView
android:id="#+id/continentName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:hint="continent"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/countryName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="country"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/continentName" />
<TextView
android:id="#+id/populationName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="population"
android:textColor="#color/black"
android:textSize="14sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/countryName" />
<TextView
android:id="#+id/dayName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="day"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/populationName" />
<TextView
android:id="#+id/timeName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="time"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/dayName" />
<TextView
android:id="#+id/CaseNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/timeName" />
<TextView
android:id="#+id/casesActiveName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="active"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/CaseNewName" />
<TextView
android:id="#+id/criticalName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="critical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/casesActiveName" />
<TextView
android:id="#+id/recoveredName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="recovered"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/criticalName" />
<TextView
android:id="#+id/M_popName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/recoveredName" />
<TextView
android:id="#+id/totalCases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/M_popName" />
<TextView
android:id="#+id/deaths1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalCases" />
<TextView
android:id="#+id/deathsNewName"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="new(Deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deaths1M_pop" />
<TextView
android:id="#+id/totalDeaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="total(deaths)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/deathsNewName" />
<TextView
android:id="#+id/tests1M_pop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="1M_pop"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalDeaths" />
<TextView
android:id="#+id/testsTotal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="total(tests)"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tests1M_pop" />
<TextView
android:id="#+id/Deaths"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Deaths"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/testsTotal" />
<TextView
android:id="#+id/Tests"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:hint="Tests"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Deaths" />
<TextView
android:id="#+id/Cases"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:hint="Cases"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Tests" />
</LinearLayout>"
I have a void method synchronizeNum(String String String) Which will be trigger in onViewCreated , This Line.
new DisDepartmentSender(myView.getContext(), urlAddress, accountEmail, departmentName).execute();
It will get data from database and finally pass back and set the data into this method synchronizeNum (String String String).After i set the data i wish to display it using TextView. I think the only way is execute again onViewCreated method, since we cannot setText inside synchronizeNum method.I have tried many detach and attach method inside the method but the problem is it get NullPointer i donno how to solve i have been looking around for 1 day just for this little problem. I need you guys help please.
CurrentDepartmentView FragmentClass
package com.example.cheng.freequeue.DisplayDepartmentStatus;
import android.app.Fragment;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.example.cheng.freequeue.R;
/**
* Created by cheng on 11/4/2018.
*/
public class CurrentStatusView extends Fragment {
DisDepartmentList disDepartmentList=new DisDepartmentList();
final static String urlAddress = "https://kokcheng95.000webhostapp.com/departmentStatus.php";
private String CompanyName, accountEmail, departmentName, imageUrl;
private String num1room1, num2room2, num3room3, num1, num2, num3, room1, room2, room3;
private final Handler handler = new Handler();
View myView;
Context c;
ViewGroup container;
private LayoutInflater inflater;
TextView company_name, department_name, num_1, num_2, num_3, room_1, room_2, room_3;
de.hdodenhof.circleimageview.CircleImageView image_department;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, Bundle savedInstanceState) {
myView = inflater.inflate(R.layout.current_department_view, container, false);
this.container=container;
company_name = (TextView) myView.findViewById(R.id.company_name);
department_name = (TextView) myView.findViewById(R.id.department_name);
image_department = (de.hdodenhof.circleimageview.CircleImageView) myView.findViewById(R.id.image_department);
num_1 = (TextView) myView.findViewById(R.id.number1);
num_1 = (TextView) myView.findViewById(R.id.number1);
num_2 = (TextView) myView.findViewById(R.id.number2);
num_3 = (TextView) myView.findViewById(R.id.number3);
room_1 = (TextView) myView.findViewById(R.id.room1);
room_2 = (TextView) myView.findViewById(R.id.room2);
room_3 = (TextView) myView.findViewById(R.id.room3);
new DisDepartmentSender(myView.getContext(), urlAddress, accountEmail, departmentName).execute();
//Set data
company_name.setText("Company Name : " + CompanyName);
department_name.setText("Department : " + departmentName);
PicassoClientDepartment.dowloadImage(myView.getContext(), imageUrl, image_department);
//Set Data
num_1.setText(num1);
num_2.setText(num1);
num_3.setText(num1);
room_1.setText(room1);
room_2.setText(room2);
room_3.setText(room3);
return myView;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bundle bundle = this.getArguments();
if (bundle != null) {
CompanyName = bundle.getString("CompanyName", "noText");
accountEmail = bundle.getString("accountEmail", "noText");
departmentName = bundle.getString("departmentName", "noText");
imageUrl = bundle.getString("imageUrl", "noText");
}
}
public void synchronizeNum(String num1room1, String num2room2, String num3room3) {
String path1[] = num1room1.split("_");
String path2[] = num2room2.split("_");
String path3[] = num3room3.split("_");
num1 = path1[0];
room1 = path1[1];
num2 = path2[0];
room2 = path2[1];
num3 = path3[0];
room3 = path3[1];
/* Fragment currentFragment = getActivity().getFragmentManager().findFragmentByTag("CurrentStatusView");
FragmentTransaction fragTransaction = (getActivity()).getFragmentManager().beginTransaction();
fragTransaction.detach(currentFragment);
fragTransaction.attach(currentFragment);
fragTransaction.commit();*/
}
public void setNum1room1(String num1room1) {
this.num1room1 = num1room1;
}
public String getNum1room1() {
return num1room1;
}
public void setNum2room2(String num2room2) {
this.num2room2 = num2room2;
}
public String getNum2room2() {
return num2room2;
}
public void setNum3room3(String num3room3) {
this.num3room3 = num3room3;
}
public String getNum3room3() {
return num3room3;
}
}
current_department_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.cheng.freequeue.LoginPage">
<TextView
android:id="#+id/department_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="4dp"
android:text="Department : Orthopedic"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.163" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.515"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.458">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/textView6"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape"
android:text="Number"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="40dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView5"
android:layout_width="150dp"
android:layout_height="80dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape"
android:text="Room"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="40dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp">
<TextView
android:id="#+id/number1"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="1001"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/room1"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="2"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="30dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp">
<TextView
android:id="#+id/number2"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="1002"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/room2"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="2"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="30dp"
android:textStyle="bold" />
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="-5dp">
<TextView
android:id="#+id/number3"
android:layout_width="200dp"
android:layout_height="50dp"
android:layout_margin="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="1003"
android:textAlignment="center"
android:textColor="#000000"
android:textSize="30dp"
android:textStyle="bold" />
<TextView
android:id="#+id/room3"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginTop="0.1sp"
android:background="#drawable/cell_shape_body"
android:text="2"
android:textAlignment="center"
android:textColor="#080808"
android:textSize="30dp"
android:textStyle="bold" />
</TableRow>
</TableLayout>
<Button
android:id="#+id/btn_booking"
android:layout_width="350dp"
android:layout_height="50dp"
android:background="#d7b9b9b9"
android:text="BOOKING"
android:textColor="#ffffff"
android:textSize="20dp"
android:textStyle="bold"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.965" />
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/image_department"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.015"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.048"
app:civ_border_color="#ffffff"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/company_name"
android:layout_width="250dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Company Name: TNB"
android:textSize="20dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.064" />
</android.support.constraint.ConstraintLayout>
This Line Error ,NullPointer
Fragment currentFragment=getActivity().getFragmentManager().findFragmentByTag("CurrentStatusView");
Logcat error
04-12 20:52:13.112 3021-3021/com.example.cheng.freequeue E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.cheng.freequeue.DisplayDepartmentStatus.CurrentStatusView.synchronizeNum(CurrentStatusView.java:104)
at com.example.cheng.freequeue.DisplayDepartmentStatus.DisDepartmentParse.onPostExecute(DisDepartmentParse.java:64)
at com.example.cheng.freequeue.DisplayDepartmentStatus.DisDepartmentParse.onPostExecute(DisDepartmentParse.java:18)
at android.os.AsyncTask.finish(AsyncTask.java:631)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5166)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:745)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:561)
at dalvik.system.NativeStart.main(Native Method)
I recommend to create an interface for updating data.
public interface SynchronizeCallback {
void synchronizeNum(String num1room1, String num2room2, String num3room3);
}
Your fragment should implement this interface.
Also set the text to the TextViews inside synchronizeNum():
num_1.setText(num1);
num_2.setText(num1);
num_3.setText(num1);
room_1.setText(room1);
room_2.setText(room2);
room_3.setText(room3);
Pass the interface to your task when you create it:
new DisDepartmentSender(myView.getContext(), urlAddress, accountEmail, departmentName, this).execute();
In onPostExecute of your AsyncTask call
syncronizeCallback.updateValues(newNum1room1, newNum2room2, newNum3room3);
Then you should see the new data.
MainActivity.java (First Class)
package com.example.we.reportcardjavaclass;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public EditText one, two, three, four, five, six;
public String bang1, bang2, bang3, bang4, bang5, bang6;
public Button results;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Find the View that shows the numbers category
}
public void getResult(View view){
results = (Button) findViewById(R.id.result);
Intent numbersIntent = new Intent(MainActivity.this, result.class);
startActivity(numbersIntent);
one = (EditText) findViewById(R.id.name);
bang1 = one.getText().toString();
two = (EditText) findViewById(R.id.Class);
bang2 = two.getText().toString();
three = (EditText) findViewById(R.id.English);
bang3 = three.getText().toString();
four = (EditText) findViewById(R.id.Hindi);
bang4 = four.getText().toString();
five = (EditText) findViewById(R.id.Maths);
bang5 = five.getText().toString();
six = (EditText) findViewById(R.id.Science);
bang6 = six.getText().toString();
}
}
activity_main.xml (First Layout File)
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.we.reportcardjavaclass.MainActivity"
android:padding="16dp">
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Student's Name"
android:id="#+id/name"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="8dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Class"
android:id="#+id/Class"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="64dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in English"
android:id="#+id/English"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="128dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Hindi"
android:id="#+id/Hindi"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="192dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Maths"
android:id="#+id/Maths"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="256dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<EditText
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="Grade in Science"
android:id="#+id/Science"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="320dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:text="Get result"
android:id="#+id/result"
android:onClick="getResult"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
android:layout_marginRight="8dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="384dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"/>
</android.support.constraint.ConstraintLayout>
</ScrollView>
## result.xml (Second Layout File) ##
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
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:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="32dp">
<android.support.v7.widget.CardView
android:id="#+id/resultCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
android:layout_gravity="center_vertical"
card_view:cardUseCompatPadding="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingTop="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Student's Name"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp"
android:text="Class"
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:textAllCaps="false"
android:textSize="15sp"
android:textStyle="normal"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="160dp"
android:background="#color/cardview_dark_background"
android:padding="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:text="English"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="40dp"
android:text="Hindi"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="40dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="80dp"
android:text="Maths"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/five"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="80dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="120dp"
android:text="Science"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="bold"
/>
<TextView
android:id="#+id/six"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="120dp"
android:textAllCaps="true"
android:textColor="#android:color/white"
android:textStyle="normal"/>
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:text="BACK TO HOME"
/>
</FrameLayout>
</android.support.v7.widget.CardView>
</LinearLayout></ScrollView>
Second Class
package com.example.we.reportcardjavaclass;
import android.os.Bundle;
import android.widget.TextView;
/**
* Created by we on 08-07-2017.
*/
public class result extends MainActivity{
public TextView one, two, three, four, five, six;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
setTitle(bang1+"s Result");
one = (TextView) findViewById(R.id.one);
one.setText(bang1);
two = (TextView) findViewById(R.id.two);
two.setText(bang2);
three = (TextView) findViewById(R.id.three);
three.setText(bang3);
four = (TextView) findViewById(R.id.four);
four.setText(bang4);
five = (TextView) findViewById(R.id.five);
five.setText(bang5);
six = (TextView) findViewById(R.id.six);
six.setText(bang6);
}
}
EditText values are getting stored MainActivity.java in variables bang - 123456. I added public modifier while declaring them so I can access these in second class too. In second class I want to use these variable values to set in into Textviews but while running it is not showing the value.
I have also attached the screenshots of both layouts after running in my emulator.
Screenshot 1
Screenshot 2
In second layout it is supposed to show name and other values.
I would suggest that you take a few steps back and get a better understanding of Android's fundamentals, including Activities in general. Perhaps a good place to start, would be understanding the Activity Lifecycle.
An answer to your question isn't very simple, because the way in which Android works is likely very different from your understanding.
In short, your second activity is a completely different instance of a class than the first. Extending the second from the first does not mean that the assigned variables are available to the second, it merely means that the class inherits the structure of its parent in the form of methods and fields.
You should instead be designing your activities in such a way that they relay those strings. When launching your second activity, include those strings in the intent when starting it.
For instance:
public class MainActivity extends AppCompatActivity {
// ...
/** Your method where the second activity is launched */
public void onButtonClicked() {
Intent intent = new Intent(this, SecondActivity.class);
intent.putString(SecondActivity.EXTRA_BANG1, bang1);
// ...
startActivity(intent);
}
}
public class SecondActivity extends AppCompatActivity {
public static final String EXTRA_BANG1 = "bang1";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result);
// ...
TextView tvOne = (TextView) findViewById(R.id.one);
// ...
String bang1 = getIntent().getString(EXTRA_BANG1);
tvOne.setText(bang1);
}
}
I thoroughly recommend reading up in the Android documentation (it's excellently written!) to better understand how (and why) it works through this mechanism. In particular, this article on Sending data between activities is very relevant to you.
If you want to use these values in another activity then simply pass these values along with the intent that is used to start a new activity.
MainActivity.java
Intent i = new Intent(MainActivity.this,SecondActivity.class);
i.putExtra("Bang1",bang1);
i.putExtra("Bang2",bang2);
i.putExtra("Bang3",bang3);
...
And you can get them in the SecondActivity.java file as:
Intent i =getIntent();
int bang1 = i.getIntExtra("Bang1");
...
But instead so choosing the string names as shown i would suggest to create a new class to store the the string names as final string constants in order to avoid any kind of mistake.
Or you can directly declare these variables as static and access them in the SecondActivity by referring to their names as :
int bang1 = MainActivity.bang1;