无法读取片段类中的EditText

时间:2012-07-20 17:55:19

标签: android dialog insert fragment

我有两个标签,每个标签都是片段。在其中一个片段(Afragment.claas)中,我有一个对话框来插入一些数据,我想将它们保存在数据库中。当我填写文本字段并按“确定”时,应用程序将停止。

我认为在初始化edittext时出现了问题。

name = (EditText) activity.findViewById(R.id.etProviderName);

以上语句返回null

这是我的代码:

import android.app.Activity;
import android.app.AlertDialog;
import android.app.ListFragment;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.CursorAdapter;
import android.widget.EditText;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;

public class Afragment extends ListFragment {

    DBAdapter db;
    Activity activity;
    private static final String fields[] = { "provider._providerName", "_providerMobile" };

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setHasOptionsMenu(true);
    }

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

        initAdapter();

    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
        case R.id.menu_add:
            add();
            return (true);

        case R.id.menu_reset:
            initAdapter();
            return (true);

        case R.id.menu_info:
            Toast.makeText(activity, "Developed By Omar Al-Shammary", Toast.LENGTH_LONG)
                    .show();
            return (true);
        }

        return (super.onOptionsItemSelected(item));
    }

    private void add() {
        // TODO Auto-generated method stub
        System.out.println("insider Add Method");
        final View addView = activity.getLayoutInflater().inflate(R.layout.add_provider, null);

        new AlertDialog.Builder(activity).setTitle("Add a Provider").setView(addView)
                .setPositiveButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int whichButton) {
                        System.out.println("Befor calling insert in providers");
                        insertInProviders();
                        System.out.println("after calling insert in providers");
                    }
                }).setNegativeButton("Cancel", null).show();

        initAdapter();

    }

    private void initAdapter() {
        activity = getActivity();
        db = new DBAdapter(activity);
        fillTheList();
    }

    public void fillTheList()
    {
        db.open();
        Cursor data = db.getAllProviders();
        CursorAdapter dataSource = new SimpleCursorAdapter(activity,
            R.layout.row, data, fields,
            new int[] { R.id.first, R.id.last });

        setListAdapter(dataSource);
        db.close();


        setListAdapter(dataSource);
    }

    public void insertInProviders()
    {
        EditText name,location,number;
        name = (EditText) activity.findViewById(R.id.etProviderName);
        location = (EditText) activity.findViewById(R.id.etProviderName);
        number = (EditText) activity.findViewById(R.id.etProviderName);
        if(name == null)
            System.out.println("EditeText name is null");
        String provName = name.getText().toString();
        if(location == null)
            System.out.println("location is null");
        String provLocation = location.getText().toString();
        String provNumber = number.getText().toString();

        System.out.println("Before Open");

        db.open();
        System.out.println("Before DB.Insert");
        db.insertProvider(provName, provLocation, provNumber);
        System.out.println("Before Close");
        db.close();
    }
}

add_provider.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/tvProviderName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Name" />

    <EditText
        android:id="@+id/etProviderName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" >

        <requestFocus />
    </EditText>

    <TextView
        android:id="@+id/tvProvLocation"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Location" />

    <EditText
        android:id="@+id/etProvLocation"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="textPersonName" />

    <TextView
        android:id="@+id/tvProvNumber"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Number" />

    <EditText
        android:id="@+id/etProvNumber"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" />

</LinearLayout>

2 个答案:

答案 0 :(得分:0)

这是我用于在Fragments中声明按钮的代码 - 也许它可以帮助你。

我要做的是访问膨胀的布局,然后您应该能够访问它下面的项目。如果这是有道理的,请告诉我。

LinearLayout theLayout = (LinearLayout)inflater.inflate(R.layout.tab_frag1_layout, container, false);
// Register for the Button.OnClick event
Button b = (Button)theLayout.findViewById(R.id.frag1_button);
        @Override
        public void onClick(View v) {
            Toast.makeText(Tab1Fragment.this.getActivity(), "OnClickMe button clicked", Toast.LENGTH_LONG).show();

        }
    });
    return theLayout;

所以也许你应该尝试这个,因为你将你的视图膨胀为“addView”

name = (EditText) addView.findViewById(R.id.etProviderName);

答案 1 :(得分:0)

findViewById不应由活动调用,而应由封闭视图调用。你应该有类似的东西:

在课堂上

View aView;

在方法中:

LayoutInflater inflater = getActivity().getLayoutInflater();
aView = inflater.inflate(R.layout.add_provider, null);

...

EditText name = (EditText) aView.findViewById(R.id.etProviderName);

我希望它有所帮助。