我試圖將數據從我的「Training2.java」類傳遞到我的「Training1.java類」,並且在執行此操作時應用程序崩潰。 「訓練2」是一項活動,「訓練1」是一個片段,我認爲這個問題源於此。我得到錯誤應用程序崩潰同時將數據從活動傳遞到片段
「android.content.ActivityNotFoundException:無法找到顯式活動類{com.hardingsoftware.hrcfitness/com.hardingsoftware.hrcfitness.Training1};你是否在你的AndroidManifest.xml中聲明瞭這個活動?」 ,
所以我幾乎可以肯定這是問題所在。任何想法如何將數據傳遞給Training 1,同時使用intent維護我的設置?
培訓1:
package com.hardingsoftware.hrcfitness;
/**
* Created by John on 2/7/16.
*/
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Training1 extends Fragment implements View.OnClickListener, AdapterView.OnItemClickListener{
private ListView lwItems;
private Button btnChangeItems;
private String DEFAULT_ITEMS_VALUES_STRING = "Item 1; Item 2";
private String customItemString;
private static SharedPreferences preferenceItems;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View rootView = inflater.inflate(R.layout.content_main, container, false);
Button btnChangeItems = (Button) rootView.findViewById(R.id.btnChangeItems);
btnChangeItems.setOnClickListener(this);
lwItems = (ListView) rootView.findViewById(R.id.lwItems);
preferenceItems = getActivity().getSharedPreferences("items", 0);
return rootView;
}
@Override
public void onResume() {
super.onResume();
Bundle setItemsIntent = getActivity().getIntent().getExtras();
if (setItemsIntent != null){
customItemString = getActivity().getIntent().getExtras().getString("customValues");
}
String[] items;
String preferenceItemsString = preferenceItems.getString("customValues", null);
if(preferenceItemsString != null){
items = getSplitAndTrim(preferenceItemsString);
}else{
items = getSplitAndTrim(DEFAULT_ITEMS_VALUES_STRING);
}
ArrayAdapter<String> arrayAdapter =
new ArrayAdapter<>(getActivity(), android.R.layout.simple_expandable_list_item_1, items);
lwItems.setAdapter(arrayAdapter);
lwItems.setOnItemClickListener(this);
}
@Override
public void onPause() {
super.onPause();
if(customItemString != null){
putStringInPreferences("customValues", customItemString);
}
}
@Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(), Training2.class);
startActivity(intent);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String textOfItem = String.valueOf(((TextView) view).getText());
Toast.makeText(getActivity(), textOfItem, Toast.LENGTH_SHORT).show();
}
public static void putStringInPreferences(String key, String customItemString) {
SharedPreferences.Editor editor = preferenceItems.edit();
editor.putString(key, customItemString);
editor.commit();
}
@NonNull
private String[] getSplitAndTrim(String preferenceItemsString) {
return preferenceItemsString.trim().split("\\s*;\\s*");
}
}
培訓2:
package com.hardingsoftware.hrcfitness;
/**
* Created by John on 2/7/16.
*/
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Training2 extends AppCompatActivity implements View.OnClickListener{
EditText etInputValues;
Button btnSetVales;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_set_items);
etInputValues = (EditText) findViewById(R.id.etInputValues);
btnSetVales = (Button) findViewById(R.id.btnSetValues);
btnSetVales.setOnClickListener(this);
}
@Override
public void onClick(View v) {
String etInputValuesText = String.valueOf(etInputValues.getText());
if (!etInputValuesText.equals("")){
Training1.putStringInPreferences("customValues", etInputValuesText);
Intent intent = new Intent(this, Training1.class);
intent.putExtra("customValues", etInputValuesText);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra("EXIT", true);
startActivity(intent);
}else{
Toast.makeText(Training2.this, "No Values Added!", Toast.LENGTH_SHORT).show();
}
}
}
將我的額外「customValues」與這個傳遞? –
我完成了代碼示例,從您的主機活動中傳遞數據或調用方法到您的片段中...... – JBA
@JBA您應該使用'setArguments'將數據傳遞到'Fragment',以便狀態仍然存在如果片段由框架恢復。請參閱http://stackoverflow.com/a/11397212/1105073和http://developer.android.com/reference/android/app/Fragment.html#setArguments(android.os.Bundle) – Eggman87