主要活动
@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>
这是log cat
01-10 00:40:49.897: E/AndroidRuntime(1030): FATAL EXCEPTION: main
01-10 00:40:49.897: E/AndroidRuntime(1030): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.multiplesystem.nosms/net.multiplesystem.nosms.Sign_In}: java.lang.NullPointerException
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.access$600(ActivityThread.java:141)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.os.Handler.dispatchMessage(Handler.java:99)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.os.Looper.loop(Looper.java:137)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.main(ActivityThread.java:5103)
01-10 00:40:49.897: E/AndroidRuntime(1030): at java.lang.reflect.Method.invokeNative(Native Method)
01-10 00:40:49.897: E/AndroidRuntime(1030): at java.lang.reflect.Method.invoke(Method.java:525)
01-10 00:40:49.897: E/AndroidRuntime(1030): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
01-10 00:40:49.897: E/AndroidRuntime(1030): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
01-10 00:40:49.897: E/AndroidRuntime(1030): at dalvik.system.NativeStart.main(Native Method)
01-10 00:40:49.897: E/AndroidRuntime(1030): Caused by: java.lang.NullPointerException
01-10 00:40:49.897: E/AndroidRuntime(1030): at net.multiplesystem.nosms.Sign_In.onCreate(Sign_In.java:18)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.Activity.performCreate(Activity.java:5133)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-10 00:40:49.897: E/AndroidRuntime(1030): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
01-10 00:40:49.897: E/AndroidRuntime(1030): ... 11 more
当我点击主要活动的登录按钮时。它显示空指针异常。已经尝试了很多,但无法解决问题。请告诉我我错在哪里。提前完成。
答案 0 :(得分:3)
Button blogin=(Button) findViewById(R.id.bRegister);
我猜你引用了错误的id按钮。
您需要在sign_in.xml
中检查按钮的ID。确保sign_in.xml
中有按钮,并确保在java代码中引用时id匹配
我想它应该是R.id.bLogin
编辑:
你拥有的是
<Button
android:id="@+id/button1"
所以改变
Button blogin=(Button) findViewById(R.id.bRegister);
到
Button bLogin = (Button)findViewById(R.id.button1);
此外,您已嵌套LinearLayout
,但没有错,您可以拥有一个LinearLayout
答案 1 :(得分:1)
试试这个..
更改
Button blogin=(Button) findViewById(R.id.blogin);
到
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);
}
});
答案 2 :(得分:0)
Intent intent1 = new Intent(getApplicationContext(),Sign_In.class);
startActivity(intent1);