将数据从活动传递到片段

时间:2016-02-07 21:55:35

标签: java android android-fragments android-activity sharedpreferences

我正在尝试将数据从我的“Training2.java”类传递到我的“Training1.java类”,当我这样做时应用程序崩溃了。 “Training2”是一项活动,“Training1”是一个片段,我相信这个问题源于此。我收到了错误

“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();
        }



    }
}

2 个答案:

答案 0 :(得分:1)

Training1是Fragment,而不是ActivityFragment生活在活动中。您不使用意图显示片段,而是需要使用FragmentManager将片段添加到您的Activity。请参阅Android文档中的this指南。

如果您只是希望Training1成为一个不同的屏幕(基本上是一个Activity),您可以通过扩展AppCompatActivity而不是Fragment使Training1成为一个活动,并将其添加到您的Android清单中。如果您这样做,则可以通过显式Intent导航到该活动。

答案 1 :(得分:0)

您需要使用FragmentTransaction将Training1作为片段加载,而不是像活动那样加载它...

在您的主要活动布局中,您可能有一些地方可以像这样加载Framgments:

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content, new Training1(), "TRAINING_1_TAG"); 
ft.commit();

然后将您的Training1片段附加到此容器中使用:

Training1 training1Frament = (Training1) getSupportFragmentManager().findFragmentByTag("TRAINING_1_TAG");
training1Frament.myPublicVar = "my variable";
training1Frament.myPublicMethod(); 

现在从其标记中获取片段,从那里设置变量和/或触发一些方法:

{{1}}