【问题标题】:adding onclick listener to button stops app from working将 onclick 侦听器添加到按钮会阻止应用程序工作
【发布时间】:2014-11-08 06:45:47
【问题描述】:

我已经尝试了很长时间了。每当我使用 setonclicklistener 时,应用程序永远不会打开。启动前崩溃。通过引用android开发者在按钮上添加的onclick方法只是让应用程序启动。但是点击按钮后会崩溃。

这是我的 MainActivity.java: 包 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>

这是日志猫:

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

【问题讨论】:

  • 请发布你的日志
  • 你在 AndroidManifest.xml 中声明 ShowMainPage 了吗?

标签: android eclipse navigation onclick onclicklistener


【解决方案1】:

您的 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 {//..

【讨论】:

  • 把它改成了 loginbutton.. 还是一样的结果
  • 将其更改为按钮...出现第一个屏幕...点击后崩溃...logcat 中的第一个错误是说您需要使用 Theme.AppCombat 主题
  • 第一个错误多次并没有指出错误的确切原因。显示完整的 logcat
【解决方案2】:

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

【讨论】:

    【解决方案3】:

    这是编辑文本

     <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" />
    

    【讨论】:

      【解决方案4】:

      必须将扩展 ActionBarActivity 更改为 Activity 并且它开始工作

      【讨论】:

        猜你喜欢
        • 2011-07-20
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2018-10-13
        • 1970-01-01
        • 2014-05-27
        • 2019-05-27
        • 1970-01-01
        相关资源
        最近更新 更多