gpt4 book ai didi

java - 在 onCreateView 方法之外的 Fragment Activity 中使用 Edittext

转载 作者:行者123 更新时间:2023-12-01 11:27:00 25 4
gpt4 key购买 nike

正如标题所说,我需要获取 fragment Activity 中包含的 editText 的值,它是 ViewPager 的一部分;到目前为止,我已经尝试了所有方法,并且我已经了解,只需在充气后传递 Activity 的当前 View 即可访问 xml 项目,这就是我所做的。但是我需要包含包含的值在用户填写后的编辑文本中,特别是在按下“保存”按钮后;在程序执行期间,当我尝试获取包含的文本时,我收到此错误:“java.lang.NullPointerException:尝试调用虚拟方法'android.text.Editable android.widget.EditText.getText ()' 在空对象引用上”抱歉这个愚蠢的问题,我是 Android 编程新手,我很感谢我能得到的所有帮助这是 fragment Activity 的代码:

public class Monday extends android.support.v4.app.Fragment{
EditText[] subjects = new EditText[6];
EditText[] classes = new EditText[6];

private OnFragmentInteractionListener mListener;

public Monday() {
// Required empty public constructor
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
ScrollView scrollView = (ScrollView)inflater.inflate(R.layout.fragment_monday, container, false);
//some code

subjects[0] = (EditText) scrollView.findViewById(R.id.editTextMateria1);
subjects[1] = (EditText) scrollView.findViewById(R.id.editTextMateria2);
subjects[2] = (EditText) scrollView.findViewById(R.id.editTextMateria3);
subjects[3] = (EditText) scrollView.findViewById(R.id.editTextMateria4);
subjects[4] = (EditText) scrollView.findViewById(R.id.editTextMateria5);
subjects[5] = (EditText) scrollView.findViewById(R.id.editTextMateria6);


classes[0] = (EditText) scrollView.findViewById(R.id.editTextClasse1);
classes[1] = (EditText) scrollView.findViewById(R.id.editTextClasse2);
classes[2] = (EditText) scrollView.findViewById(R.id.editTextClasse3);
classes[3] = (EditText) scrollView.findViewById(R.id.editTextClasse4);
classes[4] = (EditText) scrollView.findViewById(R.id.editTextClasse5);
classes[5] = (EditText) scrollView.findViewById(R.id.editTextClasse6);
return scrollView;
//return inflater.inflate(R.layout.fragment_monday, container, false);
}

// TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}

@Override
public void onDetach() {
super.onDetach();
mListener = null;
}

public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
public void onFragmentInteraction(Uri uri);
}

public String[] calculateDay(){


String s = "";
for(int i=1;i<subjects.length;i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
}
String[] monday = new String[6];

monday = s.split(";");
return monday;

}
}

这是生成错误的部分:

   s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";

这是生成 ViewPager 的“mainactivity”的代码(在其上处理按下保存的按钮):

public class CustomTimetables extends FragmentActivity implements NoticeDialogFragment.NoticeDialogListener{

public static String classname = "";
ViewPager viewPager = null;

private MyAdapter2 mAdapter;

ViewPager Tab;
MyAdapter2 TabAdapter;
ActionBar actionBar;
PagerSlidingTabStrip tabs;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_ACTION_BAR);
setContentView(R.layout.activity_custom_timetables);

tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
Tab = (ViewPager) findViewById(R.id.pager);
TabAdapter = new MyAdapter2(getSupportFragmentManager());

Tab.setAdapter(TabAdapter);
tabs.setViewPager(Tab);
Toast.makeText( getApplicationContext(),
"Inserisci l'orario nelle caselle, lasciale vuote se le lezioni terminano prima ",
Toast.LENGTH_LONG
).show();



}



@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_custom_timetables, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {

int id = item.getItemId();

//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}

return super.onOptionsItemSelected(item);
}


public void onSaveTimetable(View v){
DialogFragment dialog = new NoticeDialogFragment();
dialog.show(this.getSupportFragmentManager(), "insert_class");
}

public void onDialogPositiveClick(DialogFragment dialog) {
String timetable = "Orario;Lunedi;Martedi;Mercoledi;Giovedi;Venerdi;Sabato;\n08:00;";


String[] monday = new String[6];
Monday m = new Monday();
monday = m.calculateDay();

for(int i = 0; i<6; i++){
timetable += monday[i]+";";



switch(i){
case 0: timetable +="\n";break;
case 1 : timetable +="\n09:00;";break;
case 2 : timetable +="\n10:00;";break;
case 3 : timetable +="\n11:05;";break;
case 4 : timetable +="\n12:00;";break;
case 5 : timetable +="\n12:50;";break;
}

}

}

@Override
public void onDialogNegativeClick(DialogFragment dialog) {

}
}

class MyAdapter2 extends FragmentStatePagerAdapter
{

public MyAdapter2(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int i) {
Fragment fragment = null;
if(i==0){
fragment = new Monday();
}
if(i==1){
fragment = new Tuesday();
}
if(i==2){
fragment = new Wednesday();
}
if(i==3){
fragment = new Thursday();
}
if(i==4){
fragment = new Friday();
}
if(i==5){
fragment = new Saturday();
}
return fragment;
}

@Override
public int getCount() {
return 6;
}

@Override
public CharSequence getPageTitle(int position) {

if(position == 0){
return "Lunedi";
}
if(position == 1){
return "Martedi";
}
if(position == 2){
return "Mercoledi";
}
if(position == 3){
return "Giovedi";
}
if(position == 4){
return "Venerdi";
}
if(position == 5){
return "Sabato";
}
return null;
}


}

这是日志猫:

    06-09 19:29:16.172  24414-24443/com.progettostage.nick__000.timetablesapp D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
06-09 19:29:16.187 24414-24414/com.progettostage.nick__000.timetablesapp D/Atlas﹕ Validating map...
06-09 19:29:16.250 24414-24443/com.progettostage.nick__000.timetablesapp I/Adreno-EGL﹕ <qeglDrvAPI_eglInitialize:379>: QUALCOMM Build: 01/15/15, ab0075f, Id3510ff6dc
06-09 19:29:16.252 24414-24443/com.progettostage.nick__000.timetablesapp I/OpenGLRenderer﹕ Initialized EGL, version 1.4
06-09 19:29:16.290 24414-24443/com.progettostage.nick__000.timetablesapp D/OpenGLRenderer﹕ Enabling debug mode 0
06-09 19:32:20.725 24414-24443/com.progettostage.nick__000.timetablesapp V/RenderScript﹕ Application requested CPU execution
06-09 19:32:20.738 24414-24443/com.progettostage.nick__000.timetablesapp V/RenderScript﹕ 0xb83ba0b8 Launching thread(s), CPUs 4
06-09 19:32:38.415 24414-24414/com.progettostage.nick__000.timetablesapp D/AndroidRuntime﹕ Shutting down VM
06-09 19:32:38.422 24414-24414/com.progettostage.nick__000.timetablesapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.progettostage.nick__000.timetablesapp, PID: 24414
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.progettostage.nick__000.timetablesapp.Monday.calculateDay(Monday.java:111)
at com.progettostage.nick__000.timetablesapp.CustomTimetables.onDialogPositiveClick(CustomTimetables.java:111)
at com.progettostage.nick__000.timetablesapp.NoticeDialogFragment$2.onClick(NoticeDialogFragment.java:33)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:162)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)

这只是处理对话框的类,我认为它与它没有任何关系,但安全总比抱歉好..

public class NoticeDialogFragment extends DialogFragment {


@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
final View view = inflater.inflate(R.layout.dialog_classinput, null);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(view)
// Add action buttons
.setTitle("Inserisci nome classe")
.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
EditText et = (EditText)view.findViewById(R.id.classname22);
CustomTimetables.classname = et.getText().toString();
// Send the positive button event back to the host activity
mListener.onDialogPositiveClick(NoticeDialogFragment.this);
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.dismiss();
}
});
return builder.create();
}


public interface NoticeDialogListener {
public void onDialogPositiveClick(DialogFragment dialog);
public void onDialogNegativeClick(DialogFragment dialog);
}


NoticeDialogListener mListener;


@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NoticeDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}

}

这也是Monday.xml(最后因为太长而不得不剪掉一些部分,但很简单,就这样一直到editTextMateria6和editTextClasse6):

<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".Monday"

>


<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="8:00-9:00"
android:id="@+id/PrimaOra"
android:textSize="25dp"

android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="@+id/materia1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="115dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editTextMateria1"
android:layout_marginTop="160dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="@+id/classe1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="225dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextClasse1"
android:layout_marginTop="270dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
-----------------------------------------------------

<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="9:00-10:00"
android:id="@+id/SecondaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="325dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="@+id/materia2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="390dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextMateria2"
android:layout_marginTop="435dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="@+id/classe2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="500dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextClasse2"
android:layout_marginTop="545dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
-----------------------------------------

<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="10:00-10:50"
android:id="@+id/TerzaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="600dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="@+id/materia3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="665dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextMateria3"
android:layout_marginTop="710dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="@+id/classe3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="775dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextClasse3"
android:layout_marginTop="825dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
-----------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="11:05-12:00"
android:id="@+id/QuartaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="875dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="@+id/materia4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="940dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextMateria4"
android:layout_marginTop="985dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="@+id/classe4"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1050dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextClasse4"
android:layout_marginTop="1095dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
------------------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="12:00-12:50"
android:id="@+id/QuintaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1150dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="@+id/materia5"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1215dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextMateria5"
android:layout_marginTop="1260dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Classe"
android:id="@+id/classe5"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1325dp" />

<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:text=""
android:ems="10"
android:id="@+id/editTextClasse5"
android:layout_marginTop="1380dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
----------------------------------------------
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="58dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="12:50-13:40"
android:id="@+id/SestaOra"
android:textSize="25dp"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1435dp" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Materia"
android:id="@+id/materia6"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="1500dp" />



<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:width="400dp"
android:height="70dp"
android:onClick="onSaveTimetable"
android:text="Salva"
android:layout_marginTop="1700dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>


</RelativeLayout>
</ScrollView>

更新好吧,我添加了几行,所以也许更清楚问题是什么..

  String sub1 = subjects[0].getText().toString();
String classr1 = classes[0].getText().toString();
for(int i=0;i<subjects.length;i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
}

如果我这样设置,我会在 sub1 行得到同样的错误,因为在这样的方法中,我无法传递 View 或膨胀它,就像我在 oncreateview 上所做的那样,它无论我做什么操作,都可以正常工作。问题是,在用户在其中放入一些内容后,我需要这些编辑文本。我希望我说得更清楚了,我感谢我仍然得到的所有帮助,我'我想指出的是,我已经尝试了这里的每一个解决方案,尽管我没有全部回复

最佳答案

数组索引从0开始。访问数组时请确保使用正确的索引值。

关于java - 在 onCreateView 方法之外的 Fragment Activity 中使用 Edittext,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30739355/

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