Приложение для Android перестает работать после нажатия кнопки

Привет, ребята, я создаю приложение для Android (я очень новичок в этом), и я пытаюсь получить доступ к новому окну активности после того, как я нажимаю кнопку входа, но как только я нажимаю ее, появляется окно, которое говорит что-то вроде «к сожалению, «имя приложения» остановлено» и выходит из программы. вот мой код, вы видите, что с ним не так? Спасибо, парни

Это файл Login Class.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;
    }
    }
}
}

Вот основной экран активности, который я хочу открыть после нажатия кнопки входа.

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;
}

}

вот файл манифеста андроида, если это необходимо

<?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>


person user2014869    schedule 27.01.2013    source источник
comment
Прикрепите трассировку стека LogCat.   -  person A--C    schedule 27.01.2013
comment
Что вы пробовали? Вероятно, где-то работает нулевой указатель.   -  person Sinkingpoint    schedule 27.01.2013
comment
Где находится ваш объект-кнопка, который вы пытаетесь надуть?   -  person Jade Byfield    schedule 27.01.2013
comment
@user2014869 user2014869 Проверьте опубликованное мной решение, которое решит вашу проблему.   -  person Pratik Sharma    schedule 27.01.2013


Ответы (2)


Измените onCreate вашей активности LoginScreen с этого

@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;

}

person Jade Byfield    schedule 27.01.2013

В вашей LoginScreen деятельности у вас есть

setContentView(R.layout.activity_login_screen);

И в вашей MainScreen деятельности у вас есть

setContentView(R.layout.activity_login_screen);

Для MainScreen должен быть другой макет.

person Pratik Sharma    schedule 27.01.2013