
我正在做一个非常简单的登录屏幕,用户输入用户名和密码,单击登录按钮,然后输入显示在下一个屏幕上.
每次尝试更改textvIEw值时,都会收到NullPointerException.我已经搜寻了很长一段时间,却一无所获,这很简单,我只是完全想不到.
以下是我的代码:
public class LoggedIn extends Activity{ String username = ""; String password = ""; TextVIEw name; TextVIEw pwd; button infodisp; protected voID onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentVIEw(R.layout.logged_in); Intent intent = getIntent(); name = (TextVIEw)findVIEwByID(R.ID.username); pwd = (TextVIEw)findVIEwByID(R.ID.password); username = intent.getStringExtra("nameInfo"); password = intent.getStringExtra("passInfo"); name.setText(username); pwd.setText(password); }}编辑:我将包更改为上面的意图,这也是其余的代码(第一个活动)
public class LoginActivity extends Activity { button loginBtn; button registerBtn; EditText username; EditText pword; static String name; static String pass; @OverrIDe protected voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.activity_login); username = (EditText) this.findVIEwByID(R.ID.username); pword = (EditText) this.findVIEwByID(R.ID.password); loginBtn = (button) this.findVIEwByID(R.ID.loginBtn); loginBtn.setonClickListener(new OnClickListener() { public voID onClick(VIEw arg0){ Intent intent = new Intent(LoginActivity.this, LoggedIn.class); name = username.getText().toString(); pass = pword.getText().toString(); intent.putExtra("nameInfo", name); intent.putExtra("passInfo", pass); startActivity(intent); } }); }}
我没有错误地检查该值是否为null,因为这应该只是快速运行和完成的事情.我想只要在每个EditText中输入内容,字符串就不能为null,也不会有问题…对吗?
堆栈跟踪:
FATAL EXCEPTION: mainProcess: com.example.loginscreen, PID: 1677java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.LoggedIn}: java.lang.NullPointerException at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2176) at androID.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) at androID.app.ActivityThread.access0(ActivityThread.java:135) at androID.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) at androID.os.Handler.dispatchMessage(Handler.java:102) at androID.os.Looper.loop(Looper.java:137) at androID.app.ActivityThread.main(ActivityThread.java:4998) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.androID.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) at com.androID.internal.os.ZygoteInit.main(ZygoteInit.java:593) at dalvik.system.NativeStart.main(Native Method) Caused by: java.lang.NullPointerException at com.example.loginscreen.LoggedIn.onCreate(LoggedIn.java:30) at androID.app.Activity.performCreate(Activity.java:5243) at androID.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at androID.app.ActivityThread.performlaunchActivity(ActivityThread.java:2140) ... 11 moreAaaand xml文件:
activity_login:
<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=".LoginActivity" ><TextVIEw androID:ID="@+ID/textVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParentleft="true" androID:text="Login:" androID:textAppearance="?androID:attr/textAppearanceMedium" /><TextVIEw androID:ID="@+ID/textVIEw2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/textVIEw1" androID:layout_below="@+ID/textVIEw1" androID:layout_margintop="38dp" androID:text="Password:" androID:textAppearance="?androID:attr/textAppearanceMedium" /><EditText androID:ID="@+ID/username" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_above="@+ID/textVIEw2" androID:layout_alignParenttop="true" androID:layout_marginleft="20dp" androID:layout_toRightOf="@+ID/textVIEw1" androID:ems="10" /><EditText androID:ID="@+ID/password" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignBottom="@+ID/textVIEw2" androID:layout_alignParentRight="true" androID:layout_toRightOf="@+ID/textVIEw2" androID:ems="10" androID:inputType="textPassword" > <requestFocus /></EditText><button androID:ID="@+ID/loginBtn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/textVIEw2" androID:layout_below="@+ID/textVIEw2" androID:layout_margintop="26dp" androID:text="Login" /><button androID:ID="@+ID/registerBtn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignBaseline="@+ID/loginBtn" androID:layout_alignBottom="@+ID/loginBtn" androID:layout_alignleft="@+ID/textVIEw3" androID:layout_marginleft="46dp" androID:text="Register" /><CheckBox androID:ID="@+ID/remPass" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/loginBtn" androID:layout_below="@+ID/loginBtn" androID:layout_margintop="19dp" androID:text="Remember Password" /><TextVIEw androID:ID="@+ID/textVIEw3" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/remPass" androID:layout_margintop="28dp" androID:layout_toRightOf="@+ID/textVIEw1" androID:text="Forgot Password" />登录:
<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=".LoginActivity" ><TextVIEw androID:ID="@+ID/textVIEw1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignParenttop="true" androID:layout_centerHorizontal="true" androID:text="Logged In!" androID:textAppearance="?androID:attr/textAppearanceLarge" /><TextVIEw androID:ID="@+ID/textVIEw2" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_below="@+ID/textVIEw1" androID:layout_margintop="56dp" androID:text="Username: " androID:textAppearance="?androID:attr/textAppearanceLarge" /><TextVIEw androID:ID="@+ID/textVIEw3" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/textVIEw2" androID:layout_below="@+ID/textVIEw2" androID:layout_margintop="56dp" androID:text="Password: " androID:textAppearance="?androID:attr/textAppearanceLarge" /><TextVIEw androID:ID="@+ID/recUsername" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignBottom="@+ID/textVIEw2" androID:layout_marginleft="33dp" androID:layout_toRightOf="@+ID/textVIEw2" androID:editable="true" androID:text=" " androID:textAppearance="?androID:attr/textAppearanceLarge" /><TextVIEw androID:ID="@+ID/recPassword" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignBottom="@+ID/textVIEw3" androID:layout_alignRight="@+ID/textVIEw4" androID:editable="true" androID:text=" " androID:textAppearance="?androID:attr/textAppearanceLarge" /><button androID:ID="@+ID/infoBtn" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:layout_alignleft="@+ID/textVIEw1" androID:layout_alignRight="@+ID/textVIEw1" androID:layout_below="@+ID/textVIEw3" androID:layout_margintop="62dp" androID:text="Press to display Info" />解决方法:
问题是因为您发现TextVIEw的ID是错误的.
username = (TextVIEw) this.findVIEwByID(R.ID.username); pword = (TextVIEw) this.findVIEwByID(R.ID.password);至
username = (TextVIEw) this.findVIEwByID(R.ID.recUsername); pword = (TextVIEw) this.findVIEwByID(R.ID.recPassword); 总结 以上是内存溢出为你收集整理的java-无法通过意图传递字符串来更改TextView的文本全部内容,希望文章能够帮你解决java-无法通过意图传递字符串来更改TextView的文本所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)