我在php写了一个预算程序,然后回到智能手机上使用。我现在正在尝试重写Android平板电脑,它不会总是有互联网所以php选项不可用。所以我正在慢慢地尝试学习java / android,但是我已经在这个问题上遇到了一段时间。
我的问题是在加载我的虚假登录屏幕并点击登录按钮后,它在执行startActivity(i)时崩溃了;我无法弄清楚为什么代码实际上是从(http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html)
复制的我有2个java文件
MainActivity.java
package com.mctrivia.cloudbudget;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Intent;
public class MainActivity extends Activity {
EditText txtUserName;
EditText txtPassword;
Button btnLogin;
Button btnCancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
txtUserName=(EditText)this.findViewById(R.id.txtUname);
txtPassword=(EditText)this.findViewById(R.id.txtPwd);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
btnLogin=(Button)this.findViewById(R.id.btnLogin);
btnLogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
//Toast.makeText(MainActivity.this, "Login Successful",Toast.LENGTH_LONG).show();
Intent i = new Intent(MainActivity.this, CashActivity.class);
startActivity(i);
} else{
Toast.makeText(MainActivity.this, "Invalid Login",Toast.LENGTH_LONG).show();
}
}
});
}
}
CashActivity.java
package com.mctrivia.cloudbudget;
import android.app.Activity;
import android.os.Bundle;
public class CashActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.cats);
}
}
2个XML文件
main.xml和cats.xml它们是相同的,除了用于登录的猫的标题更改为“a”,所以我可以知道发生了变化
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1">
<TableRow>
<TextView
android:text="User Name: "
android:id="@+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:text=""
android:id="@+id/txtUname"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</EditText>
</TableRow>
<TableRow>
<TextView
android:text="Password: "
android:id="@+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
<EditText
android:text=""
android:id="@+id/txtPwd"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:password="true">
</EditText>
</TableRow>
<TableRow>
<Button
android:text="Cancel"
android:id="@+id/btnCancel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
<Button
android:text="Login"
android:id="@+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</Button>
</TableRow>
</TableLayout>
清单如下:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mctrivia.cloudbudget"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="13" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".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=".CatsActivity" android:label="CatsActivity">
</activity>
</application>
</manifest>
错误日志:
01-18 18:33:48.790: E/AndroidRuntime(386): FATAL EXCEPTION: main
01-18 18:33:48.790: E/AndroidRuntime(386): android.content.ActivityNotFoundException: Unable to find explicit activity class {com.mctrivia.cloudbudget/com.mctrivia.cloudbudget.CashActivity}; have you declared this activity in your AndroidManifest.xml?
01-18 18:33:48.790: E/AndroidRuntime(386): at android.app.Instrumentation.checkStartActivityResult(Instrumentation.java:1504)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.app.Instrumentation.execStartActivity(Instrumentation.java:1382)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.app.Activity.startActivityForResult(Activity.java:3131)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.app.Activity.startActivity(Activity.java:3237)
01-18 18:33:48.790: E/AndroidRuntime(386): at com.mctrivia.cloudbudget.MainActivity$1.onClick(MainActivity.java:39)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.view.View.performClick(View.java:3110)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.view.View$PerformClick.run(View.java:11934)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.os.Handler.handleCallback(Handler.java:587)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.os.Handler.dispatchMessage(Handler.java:92)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.os.Looper.loop(Looper.java:132)
01-18 18:33:48.790: E/AndroidRuntime(386): at android.app.ActivityThread.main(ActivityThread.java:4123)
01-18 18:33:48.790: E/AndroidRuntime(386): at java.lang.reflect.Method.invokeNative(Native Method)
01-18 18:33:48.790: E/AndroidRuntime(386): at java.lang.reflect.Method.invoke(Method.java:491)
01-18 18:33:48.790: E/AndroidRuntime(386): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
01-18 18:33:48.790: E/AndroidRuntime(386): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
01-18 18:33:48.790: E/AndroidRuntime(386): at dalvik.system.NativeStart.main(Native Method)
我做错了什么?
答案 0 :(得分:0)
尝试做
<activity
android:name=".CashActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="com.mctrivia.cloudbudget.CASHACTIVITY" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
而不是:
</activity>
<activity android:name=".CatsActivity" android:label="CatsActivity">
</activity>