自定义适配器中的按钮不会将我带到下一个屏幕

时间:2014-06-03 19:20:50

标签: android eclipse android-intent custom-adapter

我的自定义Intent中有一个新的Adapter。如果单击一个按钮,则应该将我带到一个新屏幕。但是,我一直得到Runtimeexception

    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_near_me);

    if (savedInstanceState == null) {
        getFragmentManager().beginTransaction()
        .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
        {
            View rootView = inflater.inflate(R.layout.fragment_near_me, container, false);

            getListAdresses(39, -77);
            return rootView;
        }

public ArrayList<ArrayList<String>> getListAdresses(double str, double str1)
        {
            ArrayList<ArrayList<String>> ans = new ArrayList<ArrayList<String>>();
            for (int i = 0; i < list.size(); i++)
            {
                /**
                 * 10 = lat
                 * 9 = long
                 */
                Double dlat = Double.parseDouble(list.get(i).get(10).toString());
                Double dlng = Double.parseDouble(list.get(i).get(9).toString());
                if ((dlat < (lat + 1) && dlat > (lat - 1)) 
                        && (dlng < (lng + 1) && dlng > (lng - 1)))
                {
                    ans.add(list.get(i));
                }
            }
            return ans;
        }       


    public static class Row
    {
        String storeName;
        String streetAdress;
        String location;
        String country;

        Row(String str1, String str2, String str3, String str4)
        {
            storeName = str1;
            streetAdress = str2;
            location = str3;
            country = str4;
        }
    }

    public static class CustomAdapter extends BaseAdapter
    {
        ArrayList<Row> specList;
        Context conty;

        CustomAdapter(Context c)
        {
            conty = c;
            specList = new ArrayList<Row>();
            ArrayList<String> storeName = new ArrayList<String>();
            ArrayList<String> streetAdress = new ArrayList<String>();
            ArrayList<String> location = new ArrayList<String>();
            ArrayList<String> country = new ArrayList<String>();
            for (int i = 0; i < ar.size(); i++)
            {
                storeName.add(ar.get(i).get(0).toString());
                streetAdress.add(ar.get(i).get(2).toString());
                location.add(ar.get(i).get(3).toString() + ", " 
                + ar.get(i).get(4).toString() + " " + ar.get(i).get(5).toString());
                country.add(ar.get(i).get(7).toString());
            }

            for (int i = 0; i < ar.size(); i++)
            {
                specList.add(new Row(storeName.get(i), streetAdress.get(i), 
                        location.get(i), country.get(i)));
            }
        }       

        @Override
        public int getCount() {
            // TODO Auto-generated method stub
            return specList.size();
        }

        @Override
        public Object getItem(int position) {
            // TODO Auto-generated method stub
            return specList.get(position);
        }

        @Override
        public long getItemId(int position) {
            // TODO Auto-generated method stub
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // TODO Auto-generated method stub
            LayoutInflater inflater = (LayoutInflater) conty.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            final View rw = inflater.inflate(R.layout.row, parent, false);
            TextView name = (TextView) rw.findViewById(R.id.textView1);
            TextView adress = (TextView) rw.findViewById(R.id.textView2);
            TextView location = (TextView) rw.findViewById(R.id.textView3);
            TextView country = (TextView) rw.findViewById(R.id.textView4);
            final Row temp = specList.get(position);
            name.setText(temp.storeName);
            adress.setText(temp.streetAdress);
            location.setText(temp.location);
            country.setText("  Phone:" + temp.country);
            Button call = (Button) rw.findViewById(R.id.button1);
            call.setOnClickListener(new OnClickListener(){

                @Override
                public void onClick(View v) {
                    Uri u = Uri.parse("tel:" + temp.country);
                    Intent i = new Intent(Intent.ACTION_CALL, u);
                    rw.getContext().startActivity(i);
                }

            });

            Button map = (Button) rw.findViewById(R.id.button2); 
            map.setOnClickListener(new    OnClickListener(){

                    @Override
                    public void onClick(View v) {

                               Intent i = new Intent(rw.getContext(), NearResults.class);
                               rw.getContext().startActivity(i);

                    }

                });
            return rw;
        }

这是我的自定义适配器中的listview代码。这是用Near.class编写的。

这里是logcat:

06-03 15:28:53.849: E/AndroidRuntime(22807): FATAL EXCEPTION: main
06-03 15:28:53.849: E/AndroidRuntime(22807): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapp/com.example.myapp.NearResults}: java.lang.IllegalArgumentException: No view found for id 0x7f060014 (com.example.myapp:id/container) for fragment PlaceholderFragment{4292b3b0 #0 id=0x7f060014}
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2359)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.ActivityThread.access$700(ActivityThread.java:165)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1326)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.os.Looper.loop(Looper.java:137)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.ActivityThread.main(ActivityThread.java:5455)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at java.lang.reflect.Method.invokeNative(Native Method)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at java.lang.reflect.Method.invoke(Method.java:525)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at dalvik.system.NativeStart.main(Native Method)
06-03 15:28:53.849: E/AndroidRuntime(22807): Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f060014 (com.example.vipar:id/container) for fragment PlaceholderFragment{4292b3b0 #0 id=0x7f060014}
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:895)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1075)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.BackStackRecord.run(BackStackRecord.java:682)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1455)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.Activity.performStart(Activity.java:5381)
06-03 15:28:53.849: E/AndroidRuntime(22807):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2276)
06-03 15:28:53.849: E/AndroidRuntime(22807):    ... 11 more

这是NearMe的xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.vipar.NearMe$PlaceholderFragment" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" >
    </ListView>

</RelativeLayout>

近乎成人的xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="90dp"
        android:layout_marginTop="161dp"
        android:text="Small Text"
        android:textAppearance="?android:attr/textAppearanceSmall" />

</RelativeLayout>

xml for Row:

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

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:text="Medium Text"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView2"
        android:text="TextView" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView3"
        android:text="TextView" />

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBottom="@+id/textView4"
        android:layout_alignParentRight="true"
        android:layout_marginRight="14dp"
        android:adjustViewBounds="true"
        android:src="@drawable/logo" />

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView4"
        android:text="Call" />

    <Button
        android:id="@+id/button2"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/button1"
        android:layout_alignBottom="@+id/button1"
        android:layout_alignRight="@+id/imageView1"
        android:text="Map It" />

</RelativeLayout>

2 个答案:

答案 0 :(得分:0)

UPDATE(2):您没有在activity_near_me.xml中具有id等于'container'的Layout / ViewGroup。将“android:id =”@ + id / container“行添加到包含片段

的布局中

更新:我认为您的问题与this有关,但我不知道如何在活动中设置视图层次结构以及如何添加片段

答案 1 :(得分:-2)

问题是您没有检查点击事件是否来自正确的View。 试着这样做:

Button map = (Button) rw.findViewById(R.id.button2);
map.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        if(v.getId() == R.id.button2) {
            Intent i = new Intent(Your_Activity_Name.this, NearResults.class);
            startActivity(i);
        }
    }
});