Android java.lang.RuntimeException:无法启动活动,java.lang.NullPointerException
主要活动Android java.lang.RuntimeException:无法启动活动,java.lang.NullPointerException,android,android-layout,layout,Android,Android Layout,Layout,主要活动 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.welcome_page); login=(Button) findViewById(R.id.bLogin);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_page);
login=(Button) findViewById(R.id.bLogin);
register=(Button) findViewById(R.id.bRegister);
login.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent1 = new Intent(arg0.getContext(),Sign_In.class);
startActivity(intent1);
}
});
register.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent2 = new Intent(arg0.getContext(),Register.class);
startActivity(intent2);
}
});
}
In Sign_In Activity:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sign_in);
Button blogin=(Button) findViewById(R.id.bLogin);
blogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(arg0.getContext(),MessegeBox.class);
startActivity(intent);
}
});
}
XML布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:layout_marginLeft="30dip"
android:layout_marginRight="30dip"
android:layout_marginTop="50dip"
android:background="#FFFFFF"
android:orientation="vertical" >
<EditText
android:id="@+id/phone_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:gravity="center"
android:hint="Mobile Number"
android:inputType="phone"
android:maxLength="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/phone_number"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:gravity="center"
android:hint="Invivation Pin (If Any)"
android:inputType="phone"
android:maxLength="10" />
<Button
android:id="@+id/button1"
style="@style/ButtonText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="3dip"
android:background="@drawable/blue_button"
android:text="LOGIN" />
</LinearLayout>
</LinearLayout>
当我单击主活动上的登录按钮时。它显示空指针异常。我试了很多,但没有找到解决办法。请告诉我哪里错了。提前准备好
Button blogin=(Button) findViewById(R.id.bRegister);
我猜您为button引用了错误的id
您需要检查sign_in.xml
中按钮的id。确保sign_in.xml中有按钮,并确保在java代码中引用时id匹配
我想应该是R.id.bLogin
编辑:
你所拥有的是
<Button
android:id="@+id/button1"
到
此外,您还有嵌套的LinearLayout
,尽管没有错,但您可以有一个LinearLayout
Intent intent1 = new Intent(getApplicationContext(),Sign_In.class);
startActivity(intent1);
我猜您为button引用了错误的id
您需要检查sign_in.xml
中按钮的id。确保sign_in.xml中有按钮,并确保在java代码中引用时id匹配
我想应该是R.id.bLogin
编辑:
你所拥有的是
<Button
android:id="@+id/button1"
到
此外,您还有嵌套的LinearLayout
,尽管没有错,但您可以有一个LinearLayout
试试这个
Intent intent1 = new Intent(getApplicationContext(),Sign_In.class);
startActivity(intent1);
改变
Button blogin=(Button) findViewById(R.id.blogin);
到
试试这个
改变
Button blogin=(Button) findViewById(R.id.blogin);
到
发布你的sigin类的xml这将是问题按钮blogin=(Button)findviewbyd(R.id.bRegister);使用此代码ImageButton blogin=(ImageButton)findViewById(R.id.imageButton1);使用MainActivity.thispost代替arg0.getContext(),这将是问题按钮blogin=(Button)findViewById(R.id.bRegister);使用此代码ImageButton blogin=(ImageButton)findViewById(R.id.imageButton1);使用MainActivity代替arg0.getContext()。this@user3118276postsign_in.xml
sir我已经发布了sign xml。前一个是欢迎xml布局。对不起that@user3118276postsign_in.xml
sir我已经发布了sign xml。前一个是欢迎xml布局。很抱歉,这不会导致NullPointerException
这不会导致NullPointerException
无需更改为Intent Intent=new Intent(登录此,MessegeBox.class)代码>@Raghunandan我给你这个只是为了安全。这也将给出完美的结果。argo.getContext()
@Raghunandan没有什么不安全的地方,我不是说这是错的。我是说你也可以使用它。不需要更改为Intent Intent=newintent(在.this,MessegeBox.class中签名)代码>@Raghunandan我给你这个只是为了安全。这也将给出完美的结果。argo.getContext()
@Raghunandan没有什么不安全的地方,我不是说这是错的。我是说你也可以用这个。
Button blogin=(Button) findViewById(R.id.button1);
blogin.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent(Sign_In.this,MessegeBox.class);
startActivity(intent);
}
});