从片段中的数组设置TextView

时间:2018-06-29 06:30:40

标签: java android arrays android-fragments nullpointerexception

因此,我们尝试编写从片段中获取的字符串以保存在数组中,并在另一个片段的textview中设置该数组中的文本,但如果尝试这样做,应用程序就会自杀:

public View onCreateView(final LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_timetable, container, false);
    myTL = view.findViewById(R.id.tableLayoutTage);
    MO1 = view.findViewById(R.id.MO1);
    VGMO1 = view.findViewById(R.id.MO1);
    DI1 = view.findViewById(R.id.DI1);

    DI1 = view.findViewById(R.id.DI1);
    FachMO1 = (TextView) VGMO1.findViewById(R.id.textViewFach);
    LehrerMO1 = (TextView) VGMO1.findViewById(R.id.textViewLehrer);
    RaumMO1 = (TextView) VGMO1.findViewById(R.id.textViewRaum);
    FachDI1 = (TextView) DI1.findViewById(R.id.textViewFach);
    stundeZuweisen = new StundeZuweisen();

    DI1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            String a = Montag1[0];
            String b = Montag1[1];
            String c = Montag1[2];
            schreibeInTV(a, b, c);
            return true;
        }
    });


    VGMO1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            String ViewID = getResources().getResourceName(view.getId());
            IDfinal = cutID(ViewID);


            Log.d(TAG, "" + IDfinal);

            Intent intent = new Intent(getActivity(), StundeZuweisen.class);
            intent.putExtra("ViewID", IDfinal);
            startActivity(intent);
            return true;
        }
    });


    //arrayToString();


    return view;
}

private void arrayToString() {
    a = Montag1[1];
    b = Montag1[2];
    c = Montag1[0];
    Log.d(TAG, "" + a + b);
}


public String cutID(String longID) {
    if (longID.length() == 3) {
        return longID;
    } else if (longID.length() > 3) {
        return longID.substring(longID.length() - 3);
    }
    return longID;
}


public void setzeTextViewFachMO1() {
    FachMO1.setText(stundeZuweisen.getFachSpinnerText());
}

public void schreibeInArray(String Stunde) {
    FachMO1.setText(Stunde);
}

public void schreibeInTV(String a, String b, String c) {
    FachMO1.setText(a);
    LehrerMO1.setText(b);
    RaumMO1.setText(c);
    String FachMO1Test =  FachMO1.getText().toString();
    Log.d(TAG, "schreibeInTV: korrekt" + FachMO1Test);
}

}

我们收到此错误

  

06-29 08:27:35.233 6311-6311 / com.example.jannikokan.timetable   E / AndroidRuntime:致命异常:main       流程:com.example.jannikokan.timetable,PID:6311       java.lang.NullPointerException:尝试在虚拟设备上调用虚拟方法'void android.widget.TextView.setText(java.lang.CharSequence)'   空对象引用           在com.example.jannikokan.timetable.Table.TimeTableFragment.schreibeInTV(TimeTableFragment.java:210)           在com.example.jannikokan.timetable.Table.TimeTableFragment $ 1.onLongClick(TimeTableFragment.java:154)           在android.view.View.performLongClickInternal(View.java:6374)           在android.view.View.performLongClick(View.java:6332)           在android.view.View.performLongClick(View.java:6350)           在android.view.View $ CheckForLongPress.run(View.java:24895)           在android.os.Handler.handleCallback(Handler.java:808)           在android.os.Handler.dispatchMessage(Handler.java:101)           在android.os.Looper.loop(Looper.java:166)           在android.app.ActivityThread.main(ActivityThread.java:7425)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:245)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)

有人可以帮助我们解决我们的问题吗? 谢谢

1 个答案:

答案 0 :(得分:0)

尽量不要直接设置值,请使用startActivityForResult()

时间表片段

VGMO1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            String ViewID = getResources().getResourceName(view.getId());
            IDfinal = cutID(ViewID);

            Log.d(TAG, "" + IDfinal);

            Intent intent = new Intent(getActivity(), StundeZuweisen.class);
            intent.putExtra("ViewID", IDfinal);

            startActivityForResult(intent, 0);
            return true;
        }
    });


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if(resultCode == Activity.RESULT_OK && requestCode == 0){
        String arr[] = data.getStringArrayExtra("result");
        schreibeInTV(arr[0],arr[1],arr[2]);
    }
}

StundeZuweisen

    buttonStundeZuweisen.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String FKZ = fachSpinnerText;
            String LKZ = lehrerSpinnerText;
            String RKZ = raumSpinnerText;
            Log.d(TAG, "" + ViewID + FKZ + LKZ + RKZ);
            //timeTableFragment.schreibeInArray(ViewID, FKZ,LKZ,RKZ);

            Intent returnIntent = new Intent();
            returnIntent.putExtra("result",(new String[]{FKZ, LKZ, RKZ}));
            setResult(Activity.RESULT_OK,returnIntent);
            finish();


            Toast.makeText(StundeZuweisen.this, "Stunde wurde zugewiesen.", Toast.LENGTH_LONG).show();

            /*boolean istGespeichert =  myDb.speicherStunde(ViewID, FKZ, LKZ , RKZ);
            if (istGespeichert == true){
                Toast.makeText(StundeZuweisen.this, "Stunde wurde zugewiesen.", Toast.LENGTH_LONG).show();
            }*/
        }
    });