我可以看到异常发生在" rtb.setRating(8.0f);" 此外,我能够在xml文件中手动设置评级,并产生预期的结果,例如机器人:评级=" 3.7"
static String ingredients[] = {"Ingredient1","Ingredient2","Ingredient3","Ingredient4","Ingredient5","Ingredient6","Ingredient7","Ingredient8","Ingredient9","Ingredient10","Ingredient11","Ingredient12","Ingredient13","Ingredient14","Ingredient15","Ingredient16","Ingredient17","Ingredient18","Ingredient19","Ingredient20"};
static String steps[] = {"Step1","Step2","Step3","Step4","Step5","Step6","Step7","Step8","Step9","Step10"};
String url = "http://192.168.1.2/cricketjson.txt";
private ListView mDrawerList;
private ArrayAdapter<String> mAdapter;
static int[] ids = {1,2,3,4,5};
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {@link #restoreActionBar()}.
*/
private CharSequence mTitle;
private CharSequence mStep;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RatingBar rtb = (RatingBar) findViewById(R.id.rb);
rtb.setRating(8.0f);
setContentView(R.layout.activity_recipe);
mDrawerList = (ListView)findViewById(R.id.navList);
//set visual features
//set string values
TextView[] txtStep = {(TextView) findViewById(R.id.txtStep1), (TextView) findViewById(R.id.txtStep2)};
//txtStep1.setText("ftUr");
//set font Title
TextView mRecipeTitle = (TextView) findViewById(R.id.Title);
Typeface face = Typeface.createFromAsset(getAssets(),
"Italianno-Regular.ttf");
mRecipeTitle.setTypeface(face);
mRecipeTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 77);
// set scrollview as transparent
findViewById(R.id.recipScrollView).setBackgroundColor(getResources().getColor(android.R.color.transparent));
// PD = new ProgressDialog(this);
// PD.setMessage("Loading.....");
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(RecipeActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
}
});
//here*#8*#* put in the steps to the func.jsonrq
// Funcs.makejsonobjreq(ingredients, url, this,steps,id);
Funcs.makejsonobjreq(ingredients, url, this);
Funcs.makejsonobjreqrecipe(ingredients, url, this, steps, ids[0], txtStep);
}
@Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, PlaceholderFragment.newInstance(position + 1))
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_Recipe);
break;
case 2:
mTitle = ingredients[0];
break;
case 3:
mTitle = ingredients[1];
break;
case 4:
mTitle = ingredients[2];
break;
case 5:
mTitle = ingredients[3];
break;
case 6:
mTitle = ingredients[4];
break;
case 7:
mTitle = ingredients[5];
break;
case 8:
mTitle = ingredients[6];
break;
case 9:
mTitle = ingredients[7];
break;
case 10:
mTitle = ingredients[8];
break;
case 11:
mTitle = ingredients[9];
break;
case 12:
mTitle = ingredients[10];
break;
case 13:
mTitle = ingredients[11];
break;
case 14:
mTitle = ingredients[12];
break;
case 15:
mTitle = ingredients[13];
break;
case 16:
mTitle = ingredients[14];
break;
case 17:
mTitle = ingredients[15];
break;
case 18:
mTitle = ingredients[16];
break;
case 19:
mTitle = ingredients[17];
break;
case 20:
mTitle = ingredients[18];
break;
case 21:
mTitle = ingredients[19];
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.recipe, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_recipe, container, false);
return rootView;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((RecipeActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
和同等的XML:
<!-- As the main content view, the view below consumes the entire
space available using match_parent in both dimensions. -->
<FrameLayout android:id="@+id/container" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:background="@drawable/background8"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="20dip"
android:layout_gravity="right|center_vertical"
android:weightSum="1">
<TextView
android:layout_marginTop="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/Creator"
android:layout_alignLeft="@id/title"
android:textSize="20dp"
android:layout_gravity="center_horizontal" />
<TextView
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="@+id/Title"
android:layout_below="@id/Creator"
android:layout_gravity="center_horizontal" />
<ScrollView android:layout_width="match_parent"
android:layout_marginTop="0dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_height="198dp"
android:background="#ffffff"
android:id="@+id/recipScrollView">
<LinearLayout
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dip"
android:weightSum="1">
<TextView
android:layout_marginTop="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Txt"
android:id="@+id/txtStep1" />
<TextView
android:layout_marginTop="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Txt"
android:id="@+id/txtStep2"
android:layout_below="@id/txtStep1"/>/>
</LinearLayout>
</ScrollView>
<RatingBar
android:layout_marginTop="30dp"
android:layout_marginLeft="30dp"
android:id="@+id/rb"
style="?android:attr/ratingBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:max="10"
android:numStars="10"
android:rating="3.7"
android:stepSize="0.0" />
</LinearLayout>
</FrameLayout>
<!-- android:layout_gravity="start" tells DrawerLayout to treat
this as a sliding drawer on the left side for left-to-right
languages and on the right side for right-to-left languages.
If you're not building against API 17 or higher, use
android:layout_gravity="left" instead. -->
<!-- The drawer is given a fixed width in dp and extends the full height of
the container. -->
<fragment android:id="@+id/navigation_drawer"
android:layout_width="@dimen/navigation_drawer_width" android:layout_height="match_parent"
android:layout_gravity="start" android:name="com.example.finnb.rcp.NavigationDrawerFragment"
tools:layout="@layout/fragment_navigation_drawer" />
<ListView
android:id="@+id/navList"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left|start"
android:background="#ffeeeeee"/>
LogCat错误:
03-08 04:52:30.350 1946-1946/com.example.finnb.rcp D/dalvikvm﹕ Not late-enabling CheckJNI (already on)
03-08 04:52:30.460 1946-1946/com.example.finnb.rcp D/AndroidRuntime﹕ Shutting down VM
03-08 04:52:30.460 1946-1946/com.example.finnb.rcp W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0xb0ca2b20)
03-08 04:52:30.460 1946-1946/com.example.finnb.rcp E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.finnb.rcp, PID: 1946
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.finnb.rcp/com.example.finnb.rcp.RecipeActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.finnb.rcp.RecipeActivity.onCreate(RecipeActivity.java:62)
at android.app.Activity.performCreate(Activity.java:5231)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
at android.app.ActivityThread.access$800(ActivityThread.java:135)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5017)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我相信这是因为您在设置RatingBar后设置了内容视图。尝试移动
setContentView(R.layout.activity_recipe);
之前
RatingBar rtb = (RatingBar) findViewById(R.id.rb);
rtb.setRating(8.0f
答案 1 :(得分:1)
setContentView(R.layout.activity_recipe);
此行应位于此行super.onCreate(savedInstanceState);
下方,之后您应该访问/实例化视图。
您在链接xml布局之前尝试访问评级栏。
答案 2 :(得分:0)
这很简单。假设您在setContentView中使用了正确的布局:
以下是正确的初始化顺序:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_recipe);
RatingBar rtb = (RatingBar) findViewById(R.id.rb);
rtb.setRating(8.0f);
如果通过setContentView()设置布局,则可以引用评级栏,而不是之前...