2017-05-27 2 views
-3

Das ist meine Hauptklassendatei der Tätigkeit:Wenn ich versuche, diese Aktivität zu öffnen, ich bin immer Nullpointer

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; 
    } 
} 

Diese xml von activty:

<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> 

Dieser benutzerdefinierten XML-Datei für Listenansicht:

<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> 

Wenn ich versuche, diese Aktivität zu öffnen, aus einer anderen Aktivität ich erhalte null poi nter exception

Ich habe alles versucht, was ich weiß, einschließlich der doppelten Überprüfung jeder einzelnen Codezeile und der Umbenennung aller Tags, sodass sie nicht mit anderen Tags übereinstimmen. Also bitte, schaut euch den Code an und sagt mir, was los ist.

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

poste dein fehler log – jagapathi

+0

aktualisiert .. überprüfen sie log und teilen sie mir bitte –

+0

ein paar pics fehlen von Ihrer drawable überprüfen Sie alle Drawable's – jagapathi

Antwort

0

Dies liegt daran, Sie Thumbnails in onCreate() initialisiert. Sie sollten es im Adapter initialisieren. Führen Sie einen LayoutInflater in den Adapter ein. Dann initialisieren Sie Thumbnails mit dem LayoutInflater im Adapter.

+1

Danke Anees ... du hast gerade mein Leben Mann gerettet ... Glücklicher Ramadan Mann :) –

+0

Danke bro dir gleich. –

Verwandte Themen