每当我创建一个新的按钮,我的应用程序强制关闭 [英] Whenever I create a new button my app force closes
问题描述
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RelativeLayout;
public class register_activity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
RelativeLayout RLayout = (RelativeLayout) View.inflate(this, R.layout.register, null);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
Button btnCreateNew = new Button(this);
btnCreateNew.setText("Create New User");
btnCreateNew.offsetTopAndBottom(10);
btnCreateNew.offsetLeftAndRight(10);
RLayout.addView(btnCreateNew, p);
}
}
这样code ++工程,并运行得很好,只是我看不到显示的按钮。布局是简单地里面什么都没有空白。
So that code works and runs just fine, only I can not see the button being displayed. The layout is simply blank with nothing inside it.
什么是错的?
推荐答案
您有一个NullPointerException异常,对不对? (检查你的LogCat中!)
You got a NullPointerException, right? (Check your LogCat!)
究其原因:
-
您创建一个Button与
这个
作为参数的成员变量。这可能会导致一些麻烦的,因为这个
在新按钮(这个)
被称为可能无法确定。移动初始化成的onCreate
You create a Button as a member variable with
this
as a parameter. That might cause some trouble asthis
might not be defined whennew Button(this)
is called. Move the initialization intoonCreate
您 RLayout
将是空的,在这里我非常肯定。其原因是,你不能叫 findViewById()
你调用之前的setContentView()
。如果你之前调用它,Android有不知道从哪里看,并返回null。
Your RLayout
will be null, and here I am very sure. The reason is, that you can't call findViewById()
before you called setContentView()
. If you call it before, Android has no idea where to look at and returns null.
的更新的
当你改变了你的问题相当多,这是我更新的答案:
As you changed your question quite a bit, here is my updated answer:
您设置你的内容 R.layout.register
,之后您再次膨胀了。
You set your content to R.layout.register
and after that you inflate it again.
我对你的解决方案:只需使用的setContentView(R.layout.register)
,不是使用 findViewById(R.id.layout_id)
并最终创建并添加按钮:
My solution for you: just use setContentView(R.layout.register)
, than use findViewById(R.id.layout_id)
and finally create and add your button:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
RelativeLayout RLayout = (RelativeLayout) findViewById(R.id.layout_id);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT
);
Button btnCreateNew = new Button(this);
btnCreateNew.setText("Create New User");
btnCreateNew.offsetTopAndBottom(10);
btnCreateNew.offsetLeftAndRight(10);
RLayout.addView(btnCreateNew, p);
}
当然,你也可以在XML布局直接添加按钮。我想preFER这种方式,因为你的布局和code之间较好的分离。
Of course you can also add the button in the xml layout directly. I would prefer this way, because you have a better separation between layout and code.
基本的XML结构(它的风格,只要你想):
Basic XML structure (style it to as you want):
<RelativeLayout android:id="@+id/layout_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button android:id="@+id/button_id"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="@string/button_text"/>
</RelativeLayout>
的理由让你黑屏了,您添加的按钮,将新创建 RLayout
,但这种布局是从来没有你的屏幕(从未被<$ C补充的一部分$ C>的setContentView())
The reason for you blank screen was, that you added the button to the new created RLayout
but this layout was never part of your screen (never added by setContentView()
)
这篇关于每当我创建一个新的按钮,我的应用程序强制关闭的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!