Button 当按钮和文本字段位于不同的片段中时,无法执行活动的方法
我有三个片段,第一个片段中有文本字段,第三个片段中有一个按钮 当我为最后一个片段中的按钮设置onClick以使用我的方法来获取textfields中的文本时,我得到了以下错误:Button 当按钮和文本字段位于不同的片段中时,无法执行活动的方法,button,android-activity,onclick,fragment,Button,Android Activity,Onclick,Fragment,我有三个片段,第一个片段中有文本字段,第三个片段中有一个按钮 当我为最后一个片段中的按钮设置onClick以使用我的方法来获取textfields中的文本时,我得到了以下错误: "Could not execute method of the activity" EditText在MainActivity.java中初始化如下: EditText name = (EditText) findViewById(R.id.editText10); public class MainActivit
"Could not execute method of the activity"
EditText在MainActivity.java中初始化如下:
EditText name = (EditText) findViewById(R.id.editText10);
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
public void createPdf(View view)throws Exception{
Document document=new Document();
File root = new File(Environment.getExternalStorageDirectory(), "Report");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, "Report.pdf");
PdfWriter.getInstance(document, new FileOutputStream(gpxfile));
document.open();
PdfPTable tableone = new PdfPTable(1);
tableone.setWidthPercentage(100);
PdfPCell c5 = new PdfPCell(new Phrase("Report"));
c5.setHorizontalAlignment(Element.ALIGN_CENTER);
tableone.addCell(c5);
document.add(tableone);
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
//table 2
PdfPTable tabletwo = new PdfPTable(2);
tabletwo.setWidthPercentage(100);
EditText driver = (EditText) findViewById(R.id.editText10);
EditText customer = (EditText) findViewById(R.id.editText9);
tabletwo.addCell("Driver: " + driver.getText().toString());
tabletwo.addCell("Customer: " + customer.getText().toString());
tabletwo.addCell("Cell:");
tabletwo.addCell("County:");
tabletwo.addCell("Date:");
tabletwo.addCell("Date of event:");
tabletwo.addCell("Tank:");
tabletwo.addCell("Trailer:");
tabletwo.addCell("Contact:");
tabletwo.addCell("Trucknumber:");
document.add(tabletwo);
}
}
我通过以下操作获取文本:
Textfield.getText().toString();
我假设我得到这个错误是因为文本字段不在第三个片段中
那么我怎样才能得到我的文本呢
提前谢谢
代码更新(在第一个片段和第三个片段之间没有桥接)
MainActivity.java:
EditText name = (EditText) findViewById(R.id.editText10);
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager) findViewById(R.id.pager);
PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
public void createPdf(View view)throws Exception{
Document document=new Document();
File root = new File(Environment.getExternalStorageDirectory(), "Report");
if (!root.exists()) {
root.mkdirs();
}
File gpxfile = new File(root, "Report.pdf");
PdfWriter.getInstance(document, new FileOutputStream(gpxfile));
document.open();
PdfPTable tableone = new PdfPTable(1);
tableone.setWidthPercentage(100);
PdfPCell c5 = new PdfPCell(new Phrase("Report"));
c5.setHorizontalAlignment(Element.ALIGN_CENTER);
tableone.addCell(c5);
document.add(tableone);
document.add(Chunk.NEWLINE);
document.add(Chunk.NEWLINE);
//table 2
PdfPTable tabletwo = new PdfPTable(2);
tabletwo.setWidthPercentage(100);
EditText driver = (EditText) findViewById(R.id.editText10);
EditText customer = (EditText) findViewById(R.id.editText9);
tabletwo.addCell("Driver: " + driver.getText().toString());
tabletwo.addCell("Customer: " + customer.getText().toString());
tabletwo.addCell("Cell:");
tabletwo.addCell("County:");
tabletwo.addCell("Date:");
tabletwo.addCell("Date of event:");
tabletwo.addCell("Tank:");
tabletwo.addCell("Trailer:");
tabletwo.addCell("Contact:");
tabletwo.addCell("Trucknumber:");
document.add(tabletwo);
}
}
FragmentOne.java:
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout_one,container,false);
}
}
我有一个PagerAdapter可以在碎片之间滑动:
public class PagerAdapter extends FragmentPagerAdapter {
public PagerAdapter(FragmentManager fm){
super(fm);
}
@Override
public Fragment getItem(int arg0){
switch (arg0){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
@Override
public int getCount(){
return 3;
}
}
以下是我的活动_main.xml:
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
</android.support.v4.view.ViewPager>
以下是FragmentOne的xml:
<ScrollView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#fff">
<TableLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:background="#fff">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView"
android:src="@drawable/logo"
android:layout_marginTop="20dp" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText10"
android:hint="Chaufför" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/editText9"
android:hint="Kund" />
</TableLayout>
</ScrollView>
FragmentThree只有一个带有onClick=“createPdf”的按钮,您应该像使用从第一个片段到第三个片段的桥梁一样使用该活动。正确的方法是使用接口:片段声明和活动实现:
public ThirdFragment extends Fragment{
ThirdFragmentInterface mInterface;
...
public interface ThirdFragmentInterface {
public String getText();
}
}
在您的活动中:
public MainActivity extends Activity implements ThirdFragmentInterface {
...
@Override
public String getText(){
return Textfield.getText().toString();
}
}
然后在片段中,您可以获得文本值:
String text = mInterface.getText();
希望这有帮助^^ 如果我也想从我的第二个片段中获取数据,那该怎么做呢?完全一样。在接口中创建一个“getSecondText()”并在活动中实现它。我仍然收到相同的错误。错误发生在我的方法CreatePDF()中,该方法位于main活动的以下行:“tabletwo.addCell(“Driver:+Driver.getText().toString());”如果我将第三个片段中的按钮移动到第一个片段中,我不会看到此错误。请提供更多代码详细信息,以便我们可以更好地帮助您。您在活动中找不到片段视图
(EditText驱动程序=(EditText)findViewById(R.id.editText10);)
您可以在片段中编写一个方法,如公共字符串getText10(){return txtText10.getText().toString()…}
,然后在活动中使用该片段实例调用此方法<代码>MFFragment1.getText10()代码>