正如标题所说,我需要获取片段活动中包含的editText的值,作为ViewPager的一部分;到目前为止,我已经尝试了所有内容,并且我已经明白,只需在充气后传递活动的当前视图即可访问xml项目,这就是我所做的事情..但是我需要在用户填写之后将edittexts中包含的值包含在内,特别是在按钮“保存”之后。被迫;在程序执行期间,当我尝试获取包含的文本时,我收到此错误: " java.lang.NullPointerException:尝试调用虚方法' android.text.Editable android .widget.EditText.getText()'在空对象引用" 抱歉这个愚蠢的问题,我是android编程的新手,我很感激所有的帮助 这是片段活动的代码:
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()+";";
这里是&#39; mainactivity&#39;的代码。生成ViewPager(按下按钮保存的处理):
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;
}
}
这里是logcat:
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):< / p>
<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行得到同样的错误,因为在这样的方法中,我无法传递视图或对其进行充气,就像我在oncreateview上所做的那样,无论我做什么操作,它的工作原理都很好..问题是我在用户放入内容之后需要那些编辑文本。我希望我能更清楚一些,我感谢所有的帮助。我还在接受,并且我想指出我已经尝试过每一个解决方案,即使我没有全部回复
答案 0 :(得分:1)
数组索引从0开始。请确保在访问数组时使用正确的索引值。
答案 1 :(得分:0)
这是错误的用法:
Monday m = new Monday();
monday = m.calculateDay();
试试这个:
Monday m = mAdapter.getItem(viewPager.getCurrentItem());
monday = m.calculateDay();
答案 2 :(得分:0)
您可以尝试使用:
getSupportFragmentManager().findFragmentById
获取 onDialogPositiveClick 中已启动的星期一片段。然后,您可以使用此片段来调用calculateDay方法。可以在片段的xml文件中设置id。您当前用于创建实例并调用calculateDay的实现是不正确的。
在我假设您已添加片段标记的Activity的xml布局中,添加id属性。然后在 onDialogPositiveClick 中,调用
Monday monday = (Monday) getSupportFragmentManager().
findFragmentById("that fragment id here");
现在,使用此星期日变量来调用calculateDay方法。
一些官方文档:http://developer.android.com/training/basics/fragments/communicating.html#Deliver
答案 3 :(得分:0)
之前已经说过了,但我会再说一遍。
for(int i=1;i<7;i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
应该是
for(int i = 0; i < subjects.length; i++){
s += subjects[i].getText().toString()+"-"+classes[i].getText().toString()+";";
编辑:说实话,我只是编写这样的代码
public enum Days {
MONDAY("Lunedi") { //should be string resource
public Fragment createFragment() {
return new Monday();
}
},
TUESDAY("Martedi") {
public Fragment createFragment() {
return new Tuesday();
}
},
WEDNESDAY("Mercoledi") {
public Fragment createFragment() {
return new Wednesday();
}
},
THURSDAY("Giovedi") {
public Fragment createFragment() {
return new Thursday();
}
},
FRIDAY("Venerdi") {
public Fragment createFragment() {
return new Friday();
}
},
SATURDAY("Sabato") {
public Fragment createFragment() {
return new Saturday();
}
};
private String name;
private Days(String name) {
this.name = name;
}
public String getName() { return name; }
public abstract Fragment createFragment();
}
class MyAdapter2 extends FragmentStatePagerAdapter
{
public MyAdapter2(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
Fragment fragment = null;
Days[] days = Days.values();
if(i < days.length) {
fragment = days[i].createFragment();
}
return fragment;
}
@Override
public int getCount() {
return Days.values().length;
}
@Override
public CharSequence getPageTitle(int position) {
Days[] days = Days.values();
if(position < days.length) {
return days[position].getName();
} else {
return null;
}
}
}
公平,最重要的是, subject [5] =(EditText)scrollView.findViewById(R.id.editTextMateria6); 为空,因为editTextMateria6
不存在于fragment_monday.xml
,editTextClasse6
也没有。{/ p>