findViewById(R.id.myOneButton)为什么会返回null ?
新手学android,求教高手指点一下,十分感觉。java代码如下:publicclassMainActivityextendsActionBarActivity{pro...
新手学android,求教高手指点一下,十分感觉。
java代码如下:
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = (Button)findViewById(R.id.myOneButton); //为什么这里是空值异常?
myButton.setText("我的第一个按钮");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
}
R.java如下:
public static final class id {
public static final int myOneButton=0x7f05003e;
public static final int myOneTextView=0x7f05003d;
}
mian.xml如下:(注意这里是RelativeLayout )
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.myandroidgame.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/myOneButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/myOneTextView"/>
</RelativeLayout> 展开
java代码如下:
public class MainActivity extends ActionBarActivity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button myButton = (Button)findViewById(R.id.myOneButton); //为什么这里是空值异常?
myButton.setText("我的第一个按钮");
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
}
R.java如下:
public static final class id {
public static final int myOneButton=0x7f05003e;
public static final int myOneTextView=0x7f05003d;
}
mian.xml如下:(注意这里是RelativeLayout )
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.myandroidgame.MainActivity$PlaceholderFragment" >
<Button
android:id="@+id/myOneButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/myOneTextView"/>
</RelativeLayout> 展开
若以下回答无法解决问题,邀请你更新回答
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询