简体   繁体   English

尝试更改布局时,android应用程序意外崩溃

[英]android app crashes unexpectedly when trying to change layouts

I wrote a budget program in php a while back to use on smart phones. 我有一段时间用php编写了一个预算程序,可以在智能手机上使用。 I am now trying to rewrite for android tablet which will not always have internet so the php option is not available. 我现在正尝试为Android平板电脑改写,该平板电脑将不总是具有互联网,因此php选项不可用。 So I am slowly trying to learn java/android but have been hitting my head against this problem for a while. 因此,我正在慢慢尝试学习Java / android,但是已经有一段时间了我对此问题不屑一顾。

My problem is after loading my fake login screen and hitting the login button it crashes right when it executes the startActivity(i); 我的问题是加载我的假登录屏幕并按下登录按钮后,它在执行startActivity(i)时立即崩溃。 line and I can not figure out why as the code was practically copied from ( http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html ) 行,我不知道为什么从( http://learnandroid.blogspot.com/2008/01/opening-new-screen-in-android.html )中复制了代码

i have 2 java files 我有2个Java文件

MainActivity.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 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 Files 2个XML文件

main.xml and cats.xml they are identical except caption on cats for login was changed to "a" so i could know that the change happened 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>

Manifest is as follows: 清单如下:

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

error log: 错误日志:

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)

what am I doing wrong? 我究竟做错了什么?

try doing 尝试做

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

instead of: 代替:

</activity>
    <activity android:name=".CatsActivity" android:label="CatsActivity">
    </activity>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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