我开发了一款名为pizza hut app的应用程序。这包含简单登录页面的代码,单击登录按钮时,必须导航到另一个名为“成功登录”的页面。这是我的代码。但我遇到的问题是“不幸的是披萨小屋应用程序停止了”。请帮我解决问题。
//main_activity
package com.example.pizzahutapp;
import android.os.Bundle;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class MainActivity extends Activity implements OnClickListener {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(arg0.getId()== R.id.button1)
{
Intent in = new Intent(this,SuccessfulLogin.class);
this.startActivity(in);
}
}
}
//成功登录(第二项活动)
package com.example.pizzahutapp;
import android.app.Activity;
import android.os.Bundle;
public class SuccessfulLogin extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// TODO Auto-generated method stub
}
}
//清单
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.pizzahutapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.pizzahutapp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SuccessfulLogin"
android:label="@string/app_name"></activity>
</application>
</manifest>
// catlog
01-16 01:10:13.186: E/AndroidRuntime(1149): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.pizzahutapp/com.example.pizzahutapp.MainActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.view.ViewGroup
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.os.Handler.dispatchMessage(Handler.java:102)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.os.Looper.loop(Looper.java:137)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-16 01:10:13.186: E/AndroidRuntime(1149): at java.lang.reflect.Method.invokeNative(Native Method)
01-16 01:10:13.186: E/AndroidRuntime(1149): at java.lang.reflect.Method.invoke(Method.java:515)
01-16 01:10:13.186: E/AndroidRuntime(1149): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-16 01:10:13.186: E/AndroidRuntime(1149): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-16 01:10:13.186: E/AndroidRuntime(1149): at dalvik.system.NativeStart.main(Native Method)
01-16 01:10:13.186: E/AndroidRuntime(1149): Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to android.view.ViewGroup
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.view.LayoutInflater.rInflate(LayoutInflater.java:756)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.view.LayoutInflater.rInflate(LayoutInflater.java:758)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
01-16 01:10:13.186: E/AndroidRuntime(1149): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:290)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.Activity.setContentView(Activity.java:1928)
01-16 01:10:13.186: E/AndroidRuntime(1149): at com.example.pizzahutapp.MainActivity.onCreate(MainActivity.java:22)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.Activity.performCreate(Activity.java:5243)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-16 01:10:13.186: E/AndroidRuntime(1149): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-16 01:10:13.186: E/AndroidRuntime(1149): ... 11 more
答案 0 :(得分:1)
更改
public class MainActivity extends Activity implements onClickListener
到
public class MainActivity extends Activity implements OnClickListener
还有你的导入语句,
import android.content.DialogInterface.OnClickListener;
到
import android.view.View.OnClickListener;
最后,正如我的评论中所述,您还应该将您的SuccessfulLogin
活动声明更新为如下所示,并在类名前加上一段时间:
<activity android:name=".SuccessfulLogin"
答案 1 :(得分:1)
how to fix unfortunately appname has stopped
你应该看看堆栈跟踪。有一个部分引起的,它给出了发生异常的行号。您可以转到该行并修复问题
您也可以使用调试器。放入断点并调试代码。
应该是
public class MainActivity extends Activity implements OnClickListener
更改
import android.content.DialogInterface.OnClickListener;
应该是
import android.view.View.OnClickListener;
虽然这没有错误
View v = findViewById(R.id.button1);
建议改为
Button b =(Button) findViewById(R.id.button1);
b.setOnClickListener(this);
编辑:
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
this.setContentView(R.layout.successfullogin); // remove this statement
return true;
}