向按钮添加onclick监听器可阻止应用程序正常工作

时间:2014-11-08 06:45:48

标签: android eclipse navigation onclick onclicklistener

我已经尝试了很长时间了。每当我使用setonclicklistener时,应用程序永远不会打开。在开始之前崩溃。通过引用Android开发人员添加到按钮的Onclick方法只是启动应用程序。但是点击按钮后它会崩溃。

这是我的MainActivity.java:     package com.example.tyro;

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

public class MainActivity extends Activity {

 public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
Button button;

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

 // Locate the button in activity_main.xml
    button = (Button) findViewById(R.id.loginbutton);

    // Capture button clicks
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {

            // Start NewActivity.class
            Intent myIntent = new Intent(MainActivity.this,ShowMainPage.class);
            startActivity(myIntent);
        }
    });

}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.

    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.main, menu);
    return super.onCreateOptionsMenu(menu);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

  }

这是我的showmainpage类

       package com.example.tyro;

import android.support.v7.app.ActionBarActivity;
 import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

public class ShowMainPage extends ActionBarActivity {

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

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.show_main_page, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}
}

这是我的login.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/tyro_login"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingTop="50dp"
tools:context="com.example.tyro.MainActivity" >

<TextView
    android:id="@+id/login_intro"
    android:layout_width="104dp"
    android:layout_height="wrap_content"
    android:text="Welcome"
    android:textColor="#ffffff"
    android:textSize="20sp"
    android:textStyle="bold" />

<TextView
    android:id="@+id/login_text"
    android:layout_width="327dp"
    android:layout_height="wrap_content"
 android:text="Please enter your username and password "
    android:textColor="#ffffff"
    android:textSize="20sp" />

<EditText
    android:id="@+id/login_user"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="30dp"
    android:ems="10"
    android:hint="Enter your username"
    android:inputType="textPersonName"
    android:textColor="#ffffff"
    android:textColorHint="#ffffff" />

<EditText
    android:id="@+id/login_pass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:ems="10"
    android:hint="Enter your password"
    android:inputType="textPersonName"
    android:paddingTop="10dp"
    android:textColor="#ffffff"
    android:textColorHint="#ffffff" />

<TextView
    android:id="@+id/forgot_pass"
    android:layout_width="267dp"
    android:layout_height="wrap_content"
    android:text="Forgot your username/password?"
    android:textColor="#ffffff"
    android:textSize="12sp" />

<Button
    android:id="@+id/loginbutton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="20dp"
    android:background="#00000000"
    android:text="LOGIN"



    >
</Button>

<TextView
    android:id="@+id/signup_text"
    android:layout_width="300dp"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="50dp"
    android:gravity="center"
    android:text="Don't have an account?Click here to sign up"
    android:textColor="#ffffff"
    android:textSize="12sp" />

<ImageButton
    android:id="@+id/signup_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:layout_marginTop="10dp"
    android:background="#00000000"
    android:src="@drawable/signup_button" >
</ImageButton>

</LinearLayout>

这是logcat:

11-07 23:30:15.167: E/AndroidRuntime(16904): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.tyro/com.example.tyro.ShowMainPage}: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.os.Handler.dispatchMessage(Handler.java:102)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.os.Looper.loop(Looper.java:136)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.ActivityThread.main(ActivityThread.java:5001)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at java.lang.reflect.Method.invokeNative(Native Method)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at java.lang.reflect.Method.invoke(Method.java:515)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at dalvik.system.NativeStart.main(Native Method)
11-07 23:30:15.167: E/AndroidRuntime(16904): Caused by: java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.support.v7.app.ActionBarActivityDelegate.onCreate(ActionBarActivityDelegate.java:147)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.support.v7.app.ActionBarActivityDelegateBase.onCreate(ActionBarActivityDelegateBase.java:139)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.support.v7.app.ActionBarActivity.onCreate(ActionBarActivity.java:123)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at com.example.tyro.ShowMainPage.onCreate(ShowMainPage.java:12)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.Activity.performCreate(Activity.java:5231)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
11-07 23:30:15.167: E/AndroidRuntime(16904):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148)
11-07 23:30:15.167: E/AndroidRuntime(16904):    ... 11 more

4 个答案:

答案 0 :(得分:0)

您的login_user是EditText

<EditText
android:id="@+id/login_user"

您无法将其投射到Button。在你的LogCat

java.lang.ClassCastException: android.widget.EditText cannot be cast to android.widget.Button

更新:使用

public class ShowMainPage extends Activity {//..

答案 1 :(得分:0)

您是否在AndroidManifest.xml中声明了ShowMainPage? 当您的应用崩溃时,您可以在logcat中找到详细原因。

答案 2 :(得分:0)

这是EditText

 <EditText
 android:id="@+id/login_user"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginTop="30dp"
 android:ems="10"
 android:hint="Enter your username"
 android:inputType="textPersonName"
 android:textColor="#ffffff"
 android:textColorHint="#ffffff" />

我们使用这个

 button = (Button) findViewById(R.id.login_user);
 this is button not edittext so this regian u geting error

检查编辑文本到xml文件中的按钮

 <Button
    android:id="@+id/login_user"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Button" />

答案 3 :(得分:0)

必须将ActionBarActivity扩展为Activity并开始工作