Android Studio 按钮跳转功能的实现,以及点击后闪退问题的处理

安卓小白,课程需求要做一个APP。在做的时候,做到一个功能:点击按钮,跳转到新的界面。
教程可参考:
https://blog.csdn.net/baidu_30258569/article/details/49409145
https://blog.csdn.net/mghhz816210/article/details/50373698
https://blog.csdn.net/qq_20737519/article/details/50145687

好,我在新的界面的xml文件这样写的:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".identify">

    <TextView
    android:text="欢迎来到识别界面,在此修改"/>

</android.support.constraint.ConstraintLayout>

只有一行测试用的文字。看起来好像没什么问题。然后run,点击按钮之后闪退。报错信息如下:

05-22 14:36:41.441 22856-22856/com.example.coder_cxk.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.coder_cxk.myapplication, PID: 22856
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.coder_cxk.myapplication/com.example.coder_cxk.myapplication.identify}: android.view.InflateException: Binary XML file line #9: Binary XML file line #9: You must supply a layout_width attribute.
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2892)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3027)
        at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
        at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:101)
        at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:73)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1786)
        at android.os.Handler.dispatchMessage(Handler.java:106)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6656)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:823)

看了很久,觉得没啥问题。这个layout-width似乎也指定了。然后查了很久发现,text的宽度也要指定。于是改成这样:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".identify">

    <TextView
    android:id="@+id/test_text"
    android:layout_width="wrap_content"
    android:layout_height="73dp"
    android:text="欢迎来到识别界面,在此修改"/>

</android.support.constraint.ConstraintLayout>

可以成功跳转。

发布了141 篇原创文章 · 获赞 177 · 访问量 15万+
展开阅读全文

安卓入门,一个有关页面跳转的程序闪退

04-08

做了两个页面,通过按钮跳转。跳转时闪退。个人感觉时第二个页面的声明出了问题,但是找不出解决办法,求大神帮看看,感谢 debug 线程 [main](已暂挂(异常 ActivityNotFoundException)) Instrumentation.checkStartActivityResult(int, Object) 行: 1879 Instrumentation.execStartActivity(Context, IBinder, IBinder, Activity, Intent, int, Bundle) 行: 1546 Activity1(Activity).startActivityForResult(Intent, int, Bundle) 行: 4284 Activity1(Activity).startActivityForResult(Intent, int) 行: 4231 Activity1(Activity).startActivity(Intent, Bundle) 行: 4568 Activity1(Activity).startActivity(Intent) 行: 4536 Activity1$1.onClick(View) 行: 28 Button(View).performClick() 行: 5698 Button(TextView).performClick() 行: 10896 View$PerformClick.run() 行: 22565 Handler.handleCallback(Message) 行: 739 ViewRootImpl$ViewRootHandler(Handler).dispatchMessage(Message) 行: 95 Looper.loop() 行: 148 ActivityThread.main(String[]) 行: 7224 Method.invoke(Object, Object...) 行: 不可用 [本机方法] ZygoteInit$MethodAndArgsCaller.run() 行: 1230 ZygoteInit.main(String[]) 行: 1120 报错 04-08 08:51:19.992: E/AndroidRuntime(24556): FATAL EXCEPTION: main 04-08 08:51:19.992: E/AndroidRuntime(24556): Process: com.example.secondact, PID: 24556 04-08 08:51:19.992: E/AndroidRuntime(24556): android.content.ActivityNotFoundException: No Activity found to handle Intent { } 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1879) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1546) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.app.Activity.startActivityForResult(Activity.java:4284) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.app.Activity.startActivityForResult(Activity.java:4231) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.app.Activity.startActivity(Activity.java:4568) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.app.Activity.startActivity(Activity.java:4536) 04-08 08:51:19.992: E/AndroidRuntime(24556): at com.example.secondact.Activity1$1.onClick(Activity1.java:28) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.view.View.performClick(View.java:5698) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.widget.TextView.performClick(TextView.java:10896) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.view.View$PerformClick.run(View.java:22565) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.os.Handler.handleCallback(Handler.java:739) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.os.Handler.dispatchMessage(Handler.java:95) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.os.Looper.loop(Looper.java:148) 04-08 08:51:19.992: E/AndroidRuntime(24556): at android.app.ActivityThread.main(ActivityThread.java:7224) 04-08 08:51:19.992: E/AndroidRuntime(24556): at java.lang.reflect.Method.invoke(Native Method) 04-08 08:51:19.992: E/AndroidRuntime(24556): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) 04-08 08:51:19.992: E/AndroidRuntime(24556): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120) Manifest <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.example.secondact" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="19" android:targetSdkVersion="19" /> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <activity android:name=".Activity1" android:label="@string/app_name"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.DEFAULT"/> </intent-filter> </activity> <activity android:name=".Activity2"></activity> </application> </manifest> 第一页面代码 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Activity1 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 设置显示main.xml布局*/ setContentView(R.layout.main); /* findViewById(R.id.button1)取得布局main.xml中的button1 */ Button button = (Button) findViewById(R.id.button1); /* 监听button的事件信息*/ button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 新建一个Intent对象*/ Intent intent = new Intent(); /* 指定intent要启动的类*/ startActivity(intent); /* 关闭当前的Activity */ intent.setClass(Activity1.this, Activity02.class); /* 启动一个新的Activity */ Activity1.this.finish(); } }); } } 第二页面代码 import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Activity02 extends Activity { public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* 设置显示main2布局*/ setContentView(R.layout.main2); /* findViewById(R.id.button2)取得布局main.xml中的button2 */ Button button = (Button) findViewById(R.id.button2); /* 监听button的事件信息*/ button.setOnClickListener(new Button.OnClickListener() { public void onClick(View v) { /* 新建一个Intent对象*/ Intent intent = new Intent(); /* 指定intent要启动的类*/ intent.setClass(Activity02.this, Activity1.class); /* 启动一个新的Activity */ /* startActivity(intent); /* 关闭当前的Activity */ Activity02.this.finish(); } }); } } 问答

安卓点击按钮不能实现跳转 模拟器运行软件点按钮就闪退

06-28

点登录按钮就闪退![图片说明](https://img-ask.csdn.net/upload/201906/28/1561723660_759733.jpg) 下面是代码 ```package com.example.test; import android.app.Activity; import com.example.test.R; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class Denglu extends Activity { Button denglu,zhuche; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.denglu); denglu=(Button)findViewById(R.id.denglu); zhuche=(Button)findViewById(R.id.zhuche); denglu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent=new Intent(Denglu.this,RegisterActivity.class); startActivity(intent); } }); zhuche.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { //为了跳转到注册界面,并实现注册功能 Intent intent=new Intent(Denglu.this,LostFindActivity.class); startActivity(intent); finish(); } }); } } ``` 第二个 package com.example.test; import android.app.Activity; import com.example.test.R; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class LostFindActivity extends Activity { Button register; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.zc); register =(Button)findViewById(R.id.register); register.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(LostFindActivity.this,RegisterActivity.class); startActivity(intent); Toast.makeText(LostFindActivity.this, "注册成功,跳转中", Toast.LENGTH_LONG).show(); finish(); } }); } } 第三个 package com.example.test; import com.example.test.MainActivity; import com.example.test.R; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.Toast; public class RegisterActivity extends Activity { Button denglu; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.dl); denglu=(Button)findViewById(R.id.denglu); denglu.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(RegisterActivity.this,MainActivity.class); startActivity(intent); Toast.makeText(RegisterActivity.this, "登录成功,跳转中", Toast.LENGTH_LONG).show(); finish(); } }); } } 跳转不了 思路是先注册然后登陆 进主界面 下面是报错信息 ![图片说明](https://img-ask.csdn.net/upload/201906/28/1561724541_821567.jpg) 萌新一个 求各位大佬帮帮忙 问答

当点击登录按钮时,软件闪退。

04-15

![图片说明](https://img-ask.csdn.net/upload/201904/15/1555306213_312374.png) 当密码不正确时,正常显示。 ![图片说明](https://img-ask.csdn.net/upload/201904/15/1555306037_226545.png) 当用户名密码正确时,点击登录按钮软件闪退。 ![图片说明](https://img-ask.csdn.net/upload/201904/15/1555306388_791750.png) 再次点击软件进入,显示欢迎页面然后闪退,不进入登录注册页面。 相关代码 private void login() { //1.获取输入的用户名和密码 final String loginName = et_login_name.getText().toString(); final String loginPwd = et_login_pwd.getText().toString(); //2.校验输入的用户名和密码登录 if (TextUtils.isEmpty(loginName) || TextUtils.isEmpty(loginPwd)){ Toast.makeText(LoginActivity.this,"输入的用户名或密码不能为空",Toast.LENGTH_SHORT).show(); return; } //登录逻辑处理 Model.getInstance().getGlobalThreadPool().execute(new Runnable() { @Override public void run() { // 去环信服务器登录 EMClient.getInstance().login(loginName, loginPwd, new EMCallBack() { //登陆成功后的处理 @Override public void onSuccess() { //对模型层数据的处理 Model.getInstance().loginSucess(); //保存用户账号信息到本地数据库 Model.getInstance().getUserAccountDao().addAccount(new UserInfo(loginName)); runOnUiThread(new Runnable() { @Override public void run() { // //提示登录成功 Toast.makeText(LoginActivity.this,"登录成功",Toast.LENGTH_SHORT).show(); //跳转到主页面 Intent intent = new Intent(LoginActivity.this, MainActivity.class); startActivity(intent); finish(); } }); } //登录失败的处理 @Override public void onError(int i, final String s) { //提示登录失败 runOnUiThread(new Runnable() { @Override public void run() { Toast.makeText(LoginActivity.this,"登陆失败"+s,Toast.LENGTH_SHORT).show(); } } }); ![图片说明](https://img-ask.csdn.net/upload/201904/16/1555397258_130979.jpg) 问答

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 书香水墨 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览