import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
public class MainActivity extends Activity {
DataHandlerSignIn handler1;
ImageButton signin;
EditText etxt1,etxt2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
etxt1=(EditText) findViewById(R.id.user);
etxt2=(EditText) findViewById(R.id.pass);
signin=(ImageButton) findViewById(R.id.signup);
signin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
Intent ii = new Intent(MainActivity.this,SignUp.class);
startActivity(ii);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
堆栈跟踪是
E/AndroidRuntime(1644): FATAL EXCEPTION: main
E/AndroidRuntime(1644): java.lang.RuntimeException: Unable to start activity ComponentInfo{simple.smile.my_dairy/simple.smile.my_dairy.SignUp}: java.lang.NullPointerException
E/AndroidRuntime(1644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
E/AndroidRuntime(1644): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
E/AndroidRuntime(1644): at android.app.ActivityThread.access$600(ActivityThread.java:141)
E/AndroidRuntime(1644): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
E/AndroidRuntime(1644): at android.os.Handler.dispatchMessage(Handler.java:99)
E/AndroidRuntime(1644): at android.os.Looper.loop(Looper.java:137)
E/AndroidRuntime(1644): at android.app.ActivityThread.main(ActivityThread.java:5039)
E/AndroidRuntime(1644): at java.lang.reflect.Method.invokeNative(Native Method)
E/AndroidRuntime(1644): at java.lang.reflect.Method.invoke(Method.java:511)
E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
E/AndroidRuntime(1644): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
E/AndroidRuntime(1644): at dalvik.system.NativeStart.main(Native Method)
E/AndroidRuntime(1644): Caused by: java.lang.NullPointerException
E/AndroidRuntime(1644): at simple.smile.my_dairy.SignUp.onCreate(SignUp.java:30)
E/AndroidRuntime(1644): at android.app.Activity.performCreate(Activity.java:5104)
E/AndroidRuntime(1644): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
E/AndroidRuntime(1644): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
E/AndroidRuntime(1644): ... 11 more
SignUp活动代码
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.Toast;
public class SignUp extends Activity {
ImageButton click;
DataHandlerSignIn handler;
EditText et1,et2,et3,et4,et5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up);
et1 = (EditText) findViewById(R.id.name12);
et2 = (EditText) findViewById(R.id.age12);
et3 = (EditText) findViewById(R.id.date12);
et4 = (EditText) findViewById(R.id.passed12);
et5 = (EditText) findViewById(R.id.repassed12);
click.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
String name1=et1.getText().toString();
String age1=et2.getText().toString();
String dob1=et3.getText().toString();
String password1=et4.getText().toString();
String password2=et5.getText().toString();
if(password1.equals(password2))
{
handler = new DataHandlerSignIn(getBaseContext());
handler.open();
long id = handler.insertData(name1, age1, dob1, password1);
Toast.makeText(getBaseContext(), "Saved",1000).show();
handler.close();
Intent hello = new Intent(SignUp.this,MainActivity.class);
startActivity(hello);
}
else
{
Toast.makeText(getBaseContext(),"passwords donot match plzz try again",3000).show();
et1.setText("");
et2.setText("");
et3.setText("");
et4.setText("");
et5.setText("");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_sign_up, menu);
return true;
}
}
我尝试使用意图从一个页面转到另一个页面。但我没有成功。我想我已经使用了正确的语法。但为什么会发生错误。有人请告诉我它中的错误以及如何解决它。 提前谢谢。
答案 0 :(得分:3)
您永远不会初始化ImageButton
:
click.setOnClickListener(new OnClickListener()...
// click is null, calling a method on it will cause a crash
您需要初始化上面的ImageButton
click
:
click = (ImageButton)findViewById(R.id.click);
click.setOnClickListener(new OnClickListener()...
答案 1 :(得分:2)
为click
活动初始化SignUp
imageButton。
click =(ImageButton) findViewById(R.id.click);
on onCreate()