简体   繁体   English

新活动未在按钮上启动单击[Android]

[英]New Activity not Starting on Button Click [Android]

I'm making an android app for a class, and I've gotten an okay start so far, but once the app starts up, and you click on the screen, it either does nothing while the button is invisible, or it crashes the app while the button is visible. 我正在为一个班级制作一个android应用程序,到目前为止我已经可以顺利开始,但是一旦该应用程序启动,并且您在屏幕上单击,它在按钮不可见时要么不起作用,要么崩溃按钮可见时的应用程序。 It should move to a new activity. 它应该转到新的活动。 (login) (登录)

The error is this 错误是这样

02-25 18:02:54.250: E/AndroidRuntime(25252): FATAL EXCEPTION: main
02-25 18:02:54.250: E/AndroidRuntime(25252): java.lang.IllegalStateException: Could not find a method onClick (View v)(View) in the activity class jr.crfbla.etn.main for onClick handler on view class android.widget.Button with id 'button1'
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.view.View$1.onClick(View.java:3839)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.view.View.performClick(View.java:4489)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.view.View$PerformClick.run(View.java:18803)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.os.Handler.handleCallback(Handler.java:730)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.os.Handler.dispatchMessage(Handler.java:92)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.os.Looper.loop(Looper.java:137)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.app.ActivityThread.main(ActivityThread.java:5455)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at java.lang.reflect.Method.invokeNative(Native Method)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at java.lang.reflect.Method.invoke(Method.java:525)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1187)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1003)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at dalvik.system.NativeStart.main(Native Method)
02-25 18:02:54.250: E/AndroidRuntime(25252): Caused by: java.lang.NoSuchMethodException: onClick (View v) [class android.view.View]
02-25 18:02:54.250: E/AndroidRuntime(25252):    at java.lang.Class.getConstructorOrMethod(Class.java:423)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at java.lang.Class.getMethod(Class.java:787)
02-25 18:02:54.250: E/AndroidRuntime(25252):    at android.view.View$1.onClick(View.java:3832)

Exploration To Nashville Manifest 纳什维尔清单探索

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="jr.crfbla.etn"
    android:versionCode="1"
    android:versionName="1.0" >

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:screenOrientation="landscape"
        android:theme="@style/NoActionBar" >


        <activity 
            android:name="main"
            android:label="@string/app_name"
            android:theme="@style/NoActionBar"
            android:screenOrientation="landscape"
            >
            <intent-filter>
                <action android:name="android.intent.action.MAIN"/>
                <category android:name="android.intent.category.LAUNCHER"/>
            </intent-filter>
        </activity>

        <activity
           android:name="login"
           android:label="@string/app_name"
           android:screenOrientation="landscape"
           android:theme="@style/NoActionBar"
           android:parentActivityName="main" >
           <meta-data
               android:name="android.support.PARENT_ACTIVITY"
               android:value="main" />         
         </activity>

    </application>

</manifest>

Home.xml Home.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_gravity="bottom"
    android:background="@drawable/home_page"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:screenOrientation="landscape"
    tools:context=".main" >

    <Button
        android:id="@+id/button1"
        style="?android:attr/buttonStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:focusableInTouchMode="true"
        android:longClickable="false"
        android:onClick="onClick (View v)"
        android:paddingLeft="96dp"
        android:text="@+string/Blank"
        android:visibility="invisible" />

</RelativeLayout>

main.java main.java

package jr.crfbla.etn;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;

public class main extends Activity {
    public final static String EXTRA_MESSAGE = "com.example.myfirstapp.MESSAGE";
    /** Called when the activity is first created/ */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.home);
    }


/** Called when the user clicks the Start button */ 

    // Do something in response to button
      public void onClick (View button1) {
        Intent loginintent = new Intent(main.this, login.class);
            main.this.startActivity(loginintent);
            } 

}

login.xml login.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/log_in_page"
    android:configChanges="keyboardHidden|orientation|screenSize"
    android:screenOrientation="landscape"
    tools:context="login" >

</RelativeLayout>

login.java
package jr.crfbla.etn;

import android.app.Activity;
import android.os.Bundle;

public class login extends Activity {
    /** Called when the activity is first created/ */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);
    }
}

Replace 更换

android:onClick="onClick (View v)"

with

android:onClick="onClick"

Your Activity class should implement the View.OnClickListener interface, and then override the corresponding onClick method. 您的Activity类应实现View.OnClickListener接口,然后重写相应的onClick方法。 As others have pointed out, your onClick declaration in the layout XML is also wrong and it must read "onClick" to match the overridden onClick method. 正如其他人指出的那样,布局XML中的onClick声明也是错误的,它必须读为“ onClick”以匹配重写的onClick方法。

Try the following instructions: 请尝试以下说明:

//inside your layout file
<Button 
    android:id="@+id/mButton"
    android:text="Start"
    android:background="#ffffff" />

Now, inside your activity try this: 现在,在您的活动中尝试以下操作:

public class MyActivity extends Activity implements View.OnClickListener{

   Button mButton;

   @Override
   public void onCreate(Bundle savedInstanceState){
      super.onCreate(savedInstanceState);

      setContentView(R.layout.main.xml);

      mButton = (Button)findViewById(R.id.mButton);

      mButton.setOnClickListener(this);
   }

   @Override
   public void onClick(View v){
      switch(v.getId()){
         case R.id.mButton:

              Intent intent = new Intent(this, SecondActivity.class);
              startActivity(intent);
              break;
        default:
             //nothing happened 
      }
   }
}

This way of handling click events makes sure that you can write minimal lines because you just need to set the OnClickListener - which is one line for each button and use a switch statement to check which button was clicked. 这种处理单击事件的方法可确保您可以写最少的行,因为您只需要设置OnClickListener-每个按钮一行,然后使用switch语句检查单击了哪个按钮。 If you had more activities, you can define an intent once then instantiate them inside each button depending on which button was clicked 如果您有更多的活动,则可以一次定义一个意图,然后根据单击的哪个按钮在每个按钮中实例化它们

Good luck! 祝好运!

also you don't need to use a very long way of calling an activity especially if its being called inside an activity. 同样,您也不需要使用很长的调用活动的方式,尤其是在活动内部调用活动时。 just use 只是使用

startActivity(loginintent);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM