赞
踩
android开发中Caused by: java.lang.NullPointerException
main.xml文
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);//获取组件
this.textview=(TextView)super.findViewById(R.id.mytext);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.ratingbar.setOnRatingBarChangeListener(new OnRatingBarChangeListener1());//设置监听
}
private class OnRatingBarChangeListener1 implements OnRatingBarChangeListener{
//@Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromUser) {
// 在显示文本中显示星星
MainActivity.this.textview.append("***当前的星评分:"
+ratingBar.getNumStars()+"***当前的步长:"+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;
}
}
错误。。。。。。。。。。。。。。。。
android开发
分享到:
------解决方案--------------------
像这样
setContentView(R.layout.activity_main);
this.ratingbar=(RatingBar)super.findViewById(R.id.mybar1);//获取组件
this.textview=(TextView)super.findViewById(R.id.mytext);
------解决方案--------------------
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.ratingbar=(RatingBar)findViewById(R.id.mybar1);//获取组件
this.textview=(TextView)findViewById(R.id.mytext);
------解决方案--------------------
顺序oncreate-setContentView-findView-setView
------解决方案--------------------
没有加载布局,
缺少 setContentView(R.layout.activity_main); 这句话
就不能找到里面的控件
------解决方案--------------------
setContentView后才会建立当前窗口的主view,之前获取view肯定是错的、
------解决方案--------------------
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。