我对编程和Android完全是个新手。
我的学校团队和我试图开发我们的第一个android应用程序,我们实际上并没有计划好,我们最初的MainActivity类有一个输入表单,然而,当我们开始做应用程序的其余部分时,我们意识到主要的活动需要是一个登录表单。因此,我们将主活动中的代码移动到一个名为datosppersones。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>
log.setOnClickListener((OnClickListener) this);
是你的问题,你的类没有实现OnClickListener
将implements OnClickListener
附加到MainActivity extends Activity
,这应该可以工作。
你的类必须实现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()
何时调用活动上下文或应用程序上下文?
通过commonsware检查答案