简体   繁体   中英

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

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

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

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

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. 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);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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