gpt4 book ai didi

java - 无法从Activity获取值到Fragment,NullPointerException

转载 作者:行者123 更新时间:2023-12-01 17:00:30 24 4
gpt4 key购买 nike

所以我已经为此工作了几天,但我不断收到 NullPointerException。我有一个 MainActivity、一个 OutputActivity 和一个 OutputFragment。我需要将 MainActivity 中的 editText 中的数据获取到 OutputFragment 中。我可以有意地将其从 MainActivity 获取到 OutputActivity 并将值打印到 logcat,但我无法将其从 OutputActivity 获取到 OutputFragment。

我尝试过:
-使用 bundle 将其从 MainActivity 发送到 OutputFragment
-使用Intent发送到OutputActivity,然后使用Intent发送到OutputFragment
-使用Intent将其发送到OutputActivity,然后使用bundle将其发送到OutputFragment

我一直在 Stack Overflow、Google、观看视频等方面进行搜索。

每次我尝试检索 OutputFragment 中的值时,都会收到 NullPointerException。我是一名大学生,我根本不明白我做错了什么。一切看起来都不错,但我不断遇到异常(exception)。当我尝试不同的东西时,我注释掉了一堆行,所以我为可读性道歉,我认为更多的信息总比更少的信息好,所以我留下了它们。 (整个OutputActivity目前基本都被注释掉了)

有人可以帮助我并告诉我这个愚蠢的错误吗?

谢谢

主要 Activity

package com.murach.josephsmithsemesterproject;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {


//Initialize variables
EditText benchPressEditText;
EditText squatEditText;
EditText overheadPressEditText;
EditText deadliftEditText;
Button calculateBtn;



@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

// Get references to the widgets
benchPressEditText = (EditText) findViewById(R.id.benchPressEditText);
squatEditText = (EditText) findViewById(R.id.squatEditText);
overheadPressEditText = (EditText) findViewById(R.id.overheadPressEditText);
deadliftEditText = (EditText) findViewById(R.id.deadliftEditText);
calculateBtn = (Button) findViewById(R.id.calculateBtn);



calculateBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
sendData();
openOutputActivity();
}
});


}



private void openOutputActivity() {
Intent intent = new Intent(getApplicationContext(), OutputActivity.class);
startActivity(intent);
}

private void sendData() {
//Get values from edit text
String oneRepBench = benchPressEditText.getText().toString();

OutputFragment outputFragment = new OutputFragment();
Bundle bundle = new Bundle();
bundle.putString("oneRepBench",oneRepBench);
Log.v("Data", oneRepBench);

//set fragment class arguments

//outputFragment.setArguments(bundle);

//Intent intent = new Intent(getApplicationContext(), OutputActivity.class);
//intent.putExtra("oneRepBench", oneRepBench);
//startActivity(intent);





}

}

输出 Activity

package com.murach.josephsmithsemesterproject;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;

public class OutputActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Set the view for the activities XML
setContentView(R.layout.activity_output);

//Bundle bundle = getArguments();
//Log.v("getString", bundle.getString("oneRepBench", "No value"));
//String oneRepBench = bundle.getString("oneRepBench");

//Intent intent = getIntent();
//String oneRepBench = intent.getStringExtra("oneRepBench");
//Log.v("Data now", oneRepBench);

//Intent outputIntent = new Intent(getApplicationContext(), OutputFragment.class);
//intent.putExtra("oneRepbench", oneRepBench);
//startActivity(intent);

//Bundle bundle = new Bundle();
//bundle.putString("oneRepBench", oneRepBench);

//OutputFragment outputFragment = new OutputFragment();
//outputFragment.setArguments(bundle);


}

}

输出 fragment

 package com.murach.josephsmithsemesterproject;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

import org.w3c.dom.Text;

import java.util.Objects;

public class OutputFragment extends Fragment {

/* @Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

//Bundle bundle = getArguments();
//Log.v("getString", bundle.getString("oneRepBench", "No value"));
//String oneRepBench = bundle.getString("oneRepBench");

//TextView oneRepBenchText = (TextView) view.findViewById(R.id.oneRepBench);
//oneRepBenchText.setText(oneRepBench);
//Intent intent = getActivity().getIntent();
//String oneRepBench = intent.getStringExtra("oneRepBench");
}*/

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_output, container, false);

//assert getArguments() != null;
//String oneRepBench = getArguments().getString("oneRepBench");
//Intent intent = getActivity().getIntent();
//String oneRepBench = intent.getStringExtra("oneRepBench");
//Log.v("Data now", oneRepBench);

String text;
text = this.getArguments().getString("oneRepBench");
//Returns NullPointerException

return view;
}

}

最佳答案

尝试使用此代码将数据从 Activity 传递到 fragment

在Activity中添加fragment

   Bundle bundle = new Bundle();
bundle.putString("edttext", "value");
// set Fragmentclass Arguments
Fragmentclass fragobj = new Fragmentclass();
fragobj.setArguments(bundle);
FragmentManager manager = getFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.add(R.id.container,fragobj,YOUR_FRAGMENT_STRING_TAG);
transaction.addToBackStack(null);
transaction.commit();

在 fragment 中

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Bundle bundle= getArguments();
if(bundle!=null){
String strtext = bundle.getString("value");
}

return inflater.inflate(R.layout.fragment, container, false);
}

关于java - 无法从Activity获取值到Fragment,NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61513895/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com