我想创建一个RatingBar
,它似乎没有用。似乎有两个问题。首先,当我不尝试用Java做任何事情时,例如setRating()
,评级星会出现,但超过20颗星出现5组。另一方面,当我尝试拨打setRating()
时,它会以NullPointerException
崩溃。如果有任何不同,我会在弹出窗口中显示评级以及其他信息。
这是xml。记下difficultyPopup和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>
这是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;
}
以下是错误日志:
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)
更新
看起来terrainRatingBar.getRating()也会抛出相同的错误。
更新2 NPE由Cristian修复,但布局问题仍然存在:
答案 0 :(得分:2)
请改用:
RatingBar terrainRatingBar = (RatingBar) dialog.findViewById(R.id. terrainPopup);
而不是:
RatingBar terrainRatingBar = (RatingBar) findViewById(R.id. terrainPopup);