[英]Moving java from MainActivity.java to HomeFragment.java causing errors thought the same code
我最近創建了一個帶有底部導航模板的新應用程序。 我將代碼放在 activity_main.xml 和 MainActivity.java 文件中。 但我意識到,我想把它們放在 fragment_home.xml 文件和 HomeFragment.java 中的 java 中。 All the XML was only a button and a textView, so I copied and pasted those in the fragment_home.xml file but it wasn't so easy to copy th java from the MainActivity.java to the HomeFragment.java. 這是第一次使用底部導航模板,我不確定將代碼放在哪里,在 MainAtivity.java 中工作的代碼在 HomeFragment.java 文件中顯示紅線。 由於我沒有使用片段及其專用語法的經驗,因此我在這里尋求幫助。 我希望有人更有經驗,可以幫助我將代碼從 MainAcitivity 移動到 HomeFragment,而不會出現錯誤。 所以這里是代碼。
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="408dp"
android:layout_height="742dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
<TextView
android:id="@+id/text_view_result"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#0A0A0A"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.287" />
<Button
android:id="@+id/button_parse"
android:layout_width="194dp"
android:layout_height="71dp"
android:text="Parse"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/nav_host_fragment"
app:layout_constraintVertical_bias="0.419" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java:
package com.example.spoonacular;
import android.app.VoiceInteractor;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.VolleyLog;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.lang.reflect.Array;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
private TextView mTextViewResult;
private RequestQueue mQueue;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
mTextViewResult = findViewById(R.id.text_view_result);
Button buttonParse = findViewById(R.id.button_parse);
mQueue = Volley.newRequestQueue(this);
buttonParse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jsonParse();
}
});
}
private void jsonParse() {
String url = "https://api.spoonacular.com/recipes/random?number=1&tags=vegetarian,dessert&apiKey=86912d6bb1474577a76513e236a8a58e";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("recipes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject recipes = jsonArray.getJSONObject(i);
Boolean vegetarian = recipes.getBoolean("vegetarian");
Boolean vegan = recipes.getBoolean("vegan");
Boolean glutenFree = recipes.getBoolean("glutenFree");
Boolean dairyFree = recipes.getBoolean("dairyFree");
Boolean veryHealthy = recipes.getBoolean("veryHealthy");
Boolean cheap = recipes.getBoolean("cheap");
Boolean veryPopular = recipes.getBoolean("veryPopular");
Boolean sustainable = recipes.getBoolean("sustainable");
Boolean lowFodmap = recipes.getBoolean("lowFodmap");
int weightWatcherSmartPoints = recipes.getInt("weightWatcherSmartPoints");
int spoonacularScore = recipes.getInt("spoonacularScore");
int healthScore = recipes.getInt("healthScore");
int pricePerServing = recipes.getInt("pricePerServing");
int likes = recipes.getInt("aggregateLikes");
String gaps = recipes.getString("gaps");
String source = recipes.getString("sourceName");
mTextViewResult.setText("Vegetarian: " + vegetarian + "\n\n"+ "Vegan: " + vegan + "\n\n" + "Gluten Free: " + glutenFree + "\n\n" + "Dairy Free: " + dairyFree + "\n\n" + "Very Healthy: " + veryHealthy + "\n\n"+ "Cheap: " + cheap + "\n\n" + "Very Popular: " + veryPopular + "\n\n" + "Sustainable: " + sustainable + "\n\n" + "Low Fod Map: " + lowFodmap + "\n\n" + "Weight Watcher Points: " + String.valueOf(weightWatcherSmartPoints) + "\n\n" + "Spoonacular Score: " + String.valueOf(spoonacularScore) + "\n\n" + "Health Score: " + String.valueOf(healthScore) + "\n\n" + "Price Per Serving: " + String.valueOf(pricePerServing) + "\n\n" + "Likes: " + String.valueOf(likes) + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
}
移動到 fragment_home.xml 和 homefragment java 文件。 請注意,從這里開始沒有代碼是我寫的,這些是從底部導航模板預編碼的默認值。
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
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"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.java 文件,
package com.example.spoonacular.ui.home;
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.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import com.example.spoonacular.R;
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
final TextView textView = root.findViewById(R.id.text_home);
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
textView.setText(s);
}
});
return root;
}
}
而且因為顯示 HomeViewModel 文件也可能很有用,(盡管如果有人可以向我解釋 HomeViewModel.java 和 HomeFragment.java 之間的區別,我們將不勝感激)
主頁查看型號.java
package com.example.spoonacular.ui.home;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;
public class HomeViewModel extends ViewModel {
private MutableLiveData<String> mText;
public HomeViewModel() {
mText = new MutableLiveData<>();
mText.setValue("This is home fragment");
}
public LiveData<String> getText() {
return mText;
}
}
我知道這是一個很長的問題,可能不值得回答,但我無法找到解決方案,這是我最后的希望。 我已准備好回答任何問題(並支持並接受可行的解決方案)謝謝您的幫助!
編輯
MainActivity.java
package com.example.spoonacular;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
首頁Fragment.java
package com.example.spoonacular.ui.home;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.Volley;
import com.example.spoonacular.R;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
public class HomeFragment extends Fragment {
private TextView mTextViewResult;
private Button buttonParse;
private HomeViewModel homeViewModel;
private RequestQueue mQueue;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
mTextViewResult = root.findViewById(R.id.text_view_result);
buttonParse = root.findViewById(R.id.button_parse);
mQueue = Volley.newRequestQueue(this);
buttonParse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jsonParse();
}
});
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
mTextViewResult.setText(s);
}
});
return root;
}
private void jsonParse() {
String url = "https://api.spoonacular.com/recipes/random?number=1&tags=vegetarian,dessert&apiKey=86912d6bb1474577a76513e236a8a58e";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("recipes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject recipes = jsonArray.getJSONObject(i);
Boolean vegetarian = recipes.getBoolean("vegetarian");
Boolean vegan = recipes.getBoolean("vegan");
Boolean glutenFree = recipes.getBoolean("glutenFree");
Boolean dairyFree = recipes.getBoolean("dairyFree");
Boolean veryHealthy = recipes.getBoolean("veryHealthy");
Boolean cheap = recipes.getBoolean("cheap");
Boolean veryPopular = recipes.getBoolean("veryPopular");
Boolean sustainable = recipes.getBoolean("sustainable");
Boolean lowFodmap = recipes.getBoolean("lowFodmap");
int weightWatcherSmartPoints = recipes.getInt("weightWatcherSmartPoints");
int spoonacularScore = recipes.getInt("spoonacularScore");
int healthScore = recipes.getInt("healthScore");
int pricePerServing = recipes.getInt("pricePerServing");
int likes = recipes.getInt("aggregateLikes");
String gaps = recipes.getString("gaps");
String source = recipes.getString("sourceName");
mTextViewResult.setText("Vegetarian: " + vegetarian + "\n\n" + "Vegan: " + vegan + "\n\n" + "Gluten Free: " + glutenFree + "\n\n" + "Dairy Free: " + dairyFree + "\n\n" + "Very Healthy: " + veryHealthy + "\n\n" + "Cheap: " + cheap + "\n\n" + "Very Popular: " + veryPopular + "\n\n" + "Sustainable: " + sustainable + "\n\n" + "Low Fod Map: " + lowFodmap + "\n\n" + "Weight Watcher Points: " + String.valueOf(weightWatcherSmartPoints) + "\n\n" + "Spoonacular Score: " + String.valueOf(spoonacularScore) + "\n\n" + "Health Score: " + String.valueOf(healthScore) + "\n\n" + "Price Per Serving: " + String.valueOf(pricePerServing) + "\n\n" + "Likes: " + String.valueOf(likes) + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
}
fragment_home.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<TextView
android:id="@+id/text_home"
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"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/text_view_result"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#0A0A0A"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.287" />
<Button
android:id="@+id/button_parse"
android:layout_width="194dp"
android:layout_height="71dp"
android:text="Parse"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/nav_host_fragment"
app:layout_constraintVertical_bias="0.419" />
</androidx.constraintlayout.widget.ConstraintLayout>
Activity_Main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="408dp"
android:layout_height="742dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
HomeFragment.java 中仍有錯誤
第 44 行
mQueue = Volley.newRequestQueue(this);
我不知道,我只是將一些文本從MainActivity
復制到HomeFragment
。
主要活動
package com.example.spoonacular;
import android.os.Bundle;
import com.google.android.material.bottomnavigation.BottomNavigationView;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BottomNavigationView navView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);
}
}
首頁片段
package com.example.spoonacular.ui.home;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import com.example.spoonacular.R;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProvider;
public class HomeFragment extends Fragment {
private TextView mTextViewResult;
private Button buttonParse;
private HomeViewModel homeViewModel;
private RequestQueue mQueue;
public View onCreateView(@NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel =
new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
mTextViewResult = root.findViewById(R.id.text_view_result);
buttonParse = root.findViewById(R.id.button_parse);
mQueue = Volley.newRequestQueue(this);
buttonParse.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
jsonParse();
}
});
homeViewModel.getText().observe(getViewLifecycleOwner(), new Observer<String>() {
@Override
public void onChanged(@Nullable String s) {
mTextViewResult.setText(s);
}
});
return root;
}
private void jsonParse() {
String url = "https://api.spoonacular.com/recipes/random?number=1&tags=vegetarian,dessert&apiKey=86912d6bb1474577a76513e236a8a58e";
JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("recipes");
for (int i = 0; i < jsonArray.length(); i++) {
JSONObject recipes = jsonArray.getJSONObject(i);
Boolean vegetarian = recipes.getBoolean("vegetarian");
Boolean vegan = recipes.getBoolean("vegan");
Boolean glutenFree = recipes.getBoolean("glutenFree");
Boolean dairyFree = recipes.getBoolean("dairyFree");
Boolean veryHealthy = recipes.getBoolean("veryHealthy");
Boolean cheap = recipes.getBoolean("cheap");
Boolean veryPopular = recipes.getBoolean("veryPopular");
Boolean sustainable = recipes.getBoolean("sustainable");
Boolean lowFodmap = recipes.getBoolean("lowFodmap");
int weightWatcherSmartPoints = recipes.getInt("weightWatcherSmartPoints");
int spoonacularScore = recipes.getInt("spoonacularScore");
int healthScore = recipes.getInt("healthScore");
int pricePerServing = recipes.getInt("pricePerServing");
int likes = recipes.getInt("aggregateLikes");
String gaps = recipes.getString("gaps");
String source = recipes.getString("sourceName");
mTextViewResult.setText("Vegetarian: " + vegetarian + "\n\n" + "Vegan: " + vegan + "\n\n" + "Gluten Free: " + glutenFree + "\n\n" + "Dairy Free: " + dairyFree + "\n\n" + "Very Healthy: " + veryHealthy + "\n\n" + "Cheap: " + cheap + "\n\n" + "Very Popular: " + veryPopular + "\n\n" + "Sustainable: " + sustainable + "\n\n" + "Low Fod Map: " + lowFodmap + "\n\n" + "Weight Watcher Points: " + String.valueOf(weightWatcherSmartPoints) + "\n\n" + "Spoonacular Score: " + String.valueOf(spoonacularScore) + "\n\n" + "Health Score: " + String.valueOf(healthScore) + "\n\n" + "Price Per Serving: " + String.valueOf(pricePerServing) + "\n\n" + "Likes: " + String.valueOf(likes) + "\n\n");
}
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
error.printStackTrace();
}
});
mQueue.add(request);
}
}
活動主
<?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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize"
>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/bottom_nav_menu" />
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="408dp"
android:layout_height="742dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="@id/nav_view"
app:layout_constraintHorizontal_bias="0.333"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="@navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
片段主頁
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TextView
android:id="@+id/text_view_result"
android:layout_width="194dp"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#0A0A0A"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.287"
tools:text="dfgdfsgdfgdfgd"
/>
<Button
android:id="@+id/button_parse"
android:layout_width="194dp"
android:layout_height="71dp"
android:text="Parse"
app:layout_constraintBottom_toTopOf="@+id/nav_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/nav_host_fragment"
app:layout_constraintVertical_bias="0.419"
/>
</LinearLayout>
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.