2016-01-20 28 views
0

我不能運行的活動,因爲在類MyListAdapter方法getView變量t1 javacode
爲什麼ListView中有錯誤?

我不知道爲什麼,但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> 
+0

stacktrace似乎不是來自此版本的代碼。 stacktrace表示你正試圖在'getView()'中調用'LinearLayout#setVisibility()',但在你發佈的代碼中沒有證據。 – laalto

+0

雙擊LogCat中的這一行:at com.example.vipcard.ListCopunes $ MyListAdapter.getView(ListCopunes.java:132)然後告訴我們什麼是標記 –

回答

1

有兩個findViewById方法:在Activity類和View類中。

您正在調用Activity類的方法findViewById(R.id.cs_busName),但由於該視圖不是Activity的子項,因此無法在其中找到它,所以findViewById返回null。嘗試itemView.findViewById(R.id.cs_busName)