Java 包裹未填充
我试图将一些数据从一个活动发送到它的片段。我已经绑定了数据,但是当我试图在片段中检索它时,它是空的 Graph.java(片段) 我已经用****标记了捆绑包和检索代码。从我的日志跟踪中,我知道bundle代码正在运行,setdata()方法正在运行,但是'if'语句从来没有运行过。我添加了代码Java 包裹未填充,java,android,bundle,android-bundle,Java,Android,Bundle,Android Bundle,我试图将一些数据从一个活动发送到它的片段。我已经绑定了数据,但是当我试图在片段中检索它时,它是空的 Graph.java(片段) 我已经用****标记了捆绑包和检索代码。从我的日志跟踪中,我知道bundle代码正在运行,setdata()方法正在运行,但是'if'语句从来没有运行过。我添加了代码if(getArguments()!=null){以避免java.lang.nullPointerException 我认为数据被正确绑定,但由于某种原因,它在片段中显示为null。我可能对其中一些数据编
if(getArguments()!=null){
以避免java.lang.nullPointerException
我认为数据被正确绑定,但由于某种原因,它在片段中显示为null。我可能对其中一些数据编码错误-这是我第一次尝试使用绑定
编辑:我已经设置了livedata和View模型类,但是我不确定如何实现它们来更新数据
InputViewModel.java
private InputRepository mRepository;
private LiveData<List<Input1>> mAllInput1s;
public InputViewModel (Application application) {
super(application);
mRepository = new InputRepository(application);
mAllInput1s = mRepository.getAllInput1s();
}
LiveData<List<Input1>> getAllInput1s() {return mAllInput1s;}
void insert(Input1 input1) {mRepository.insert(input1);}
}
private输入存储库;
私有LiveData mAllInput1s;
公共输入视图模型(应用程序){
超级(应用);
mRepository=新输入存储(应用);
mAllInput1s=mRepository.getAllInput1s();
}
LiveData getAllInput1s(){return mAllInput1s;}
void insert(Input1 Input1){mRepository.insert(Input1);}
}
您没有对在那里创建的图形执行任何操作。该实例确实设置了其参数,但除非您处理该实例,否则它实际上不会被使用。如果您试图更新“live”片段
中的某些内容,则无法使用参数进行更新,这是一种一次性交易,您只能在处理片段
之前进行设置。谢谢@MikeM的帮助。你建议怎么做呢?嗯,最新流行的是ViewModel
/LiveData
设置,但便宜而简单的方法是在片段中加入public
方法来更新和刷新图表。不过,您必须小心,因为Fragment
s的行为可能不像您预期的那样;e、 例如,在设备旋转等情况下进行娱乐。非常感谢您的帮助@MikeM。-你是救命恩人。我已经更新了我的问题,以包括我已经为保存输入而设置的ViewModel类(抱歉,我不知道它是相关的,否则我本来会包括它)。您能告诉我/指导我如何创建设置以使用输入更新图形的一些资源吗。否则,您能否向我解释更多/指导我如何通过public方法实现这一点(我不担心设备旋转)。再次感谢你们!!!如果您使用的是ViewModel
/LiveData
,那么就不需要使用public
方法。这相对简单,只要你用心去想。有一个非常简单的例子。您的设置有点像,除了主片段
之外,还有您的活动
。
//getting inputs from room and putting them into the view in table
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
mInputViewModel = ViewModelProviders.of(this).get(InputViewModel.class);
//only add if there is a new input
Log.e("result1","onActivityResult tabbed reacher");
if (requestCode == INPUT_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK) {
//retrieve input from room database
Input1 input1 = new Input1(data.getStringExtra(input.EXTRA_REPLY));
//insert the inputs into the view model of the recyclerView
mInputViewModel.insert(input1);
//convert the String input1 to a double
Double d = Double.parseDouble(input1.getValue());
*****
//send d value to graph.java
Bundle bundle = new Bundle();
Log.e("Testing", "Bundle made");
bundle.putString("INPUT", String.valueOf(d));
Graph myObj = new Graph();
myObj.setArguments(bundle);
*****
}
//display pop-up if no input is added don't save the input
} else {
Toast.makeText(
getApplicationContext(),
R.string.empty_not_saved,
Toast.LENGTH_LONG).show();
}
}
private InputRepository mRepository;
private LiveData<List<Input1>> mAllInput1s;
public InputViewModel (Application application) {
super(application);
mRepository = new InputRepository(application);
mAllInput1s = mRepository.getAllInput1s();
}
LiveData<List<Input1>> getAllInput1s() {return mAllInput1s;}
void insert(Input1 input1) {mRepository.insert(input1);}
}