سؤال

I'm tring to create a RatingBar and it does not seem to be working. There appears to be two problems. First of all, when I don't try to do anything to it all in Java such as setRating() the rating stars appear but over 20 stars appear with 5 set. On the other hand, when I try to call setRating() it crashes with a NullPointerException. I am displaying the ratings along with other information inside a popup window if that makes any difference.

This is the xml. Take note of difficultyPopup and terrainPopup:

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <TableLayout
        android:id="@+id/add_spot_layout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:shrinkColumns="1"
        android:stretchColumns="1" >

       <TableRow
            android:layout_marginBottom="2sp"
            android:layout_marginTop="2sp" >
            <TextView android:text="Distance:" />
            <TextView android:id="@+id/distancePopup" />
        </TableRow>

       <TableRow
            android:layout_marginBottom="2sp"
            android:layout_marginTop="2sp" >
            <TextView android:text="Address:" />
            <TextView android:id="@+id/addrPopup" />
        </TableRow>

       <TableRow
            android:layout_marginBottom="2sp"
            android:layout_marginTop="2sp" >
            <TextView android:text="Type:" />
            <TextView android:id="@+id/typePopup" />
        </TableRow>

       <TableRow
            android:layout_marginBottom="2sp"
            android:layout_marginTop="2sp" >
            <TextView android:text="Terrain:" />
            <RatingBar
                android:id="@+id/terrainPopup"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:isIndicator="true"
                android:numStars="5"
                android:rating="1"
                android:stepSize="1"/>
        </TableRow>

        <TableRow>
            <TextView android:text="Difficulty:" />
            <RatingBar
                android:id="@+id/difficultyPopup"
                style="?android:attr/ratingBarStyleSmall"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:isIndicator="true"
                android:numStars="5"
                android:rating="1" 
                android:stepSize="1"/>

        </TableRow>

       <TableRow
            android:layout_marginBottom="2sp"
            android:layout_marginTop="2sp" >
            <TextView android:text="Description:" />
            <TextView
                android:id="@+id/descPopup"
                android:gravity="top"
                android:lines="2"
                android:maxLines="2"
                android:scrollHorizontally="false" />
        </TableRow>

    </TableLayout>

</ScrollView>

This is the Java:

public boolean onMarkerClick(Marker marker)
{
    HashMap<String, String> details = markerMap.get(marker.getId());

    Dialog dialog = new Dialog(MainActivity.this);
    dialog.setContentView(R.layout.popup);
    dialog.setTitle(details.get("name"));
    dialog.setCancelable(true);
    //there are a lot of settings, for dialog, check them all out!

    //set up text
    TextView textName = (TextView) dialog.findViewById(R.id.distancePopup);
    textName.setText(details.get("distance"));

    TextView textAddress= (TextView) dialog.findViewById(R.id.addrPopup);
    String addr = details.get("address").equals("null") ? "" : details.get("address");
    textAddress.setText(addr);

    TextView textType = (TextView) dialog.findViewById(R.id.typePopup);
    textType.setText(details.get("type"));

    TextView textDesc = (TextView) dialog.findViewById(R.id.descPopup);
    textDesc.setMovementMethod(new ScrollingMovementMethod());
    textDesc.setText(details.get("description"));

    RatingBar terrainRatingBar = (RatingBar) findViewById(R.id.terrainPopup); //No error here
    System.out.println("Terrain: "  + details.get("terrain"));//This works
    terrainRatingBar.setRating(Float.parseFloat(details.get("terrain"))); //This does not work


    float poo = difficultyRatingBar.getRating();
    System.out.println("poo: "  + poo); 

    //now that the dialog is set up, it's time to show it    
    dialog.show();
    return false;
}

Here is the error log:

03-13 23:39:55.937: D/dalvikvm(19197): GC_CONCURRENT freed 2251K, 18% free 11141K/13447K, paused 1ms+2ms
03-13 23:39:56.047: D/dalvikvm(19197): GC_CONCURRENT freed 738K, 18% free 11099K/13447K, paused 2ms+2ms
03-13 23:40:01.717: D/CLIPBOARD(19197): Hide Clipboard dialog at Starting input: finished by someone else... !
03-13 23:40:04.027: D/AndroidRuntime(19197): Shutting down VM
03-13 23:40:04.027: W/dalvikvm(19197): threadid=1: thread exiting with uncaught exception (group=0x40c3f1f8)
03-13 23:40:04.032: E/AndroidRuntime(19197): FATAL EXCEPTION: main
03-13 23:40:04.032: E/AndroidRuntime(19197): java.lang.NullPointerException
03-13 23:40:04.032: E/AndroidRuntime(19197):    at com.example.neatspots.MainActivity.onMarkerClick(MainActivity.java:461)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at com.google.android.gms.maps.GoogleMap$4.f(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at com.google.android.gms.internal.an$a.onTransact(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at android.os.Binder.transact(Binder.java:297)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at com.google.android.gms.maps.internal.IOnMarkerClickListener$Stub$Proxy.onMarkerClick(IOnMarkerClickListener.java:84)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.z.by.a(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.y.ba.a(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.y.d.c(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.y.bj.a(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.y.v.c(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.y.bf.onSingleTapConfirmed(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.d.v.onSingleTapConfirmed(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at maps.d.j.handleMessage(Unknown Source)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at android.os.Looper.loop(Looper.java:137)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at android.app.ActivityThread.main(ActivityThread.java:4507)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at java.lang.reflect.Method.invoke(Method.java:511)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
03-13 23:40:04.032: E/AndroidRuntime(19197):    at dalvik.system.NativeStart.main(Native Method)

UPDATE

It appears that terrainRatingBar.getRating() throws the same error as well.

UPDATE 2 The NPE was fixed by Cristian but the layout problem still persists:

enter image description here

هل كانت مفيدة؟

المحلول

Use this instead:

RatingBar terrainRatingBar = (RatingBar) dialog.findViewById(R.id. terrainPopup);

instead of:

RatingBar terrainRatingBar = (RatingBar) findViewById(R.id. terrainPopup);
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top