因此,我已经为此工作了几天,并且不断收到NullPointerException。我有一个MainActivity,一个OutputActivity和一个OutputFragment。我需要将数据从MainActivity中的editText获取到OutputFragment中。我可以有意地从MainActivity将其获取到OutputActivity并将其打印到logcat,但不能从OutputActivity将其获取到OutputFragment。
我试过了:
-使用捆绑包将其从MainActivity发送到OutputFragment
-使用Intent将其发送到OutputActivity,然后使用Intent将其发送到OutputFragment
-使用Intent将其发送到OutputActivity,然后使用分发包将其发送到OutputFragment
我一直在搜索Stack Overflow,Google,观看视频等。
每次尝试在OutputFragment中检索值时,都会收到NullPointerException。
我是一名大学生,我根本不明白自己在做什么错。一切看起来都不错,但我不断收到例外。在尝试其他操作时,我有很多注释行,因此我为可读性表示歉意,我认为更多的信息总比少多了,因此我将其保留。 (目前,基本上已注释掉整个OutputActivity)
有人会帮我解决这个愚蠢的错误吗?
谢谢
主要活动
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);
}
}
OutputActivity
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);
}
}
输出片段
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;
}
}
最佳答案
尝试使用此代码将数据从活动传递到片段
在活动中添加片段
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();
片段中
@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/