0

我知道已经有人问过这样的问题,但是即使在研究了其他几个人的问题之后,我仍然无法让我的代码工作,主要是因为项目之间的差异使我很难理解做什么和如何做。

通常这会起作用,但是表单是在片段活动中,所以我很自然地遇到了几个问题(有些我已经解决了,有些没有),这是因为片段没有扩展这个或那个等。

package com.example;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;

public class Fragment1 extends Fragment{

@Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
    setHasOptionsMenu(true);
    View fragment1View=inflater.inflate(R.layout.fragment1,container,false);
    return fragment1View;

}

public void sendEmail(View button){
    final EditText formName=(EditText)findViewById(R.id.formName);
    String clientName=formName.getText().toString();
    final EditText formEmail=(EditText)findViewById(R.id.formEmail);
    String clientEmail=formEmail.getText().toString();
    final EditText formDetails=(EditText)findViewById(R.id.formDetails);
    String clientDetails=formDetails.getText().toString();

    Intent i = new Intent(Intent.ACTION_SEND);
    i.setType("message/rfc822");
    i.putExtra(Intent.EXTRA_EMAIL,new String[]{"example@email.com"});
    i.putExtra(Intent.EXTRA_SUBJECT, "New Private Service Request");
    i.putExtra(Intent.EXTRA_TEXT, "TODO: compose message body");
    try {
        startActivity(Intent.createChooser(i, "Send email with...?"));
    } catch (android.content.ActivityNotFoundException exception) {
        Toast.makeText(Fragment1.this.getActivity(), "No email clients installed on device!", Toast.LENGTH_LONG).show();
    }
}

}

基本上发生的情况是 EditText 控件的 findViewById 都带有下划线作为错误:“Fragment1 类型的方法 findViewById(int) 未定义”。我试过这个:

final EditText formName=(EditText)fragment1View.findViewById(R.id.formName);

And though this supposedly helped some people, for me the fragment1View is simply unresolved, even though I have declared it in the onCreateView.

I've tried alot of things and just don't know what to do. I wanted to give up and go with a standard Activity instead of Fragment, but it has to be fragment. Please help :/

4

2 回答 2

0

You need to create fields for every view you want to find in your fragment. THen in onCreateView write something like this:

    Button button;
    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
        setHasOptionsMenu(true);
        View fragment1View=inflater.inflate(R.layout.fragment1,container,false);
        button = (Button) fragment1View.findViewById(R.id.button);
        return fragment1View;
    }

You can't use findVIewById in fragment. But you can use it with inflated View in onCreateView which is actually is your fragment's layout.

于 2013-06-16T14:27:09.157 回答
0

Solution is while defining and initializing edittext define view with it. for example:

final EditText formName=(EditText)fragment1View.findViewById(R.id.formName);

do this whenever u initialize buttons in fragment. :) happy coding.

于 2014-06-07T11:05:30.700 回答