main.xml text
xmlns: tools = "http://schemas.android.com/tools"
android: id = "@ + id/LinearLayout1"
android: layout_width = "match_parent"
android: layout_height = "match_parent"
android: orientation = "vertical"
android: paddingBottom = "@ dimen / activity_vertical_margin"
android: paddingLeft = "@ dimen / activity_horizontal_margin"
android: paddingRight = "@ dimen / activity_horizontal_margin"
android: paddingTop = "@ dimen / activity_vertical_margin"
tools: context = ". MainActivity">android: id = "@ + id/mybar1"
android: numStars = "4"
android: stepSize = "0.5"
android: isIndicator = "false"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: id = "@ + id/mybar2"
android: numStars = "4"
android: isIndicator = "true"
android: rating = "2"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />android: id = "@ + id / mytext"
android: layout_width = "wrap_content"
android: layout_height = "wrap_content" />
mainActivity.java
package com.example.ratingbarproject1;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.RatingBar;
import android.widget.RatingBar.OnRatingBarChangeListener;
import android.widget.TextView;
public class MainActivity extends Activity {
private RatingBar ratingbar = null;
private TextView textview = null;
protected void onCreate (Bundle savedInstanceState) {
this.ratingbar = (RatingBar) super.findViewById (R.id.mybar1) ;/ / get the component
this.textview = (TextView) super.findViewById (R.id.mytext);
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
this.ratingbar.setOnRatingBarChangeListener (new OnRatingBarChangeListener1 ()) ;/ / set the listener
}
private class OnRatingBarChangeListener1 implements OnRatingBarChangeListener {
/ / @ Override
public void onRatingChanged (RatingBar ratingBar, float rating,
boolean fromUser) {
/ / Display the text in the display of stars
MainActivity.this.textview.append ("*** Current Star Rating:"
+ RatingBar.getNumStars () + "*** the current step:" + ratingBar.getStepSize () + "\ n");
}
}
@ Override
public boolean onCreateOptionsMenu (Menu menu) {
/ / Inflate the menu; this adds items to the action bar if it is present
.getMenuInflater () inflate (R.menu.main, menu);.
return true;
}
}
Error. . . . . . . . . . . . . . . .
<-! Main posts under Banner (D4) -><-! Posts under the main text (D5) ->
Reply:
this.ratingbar = (RatingBar) super.findViewById (R.id.mybar1) ;/ / get the component this.textview = (TextView) super.findViewById (R.id.mytext); super.onCreate (savedInstanceState); setContentView (R.layout.activity_main);
You can not go get setContentView components come into the following
Reply:
Like
setContentView (R.layout.activity_main);
this.ratingbar = (RatingBar) super.findViewById (R.id.mybar1) ;/ / get the component
this.textview = (TextView) super.findViewById (R.id.mytext);
Reply:
supper do not need
Such
like:
Reply:
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
this.ratingbar = (RatingBar) findViewById (R.id.mybar1) ;/ / get the component
this.textview = (TextView) findViewById (R.id.mytext);
Reply:
this is not necessary.
Reply:
Order oncreate-setContentView-findView-setView
Reply:
Support ah, factory good
Reply:
Replies will score yet
Reply:
Not loaded layout,
Missing setContentView (R.layout.activity_main); these words
You can not find the controls inside
Reply:
So many points! Tyrant, we be friends

Reply:
will establish the main view of the current window after setContentView, before fetching view is certainly wrong,
Reply:
setContentView (R.layout.activity_main); layout file is loaded, to get the current window view
Components are defined in the layout of the document, only the first load layout file, to find the controls, so you want to get after the component.
In addition, the control is loaded wrong format:
There are two ways:
1, the first definition, after obtaining components.
private RatingBar ratingbar;
private TextView textview;
ratingbar = (RatingBar) findViewById (R.id.mybar1);
textview = (TextView) findViewById (R.id.mytext);
Note: Do not null, this, super
2, component definition and access simultaneously
RatingBar ratingbar = (RatingBar) findViewById (R.id.mybar1);
TextView textview = (TextView) findViewById (R.id.mytext);
Reply:
Thank God help large
No comments:
Post a Comment