2017-05-27 65 views
-3

這是活動的我的主類文件:用於列表視圖當我試圖打開這個活動我得到的NullPointerException

<RelativeLayout> 

    <TextView 
    android:id="@+id/sometextview" 
    android:layout_width="match_parent" 
    android:layout_height="60dp" 
    android:text="Some Text" 
    android:textColor="#FFFFFF" 
    android:background="#000000" 
    android:textSize="25sp" 
    android:gravity="center" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    /> 

<ListView 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:id="@+id/hindpro" 
    android:layout_below="@+id/textView2"> 
</ListView> 


</RelativeLayout> 

這個自定義XML文件:

public class product extends AppCompatActivity { 

    String name[] = { 
      "q", 
      "w", 
      "e", 
      "r", 
      "t", 
      "y", 
      "u" 
    }; 

    Integer pisc[] = { 
      R.drawable.a, 
      R.drawable.e, 
      R.drawable.c, 
      R.drawable.d, 
      R.drawable.e, 
      R.drawable.a, 
      R.drawable.d 
    }; 
    String location[] = { 
      "a", 
      "s", 
      "dr", 
      "d", 
      "f", 
      "r", 
      "r" 
    }; 

    String lang[] = { 
      "Hindi", 
      "Hindi", 
      "Hindi", 
      "Hindi", 
      "Hindi", 
      "Hindi", 
      "Hindi" 

    }; 

    Float rating[] = { 
      5f, 
      4.5f, 
      4.5f, 
      4f, 
      4f, 
      3.5f, 
      3f 

    }; 

    String rate[] = { 
      "Rated by 452+ people", 
      "Rated by 427+ people", 
      "Rated by 369+ people", 
      "Rated by 332+ people", 
      "Rated by 279+ people", 
      "Rated by 259+ people", 
      "Rated by 129+ people" 
    }; 


    TextView naam,loc,langs,rat; 
    Button thumbnails; 
    ListView hind; 
    RatingBar rates; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_product); 


     hind = (ListView) findViewById(R.id.hindpro); 
     naam = (TextView) findViewById(R.id.nameh); 
     loc = (TextView) findViewById(R.id.locations); 
     langs = (TextView) findViewById(R.id.lang); 
     rat = (TextView) findViewById(R.id.rating); 
     thumbnails = (Button) findViewById(R.id.pic); 
     rates = (RatingBar)findViewById(R.id.ratingBar); 



     class customadapterpz extends BaseAdapter { 



      @Override 
      public int getCount() { 
       return name.length; 
      } 

      @Override 
      public Object getItem(int position) { 
       return null; 
      } 

      @Override 
      public long getItemId(int position) { 
       return 0; 
      } 

      @Override 
      public View getView(int positionq, View convertView, ViewGroup 
     parent) { 
       View view4 = getLayoutInflater().inflate(R.layout.hindpro, 
null); 


       if (name[positionq] != null) { 




        Bitmap bitmahind = 
decodeSampledBitmapFromResource(product.this.getResources(), 
          pisc[positionq], 150, 150); 
        BitmapDrawable bitmapDrawableaw = new 
BitmapDrawable(getResources(), bitmahind); 
        thumbnails.setBackgroundDrawable(bitmapDrawableaw); 
        // thumbnails.setImageBitmap(bitmahind); 
        naam.setText(name[positionq]); 
        loc.setText(location[positionq]); 
        langs.setText(lang[positionq]); 
        rat.setText(rate[positionq]); 
        rates.setRating(rating[positionq]); 

       } 

       return view4; 
      } 
     } 

     customadapterpz item23 = new customadapterpz(); 
     hind.setAdapter(item23); 






    } 

    public static Bitmap decodeSampledBitmapFromResource(Resources res, int 
resId, int reqWidth, int reqHeight) { 

     // First decode with inJustDecodeBounds=true to check dimensions 
     final BitmapFactory.Options options = new BitmapFactory.Options(); 
     options.inJustDecodeBounds = true; 
     BitmapFactory.decodeResource(res, resId, options); 

     // Calculate inSampleSize 
     options.inSampleSize = calculateInSampleSize(options, reqWidth, 
reqHeight); 

     // Decode bitmap with inSampleSize set 
     options.inJustDecodeBounds = false; 
     return BitmapFactory.decodeResource(res, resId, options); 
    } 

    public static int calculateInSampleSize(BitmapFactory.Options options, 
int reqWidth, int reqHeight) { 
     // Raw height and width of image 
     final int height = options.outHeight; 
     final int width = options.outWidth; 
     int inSampleSize = 4; 

     if (height > reqHeight || width > reqWidth) { 

      final int halfHeight = height/8; 
      final int halfWidth = width/8; 

      // Calculate the largest inSampleSize value that is a power of 2 
and keeps both 
      // height and width larger than the requested height and width. 
      while ((halfHeight/inSampleSize) >= reqHeight 
        && (halfWidth/inSampleSize) >= reqWidth) { 
       inSampleSize *= 8; 
      } 
     } 

     return inSampleSize; 
    } 
} 

activty的這個XML:

<RelativeLayout> 
    <Button 
     android:id="@+id/pic" 
     android:layout_width="120dp" 
     android:layout_height="150dp" 
     android:layout_marginStart="11dp" 
     android:layout_marginTop="13dp" 
     android:layout_marginBottom="11dp" 
     android:layout_alignParentTop="true" 
     android:layout_alignParentStart="true" /> 

    <TextView 
     android:id="@+id/nameh" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentStart="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginStart="149dp" 
     android:textColor="#FFFFFF" 
     android:layout_marginTop="14dp" 
     android:textSize="20sp" 
     android:text="Name" /> 

    <TextView 
     android:id="@+id/locations" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignStart="@+id/nameh" 

     android:layout_below="@+id/nameh" 
     android:text="Mansarovar, jaipur" /> 

    <TextView 
     android:id="@+id/lang" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 

     android:textSize="12sp" 
     android:text="Hindi" 
     android:layout_below="@+id/locations" 
     android:layout_alignStart="@+id/locations" /> 

    <RatingBar 
     android:id="@+id/ratingBar" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     style="?android:attr/ratingBarStyleSmall" 
     android:scaleX="2" 
     android:scaleY="2" 
     android:layout_centerVertical="true" 
     android:layout_alignEnd="@+id/locations" /> 

    <TextView 
     android:id="@+id/rating" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="Rated by 129 people" 
     android:textSize="10sp" 
     android:layout_above="@+id/book" 
     android:layout_alignStart="@+id/lang" /> 

    <Button 
     android:id="@+id/book" 
     android:layout_width="wrap_content" 
     android:layout_marginRight="10dp" 
     android:layout_height="wrap_content" 
     android:text="BOOK" 
     android:layout_alignBottom="@+id/pic" 
     android:layout_alignParentEnd="true" /> 





</RelativeLayout> 

當我嘗試從其他活動中打開此活動時,我收到了null poi例外

我嘗試了我所知的一切,包括仔細檢查每一行代碼並重命名每個標記,以便它們與任何其他標記都不匹配。 所以,請大家,看看代碼,並告訴我什麼是錯的。

的logcat:

05-27 21:56:19.974 22216-22216/com.example.pankaz.qw E/AndroidRuntime: FATAL EXCEPTION: main 
                       Process: com.example.pankaz.qw, PID: 22216 
                       java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setBackgroundDrawable(android.graphics.drawable.Drawable)' on a null object reference 
                        at com.example.pankaz.qw.product$1customadapterpz.getView(product.java:137) 
                        at android.widget.AbsListView.obtainView(AbsListView.java:2346) 
                        at android.widget.ListView.measureHeightOfChildren(ListView.java:1280) 
                        at android.widget.ListView.onMeasure(ListView.java:1188) 
                        at android.view.View.measure(View.java:18788) 
                        at android.widget.RelativeLayout.measureChild(RelativeLayout.java:676) 
                        at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:479) 
                        at android.view.View.measure(View.java:18788) 
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                        at android.support.v7.widget.ContentFrameLayout.onMeasure(ContentFrameLayout.java:139) 
                        at android.view.View.measure(View.java:18788) 
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                        at android.view.View.measure(View.java:18788) 
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                        at android.view.View.measure(View.java:18788) 
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                        at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1465) 
                        at android.widget.LinearLayout.measureVertical(LinearLayout.java:748) 
                        at android.widget.LinearLayout.onMeasure(LinearLayout.java:630) 
                        at android.view.View.measure(View.java:18788) 
                        at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5951) 
                        at android.widget.FrameLayout.onMeasure(FrameLayout.java:194) 
                        at com.android.internal.policy.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2643) 
                        at android.view.View.measure(View.java:18788) 
                        at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2100) 
                        at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1216) 
                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1452) 
                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107) 
                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013) 
                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858) 
                        at android.view.Choreographer.doCallbacks(Choreographer.java:670) 
                        at android.view.Choreographer.doFrame(Choreographer.java:606) 
                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844) 
                        at android.os.Handler.handleCallback(Handler.java:739) 
                        at android.os.Handler.dispatchMessage(Handler.java:95) 
                        at android.os.Looper.loop(Looper.java:148) 
                        at android.app.ActivityThread.main(ActivityThread.java:5417) 
                        at java.lang.reflect.Method.invoke(Native Method) 
                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
+0

發佈錯誤日誌 – jagapathi

+0

已更新..請檢查日誌並告訴我 –

+0

您可繪製的檢查中缺少一些圖片所有可繪製的 – jagapathi

回答

0

這是因爲你初始化縮略圖中的onCreate()。您應該在適配器內初始化它。將LayoutInflater傳遞到適配器中。然後使用適配器內的LayoutInflater初始化縮略圖。

+1

Thanx Anees ...你剛剛救了我的生命人...快樂的齋月男人:) –

+0

感謝兄弟同樣給你。 –

相關問題