简体   繁体   English

Android Studio / Java空对象引用-似乎无法从活动中编辑片段变量

[英]Android Studio/Java null object reference - can't seem to edit fragment variable from activity

Ok, I'm new to Android Studio so bear with me here if this is a silly question but I've been researching for hours trying to figure out why I can't do this and I can't seem to figure it out. 好的,我是Android Studio的新手,如果这是一个愚蠢的问题,请在这里接受,但是我已经研究了几个小时,试图弄清为什么我不能做到这一点,而且我似乎也无法弄清楚。

I have a MainActivity and 2 fragments. 我有一个MainActivity和2个片段。 I'm trying to edit public variables (Lists) within the fragments from the main activity. 我正在尝试在主要活动的片段内编辑公共变量(列表)。 Here's how I've gone about doing it so far. 到目前为止,这是我所做的事情。

Inside MainActivity I have the following: 在MainActivity内部,我有以下内容:

public class MainActivity extends AppCompatActivity implements Form.sendFormData, Chart.chartData {

    private Form form = new Form();
    private Chart chart = new Chart();

    public MainActivity() {
        this.form = (Form) getSupportFragmentManager().findFragmentById(R.id.fragmentForm);
        this.chart = (Chart) getSupportFragmentManager().findFragmentById(R.id.fragmentChart);
    }

...

Form and Chart are my 2 fragments, neither of which will let me edit their publicly declared variables. Form和Chart是我的2个片段,这两个片段都不会让我编辑它们的公开声明的变量。

Here's what it looks like at the start of my Chart class: 这是我的Chart类开始时的样子:

public class Chart extends Fragment {

    private static final String TAG = "Chart";

    PieChart chart;

    public List<Entry> entryList = new ArrayList<>();
    public List<PieEntry> entries = new ArrayList<>();
...

What I'm trying to do is access entryList via MainActivity by doing the following: chart.entryList.add(...); 我想做的是通过执行以下操作通过MainActivity访问entryListchart.entryList.add(...); . However when I execute the method and it gets to that line of code (or any line of code that attempts to read/change a variable inside a fragment from MainActivity I get the null object reference. 但是,当我执行该方法并到达代码的那一行(或试图从MainActivity读取片段中的变量的任何代码行)时,我会得到空对象引用。

I have interfaces set up in each of the fragments for sending information from the Fragment to the MainActivity. 我在每个片段中都设置了接口,用于将信息从片段发送到MainActivity。 Am I supposed to use the same interface to send information from MainActivity to the Fragments and if so how? 我是否应该使用相同的接口将信息从MainActivity发送到Fragments,如果可以,怎么办?

Here's the logcat: 这是logcat:

05-03 00:44:10.873 29594-29594/com.mptests.rafflemore E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.mptests.rafflemore, PID: 29594
java.lang.NullPointerException: Attempt to read from field 'java.util.List com.mptests.rafflemore.Chart.entryList' on a null object reference
at com.mptests.rafflemore.MainActivity.onSubmitEntry(MainActivity.java:61)
at com.mptests.rafflemore.Form.submitEntry(Form.java:139)
at com.mptests.rafflemore.Form$1.onClick(Form.java:74)
at android.view.View.performClick(View.java:5637)
at android.view.View$PerformClick.run(View.java:22429)
at android.os.Handler.handleCallback(Handler.java:751)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

EDIT: 编辑:

I should mention that the code executes if I declare form and chart as follows and don't try to reference them to their fragment id's: 我应该提到的是,如果我按以下方式声明formchart ,并且不尝试将它们引用到它们的片段ID上,则代码将执行:

private Form form = new Form();
private Chart chart = new Chart();

However when I do it that way the fragments don't actually update with the data I want to change within them. 但是,当我这样做时,片段实际上并不会使用我要在其中更改的数据进行更新。 So I assume that the problem lies within the reference to the fragments at getSupportFragmentManager().findFragmentById(R.id.---); 所以我假设问题出在getSupportFragmentManager().findFragmentById(R.id.---);的片段引用之内getSupportFragmentManager().findFragmentById(R.id.---); . I just can't seem to understand why the reference is null. 我只是似乎不明白为什么引用为空。

尝试在片段中使用static关键字,如下所示:

public static List<Entry> entryList = new ArrayList<>();

Your doubt is correct. 您的怀疑是正确的。 The problem is here: 问题在这里:

public MainActivity() {
    this.form = (Form) getSupportFragmentManager().findFragmentById(R.id.fragmentForm);
    this.chart = (Chart) getSupportFragmentManager().findFragmentById(R.id.fragmentChart);
}

You can not assign form and chart here. 您不能在此处分配表格和图表。 Here, there is no view been infalted. 在这里,没有观点被破坏。 So the findFragmentById(R.id.fragmentForm) and findFragmentById(R.id.fragmentChart) will be null. 因此,findFragmentById(R.id.fragmentForm)和findFragmentById(R.id.fragmentChart)将为null。

You can assgin them in onCreate() 您可以在onCreate()中设置它们

@Override
onCreate() {
    setContentVew(R.layout.my_view);

    this.form = (Form) getSupportFragmentManager().findFragmentById(R.id.fragmentForm);
    this.chart = (Chart) getSupportFragmentManager().findFragmentById(R.id.fragmentChart);
}

I don't like that once you meet a problem, only use static. 遇到问题后,我不喜欢使用静态方法。 You can try to find why it happens. 您可以尝试找出原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM