0

私はプログラミングと Android の初心者です。

私の学校のチームと私は最初の Android アプリを開発しようとしていましたが、実際にはうまく計画できておらず、元の MainActivity クラスには入力フォームがありました。メイン アクティビティはログイン フォームである必要がありました。そのため、メイン アクティビティ内のコードを Datospersonales.java という名前のクラスに移動し、ログイン フォームを MainActivity.java に配置しましたが、それ以降はまったく実行されません。

05-06 10:01:35.681: E/AndroidRuntime(278): FATAL EXCEPTION: main
05-06 10:01:35.681: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lania.saludandroidte/com.lania.saludandroidte.MainActivity}: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-06 10:01:35.681: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:01:35.681: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
05-06 10:01:35.681: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-06 10:01:35.681: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-06 10:01:35.681: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
05-06 10:01:35.681: E/AndroidRuntime(278): Caused by: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:01:35.681: E/AndroidRuntime(278):  at com.lania.saludandroidte.MainActivity.onCreate(MainActivity.java:19)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-06 10:01:35.681: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-06 10:01:35.681: E/AndroidRuntime(278):  ... 11 more
05-06 10:20:01.692: D/AndroidRuntime(309): Shutting down VM
05-06 10:20:01.702: W/dalvikvm(309): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
05-06 10:20:01.712: E/AndroidRuntime(309): FATAL EXCEPTION: main
05-06 10:20:01.712: E/AndroidRuntime(309): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.lania.saludandroidte/com.lania.saludandroidte.MainActivity}: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.os.Handler.dispatchMessage(Handler.java:99)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.os.Looper.loop(Looper.java:123)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.main(ActivityThread.java:4627)
05-06 10:20:01.712: E/AndroidRuntime(309):  at java.lang.reflect.Method.invokeNative(Native Method)
05-06 10:20:01.712: E/AndroidRuntime(309):  at java.lang.reflect.Method.invoke(Method.java:521)
05-06 10:20:01.712: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
05-06 10:20:01.712: E/AndroidRuntime(309):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
05-06 10:20:01.712: E/AndroidRuntime(309):  at dalvik.system.NativeStart.main(Native Method)
05-06 10:20:01.712: E/AndroidRuntime(309): Caused by: java.lang.ClassCastException: com.lania.saludandroidte.MainActivity
05-06 10:20:01.712: E/AndroidRuntime(309):  at com.lania.saludandroidte.MainActivity.onCreate(MainActivity.java:19)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
05-06 10:20:01.712: E/AndroidRuntime(309):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
05-06 10:20:01.712: E/AndroidRuntime(309):  ... 11 more
05-06 10:20:03.832: I/Process(309): Sending signal. PID: 309 SIG: 9

コードは次のようになります。

 package com.lania.saludandroidte;


import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends Activity{
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Button log = (Button) findViewById(R.id.btnLogin2);
        log.setOnClickListener((OnClickListener) this);

        TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

        // Listening to register new account link
        registerScreen.setOnClickListener(new View.OnClickListener() {

            public void onClick(View v) {
                // Switching to Register screen

                if (v.getId() == R.id.btnLogin2) {
                    startActivity(new Intent(MainActivity.this, Menu.class));
                }
                Intent i = new Intent(getApplicationContext(), RegisterActivity.class);
                startActivity(i);
            }
        });
    }



}






**And this is the layout:**

    <?xml version="1.0" encoding="utf-8"?>
<ScrollView
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="fill_parent"
  android:layout_height="fill_parent"
  android:fillViewport="true">
  <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" android:background="#ffffff">

        <!--  Header  Starts-->
        <LinearLayout android:id="@+id/header"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:background="@layout/header_gradient"
                android:paddingTop="5dip"
                android:paddingBottom="5dip">
                <!-- Logo Start-->
                <!-- Logo Ends -->
        </LinearLayout>
        <!--  Header Ends -->
        <!-- Footer Start -->
        <LinearLayout android:id="@+id/footer"
                android:layout_width="fill_parent"
                android:layout_height="90dip"
                android:background="@layout/footer_repeat"
                android:layout_alignParentBottom="true">
        </LinearLayout>
        <!-- Footer Ends -->

        <!-- Login Form -->
        <LinearLayout

          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:padding="10dip"
          android:layout_below="@id/header">
          <!--  Email Label -->
          <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Email"/>
          <EditText android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:layout_marginBottom="20dip"
                android:singleLine="true"/>
          <!--  Password Label -->
          <TextView android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:textColor="#372c24"
                android:text="Password"/>
          <EditText android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginTop="5dip"
                android:singleLine="true"
                android:password="true"/>
          <!-- Login button -->

          <Button
              android:id="@+id/btnLogin2"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginTop="10dip"
              android:text="Login" />

          <!-- Link to Registration Screen -->

          <TextView
              android:id="@+id/link_to_register"
              android:layout_width="fill_parent"
              android:layout_height="wrap_content"
              android:layout_marginBottom="40dip"
              android:layout_marginTop="40dip"
              android:gravity="center"
              android:text="Registrarse "
              android:textColor="#0b84aa"
              android:textSize="20dip" />

        </LinearLayout>
        <!-- Login Form Ends -->
  </RelativeLayout>
</ScrollView>
4

2 に答える 2

0

クラスは OnClicListener を実装し、onClick メソッドをオーバーライドする必要があります

public class MainActivity extends Activity implements OnClickListener{
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    Button log = (Button) findViewById(R.id.btnLogin2);
    log.setOnClickListener(this);

    TextView registerScreen = (TextView) findViewById(R.id.link_to_register);

    // Listening to register new account link
    registerScreen.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // Switching to Register screen

            if (v.getId() == R.id.btnLogin2) {// this is never true
                startActivity(new Intent(MainActivity.this, Menu.class));
            }
            // always navigates to RegisterActivity on textview click
            // not sure what you want to achieve.   
            Intent i = new Intent(MainActivity.this, RegisterActivity.class);
            startActivity(i);
        }
    });
 }

@Override
public void onClick(View v) {

          // on button click if you want to navigate to menu activity
           startActivity(new Intent(MainActivity.this, Menu.class));

}
}

                             OR

以下のように匿名の内部クラスを使用できます

  log.setOnClickListener(new OnClickListener()
            {
                  public void onClick(View v)
                  {
                   //do something
                   // on button click if you want to navigate to menu activity
                   startActivity(new Intent(MainActivity.this, Menu.class));  
                  }                 
            });

また、getApplicationContext() の代わりにアクティビティ コンテキストを使用します。

アクティビティ コンテキストまたはアプリケーション コンテキストをいつ呼び出すか?

コモンズウェアによる回答を確認する

于 2013-05-06T16:45:45.933 に答える