单击按钮后 Android 应用程序停止工作

标签 android button onclicklistener

大家好,我正在创建一个 Android 应用程序(我对此很陌生),我试图在点击登录按钮后访问一个新的 Activity 窗口,但是一旦我点击它,一个窗口弹出类似“不幸的是“appname”已停止”之类的内容并退出程序。这是我的代码,你能看出它有什么问题吗?谢谢大家

这是登录类.java文件

package com.example.smartsignoutv3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class LoginScreen extends Activity implements OnClickListener {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_screen);

    ((Button)findViewById(R.id.sign_in_button)).setOnClickListener(this);
}

@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_login_screen, menu);
    return true;
  }
@Override
public void onClick(View arg0) {
    switch (arg0.getId()) {
    case (R.id.sign_in_button): {
        System.out.println("yo dawg");
        Intent i = new Intent(this, MainScreen.class);
        startActivity(i);
        return;
    }
    }
}
}

这是单击登录按钮后我想打开的主 Activity 屏幕

package com.example.smartsignoutv3;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;



public class MainScreen extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private static final int SWIPE_THRESHOLD_VELOCITY = 200;
private GestureDetector gestureDetector;

@SuppressWarnings("deprecation")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_login_screen);

    gestureDetector = new GestureDetector(new MyGestureDetector());
    View mainview = (View) findViewById(R.id.welcome_screen);

    // Set the touch listener for the main view to be our custom gesture listener
    mainview.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    });
}

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    Intent intent = new Intent(MainScreen.this.getBaseContext(), MainScreen.class);

        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH) {
            return false;
        }

        // right to left swipe
        if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        startActivity(intent);
        MainScreen.this.overridePendingTransition(
        R.anim.slide_in_right,
        R.anim.slide_out_left
        );
        // right to left swipe
        }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
        startActivity(intent);
        MainScreen.this.overridePendingTransition(
        R.anim.slide_in_left, 
        R.anim.slide_out_right
        );
        }

        return false;
    }

    // It is necessary to return true from onDown for the onFling event to register
    @Override
    public boolean onDown(MotionEvent e) {
            return true;
    }
}

@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_login_screen, menu);
    return true;
}

如果需要,这里是 android list 文件

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.smartsignoutv3"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="17" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name="com.example.smartsignoutv3.LoginScreen"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name="com.example.smartsignoutv3.MainScreen"
        android:label="@string/app_name">

    </activity>
</application>

最佳答案

从此更改您的 LoginScreen Activity 的 onCreate

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);

((Button)findViewById(R.id.sign_in_button)).setOnClickListener(this);
}

为此

private Button button;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login_screen);

   button = (Button)findViewById(R.id.sign_in_button).setOnClickListener(this);
}

然后在你的 onClick 中直接执行此操作

@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.sign_in_button: 
    System.out.println("yo dawg");
    Intent i = new Intent(this, MainScreen.class);
    startActivity(i);

 break;


}

  return true;

关于单击按钮后 Android 应用程序停止工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14544126/

相关文章:

android - 为什么我的 BroadCast 接收器不工作?

java - 在Android应用程序包名称中使用特殊字符

android - EK-GC200 上的硬件相机快门/拍摄按钮三星 Galaxy 相机

android - 在 RealmMigration 中创建对象并将其添加到 RealmList

java - 等待用户输入继续

HTML/CSS - 按钮 - 何时使用什么

android - 如何将整个对象传递给 ExpandableListView 中的 onChildClick 中的 Intent

android - new OnClickListener(){} 类型的 onClick(View) 方法必须覆盖或实现父类(super class)型方法

android - AlertDialog - DialogInterface onClick 或 ListView onClick 未在项目单击时被调用

使用 ProGuard 的 Android Export 应用程序