为什么ListView中有错误?

时间:2016-01-20 20:07:10

标签: android android-layout listview

我无法运行活动,因为在javacode中 在类MyListAdapter方法中getView变量t1

我不知道为什么,但TextView cs_desc和cs_busName 可编辑的

     FATAL EXCEPTION: main
 Process: com.example.vipcard, PID: 5042
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.LinearLayout.setVisibility(int)' on a null object reference
at com.example.vipcard.ListCopunes$MyListAdapter.getView(ListCopunes.java:132)
at android.widget.AbsListView.obtainView(AbsListView.java:2349)
at android.widget.ListView.makeAndAddView(ListView.java:1864)
at android.widget.ListView.fillDown(ListView.java:698)
at android.widget.ListView.fillFromTop(ListView.java:759)
at android.widget.ListView.layoutChildren(ListView.java:1673)
at android.widget.AbsListView.onLayout(AbsListView.java:2153)
at android.view.View.layout(View.java:15686)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1077)
at android.view.View.layout(View.java:15686)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15686)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at com.android.internal.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:494)
at android.view.View.layout(View.java:15686)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.widget.FrameLayout.layoutChildren(FrameLayout.java:579)
at android.widget.FrameLayout.onLayout(FrameLayout.java:514)
at android.view.View.layout(View.java:15686)
at android.view.ViewGroup.layout(ViewGroup.java:5039)
at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2086)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1843)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1061)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5891)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:767)
at android.view.Choreographer.doCallbacks(Choreographer.java:580)
at android.view.Choreographer.doFrame(Choreographer.java:550)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:753)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5294)
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:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:699)

// CLASS LISTCOUPONES:

public class ListCopunes extends Activity {
    private ArrayList<Coupones> mycopune;

    int flag;
    String url="https://fs.kamatera.com/index.php/s/TdXaaG8iTqCgeux/download?path=%2F&files=";
    @SuppressWarnings("unchecked")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_copunes);
        Intent back =getIntent();
        Window w =getWindow();

        mycopune =(ArrayList<Coupones>)back.getSerializableExtra("coupones");
        populateListView();
        registerClickCallback();

    }


    public void populateListView() {
        ArrayAdapter<Coupones> adapter = new MyListAdapter();
        ListView list = (ListView) findViewById(R.id.listCopune);
        list.setAdapter(adapter);

    }

    public void registerClickCallback() {
        ListView list = (ListView) findViewById(R.id.listCopune);
        list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View viewClicked,int position, long id) {

                Coupones coup = mycopune.get(position);
                Intent intent = new Intent(ListCopunes.this,CopuneView.class);
                intent.putExtra("specx",coup);
                startActivity(intent);
                finish();
            }
        });

    }
    public class MyListAdapter extends ArrayAdapter<Coupones> {
        public MyListAdapter() {
            super(ListCopunes.this, R.layout.item_copunes, mycopune);
        }
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            // Make sure we have a view to work with (may have been given null)
            View itemView = convertView;

            if (itemView == null) 
                itemView = getLayoutInflater().inflate(R.layout.item_copunes, parent, false);

            // Find the to work with.
            Coupones IS = mycopune.get(position);

            // name the view:
            TextView nameText = (TextView) itemView.findViewById(R.id.cs_name_item);
            nameText.setText(IS.getName());

            //Image the view:
            SmartImageView imageView = (SmartImageView)itemView.findViewById(R.id.cs_image);
            DisplayMetrics metrics = new DisplayMetrics();
            getWindowManager().getDefaultDisplay().getMetrics(metrics);
            int screenHeight = metrics.heightPixels;
           imageView.setMaxHeight((screenHeight/3)-(screenHeight/12));
            imageView.setImageUrl(url+IS.getImage());
            //this is A problem
            TextView t1 = (TextView)findViewById(R.id.cs_busName);
            t1.setText(IS.getNameBusiness());

                // Old Price:
                TextView yearText = (TextView) itemView.findViewById(R.id.cs_1oldprice);
                yearText.setText("₪"+IS.getPrice1());
                yearText.setPaintFlags(yearText.getPaintFlags() | Paint.STRIKE_THRU_TEXT_FLAG);

                // New Price:
                TextView condionText = (TextView) itemView.findViewById(R.id.cs_1newprice);
                condionText.setText(("₪"+IS.getPrice2()).toString());


            return itemView;
        }
    }
}

// XML activity_copunes

 <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"
        tools:context="com.example.vipcard.ListCopunes" >
    <ListView
    android:id="@+id/listCopune"
     android:layout_width="match_parent"
     android:layout_height="match_parent"
     android:listSelector="@android:color/transparent"
     android:headerDividersEnabled="false"
     android:footerDividersEnabled="false"
     android:dividerHeight="8dp" 
     android:divider="@color/grey3" 
     android:cacheColorHint="@color/grey3"
    android:drawSelectorOnTop="false"> 
     </ListView>
    </RelativeLayout>
//XML item_copunes
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:tools="http://schemas.android.com/tools"
      android:background="@color/grey3"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <LinearLayout
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:paddingTop="5dp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:background="@color/white" >
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/cs_name_item"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center"
        android:text=""
        android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/cs_busName"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="end"
        android:lines="1"
        android:text="" />
    </LinearLayout>

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <com.loopj.android.image.SmartImageView
            android:id="@+id/cs_image"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginTop="5dp"
            android:adjustViewBounds="true"
            android:baselineAlignBottom="true"
            android:src="@drawable/titled" />

        <com.loopj.android.image.SmartImageView
            android:id="@+id/cs_imagebusiness"
            android:layout_gravity="end"
            android:layout_width="120dp"
            android:layout_height="50dp"
            android:src="@null" />

    </FrameLayout>
<LinearLayout 
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:id="@+id/cs_desc"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:gravity="right"
        android:lines="3"
        android:ems="10"
        android:text=""/>
        </LinearLayout>


        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="מחיר רגיל:" />

                <TextView
                    android:id="@+id/cs_1oldprice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:text=""
                    android:ems="10"
                    android:textColor="@color/red"
                    android:typeface="sans" />
            </RelativeLayout>
        </LinearLayout>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content" >

            <RelativeLayout
                android:layout_width="wrap_content"
                android:layout_height="match_parent" >

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentRight="true"
                    android:text="מחיר מבצע:" />

                <TextView
                    android:id="@+id/cs_1newprice"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:textStyle="bold"
                    android:textColor="@color/green"
                    android:ems="10"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:text="" />
            </RelativeLayout>
        </LinearLayout>


</LinearLayout>


</RelativeLayout>

1 个答案:

答案 0 :(得分:1)

有两种findViewById方法:在Activity类和View类中。

您正在调用Activity类的方法findViewById(R.id.cs_busName),但由于该视图不是Activity的子项,因此无法在其中找到它,因此findViewById会返回null。试试itemView.findViewById(R.id.cs_busName)