当我打开它时,Android简单应用程序崩溃了

时间:2012-12-28 21:43:36

标签: android

我的主要课程:

package com.example.hi;
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;


public class SampleLogin 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.activity_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() {

   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub

    if((txtUserName.getText().toString()).equals(txtPassword.getText().toString())){
           Toast.makeText(SampleLogin.this, "Login Successful",Toast.LENGTH_LONG).show();
          } else{
           Toast.makeText(SampleLogin.this, "Invalid Login",Toast.LENGTH_LONG).show();
          }

   }
  });       
    }
} 

我的XML文件......

<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="@string/User_Name: " 
 android:id="@+id/TextView01" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
 </TextView>

 <EditText 
 android:inputType="textPassword|number"
 android:text="" 
 android:id="@+id/txtUname" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">

 </EditText>
</TableRow>


<TableRow>
 <TextView 
 android:text="@string/Password: " 
 android:id="@+id/TextView02" 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content">
 </TextView>


 <EditText 
 android:inputType="textPassword|number" 
 android:text="" 
 android:id="@+id/txtPwd" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"  />

</TableRow>


<TableRow>
 <Button
 android:text="@string/Cancel" 
 android:id="@+id/btnCancel" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
 </Button>


 <Button
 android:text="@string/Login" 
 android:id="@+id/btnLogin" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content">
 </Button>


</TableRow>


</TableLayout>

所以这是我的代码由于某种原因,当我打开它时,应用程序停止工作,“application()已经意外停止。请再试一次avd ...”我不知道为什么会这样,请帮助!< / p>

我是android的新手,有人可以告诉我如何获取catlog文件吗?

关于xml文件中的字符串,我是新手,我得到一个关于“硬编码字符串”或类似的东西的错误,它说我需要将它添加到res中找到的字符串文件文件夹所以我添加了字符串,然后添加了@string

CatLog档案:

12-28 17:36:50.883: E/Zygote(33): setreuid() failed. errno: 2
12-28 17:37:00.573: E/Zygote(33): setreuid() failed. errno: 17
12-28 17:37:01.933: E/BatteryService(61): usbOnlinePath not found
12-28 17:37:01.933: E/BatteryService(61): batteryVoltagePath not found
12-28 17:37:01.933: E/BatteryService(61): batteryTemperaturePath not found
12-28 17:37:01.953: E/SurfaceFlinger(61): Couldn't open /sys/power/wait_for_fb_sleep or /sys/power/wait_for_fb_wake
12-28 17:37:02.173: E/SensorService(61): couldn't open device for module sensors (Invalid argument)
12-28 17:37:06.682: E/System(61): Failure starting core service
12-28 17:37:06.682: E/System(61): java.lang.SecurityException
12-28 17:37:06.682: E/System(61):   at android.os.BinderProxy.transact(Native Method)
12-28 17:37:06.682: E/System(61):   at android.os.ServiceManagerProxy.addService(ServiceManagerNative.java:146)
12-28 17:37:06.682: E/System(61):   at android.os.ServiceManager.addService(ServiceManager.java:72)
12-28 17:37:06.682: E/System(61):   at com.android.server.ServerThread.run(SystemServer.java:206)
12-28 17:37:06.712: E/EventHub(61): could not get driver version for /dev/input/mouse0, Not a typewriter
12-28 17:37:06.712: E/EventHub(61): could not get driver version for /dev/input/mice, Not a typewriter
12-28 17:37:07.122: E/SoundPool(61): error loading /system/media/audio/ui/Effect_Tick.ogg
12-28 17:37:07.122: E/SoundPool(61): error loading /system/media/audio/ui/KeypressStandard.ogg
12-28 17:37:07.122: E/SoundPool(61): error loading /system/media/audio/ui/KeypressSpacebar.ogg
12-28 17:37:07.122: E/SoundPool(61): error loading /system/media/audio/ui/KeypressDelete.ogg
12-28 17:37:07.122: E/SoundPool(61): error loading /system/media/audio/ui/KeypressReturn.ogg
12-28 17:37:07.164: E/UsbObserver(61): java.lang.NullPointerException
12-28 17:37:07.164: E/UsbObserver(61):  at com.android.server.UsbObserver.init(UsbObserver.java:131)
12-28 17:37:07.164: E/UsbObserver(61):  at com.android.server.UsbObserver.<init>(UsbObserver.java:65)
12-28 17:37:07.164: E/UsbObserver(61):  at com.android.server.ServerThread.run(SystemServer.java:402)
12-28 17:37:07.673: E/ThrottleService(61): Could not open GPS configuration file /etc/gps.conf
12-28 17:37:08.392: E/logwrapper(135): executing /system/bin/tc failed: No such file or directory
12-28 17:37:08.392: E/logwrapper(136): executing /system/bin/tc failed: No such file or directory
12-28 17:37:08.432: E/logwrapper(138): executing /system/bin/tc failed: No such file or directory
12-28 17:37:35.198: E/AndroidRuntime(335): FATAL EXCEPTION: main
12-28 17:37:35.198: E/AndroidRuntime(335): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hi/com.example.hi.MainActivity}: java.lang.ClassNotFoundException: com.example.hi.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.hi-1.apk]
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.os.Looper.loop(Looper.java:123)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-28 17:37:35.198: E/AndroidRuntime(335):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 17:37:35.198: E/AndroidRuntime(335):  at java.lang.reflect.Method.invoke(Method.java:507)
12-28 17:37:35.198: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-28 17:37:35.198: E/AndroidRuntime(335):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-28 17:37:35.198: E/AndroidRuntime(335):  at dalvik.system.NativeStart.main(Native Method)
12-28 17:37:35.198: E/AndroidRuntime(335): Caused by: java.lang.ClassNotFoundException: com.example.hi.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.hi-1.apk]
12-28 17:37:35.198: E/AndroidRuntime(335):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-28 17:37:35.198: E/AndroidRuntime(335):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-28 17:37:35.198: E/AndroidRuntime(335):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 17:37:35.198: E/AndroidRuntime(335):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
12-28 17:37:35.198: E/AndroidRuntime(335):  ... 11 more
12-28 17:38:05.517: E/AndroidRuntime(344): FATAL EXCEPTION: main
12-28 17:38:05.517: E/AndroidRuntime(344): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.hi/com.example.hi.MainActivity}: java.lang.ClassNotFoundException: com.example.hi.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.hi-1.apk]
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1569)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.os.Handler.dispatchMessage(Handler.java:99)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.os.Looper.loop(Looper.java:123)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.app.ActivityThread.main(ActivityThread.java:3683)
12-28 17:38:05.517: E/AndroidRuntime(344):  at java.lang.reflect.Method.invokeNative(Native Method)
12-28 17:38:05.517: E/AndroidRuntime(344):  at java.lang.reflect.Method.invoke(Method.java:507)
12-28 17:38:05.517: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
12-28 17:38:05.517: E/AndroidRuntime(344):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
12-28 17:38:05.517: E/AndroidRuntime(344):  at dalvik.system.NativeStart.main(Native Method)
12-28 17:38:05.517: E/AndroidRuntime(344): Caused by: java.lang.ClassNotFoundException: com.example.hi.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.hi-1.apk]
12-28 17:38:05.517: E/AndroidRuntime(344):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:240)
12-28 17:38:05.517: E/AndroidRuntime(344):  at java.lang.ClassLoader.loadClass(ClassLoader.java:551)
12-28 17:38:05.517: E/AndroidRuntime(344):  at java.lang.ClassLoader.loadClass(ClassLoader.java:511)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
12-28 17:38:05.517: E/AndroidRuntime(344):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1561)
12-28 17:38:05.517: E/AndroidRuntime(344):  ... 11 more

它不再崩溃,但我无法输入任何东西!我在xml文件上更改了关于打字的内容,因为它给了我一个问题

android:inputType="textPassword|number"

我认为就是这样,但当我尝试输入某些内容时,它什么也没做!

这应该有用吗?

<EditText 
 android:inputType="textPassword|number" 
 android:text="" 
 android:id="@+id/txtPwd" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content"  />


 <EditText 
 android:inputType="textPassword|number"
 android:text="" 
 android:id="@+id/txtUname" 
 android:layout_width="fill_parent" 
 android:layout_height="wrap_content" />

2 个答案:

答案 0 :(得分:2)

从您的LogCat:

Caused by: java.lang.ClassNotFoundException: com.example.hi.MainActivity in loader dalvik.system.PathClassLoader[/data/app/com.example.hi-1.apk]

这意味着您可能已将您的班级名称从SampleLogin更改为MainActivity,但忘记更新您的清单。

将清单更新为如下所示:

<activity
    android:name=".SampleLogin"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

(您还需要按照我在评论中提到的android:text="@string/User_Name: "android:text="@string/Password: "来解决您再次编译应用之前的问题。)

答案 1 :(得分:1)

您应该看到LogCat确切地知道错误是什么。

我能在你的代码中发现的唯一一件事就是你正在启动btnLogin两次并且错过了btnCancel的初始化。除非你在某处使用btnCancel对象,否则这不会是错误的来源。

 btnLogin=(Button)this.findViewById(R.id.btnLogin);
 btnLogin=(Button)this.findViewById(R.id.btnLogin);

如果您可以从LogCat发布错误,则可以更轻松地提供帮助。

修改

在您的布局XML文件中,您看起来很奇怪

android:text="@string/Password: " 

android:id="@+id/TextView01" 

如果需要,资源名称不应该是所有小写字母,只有下划线作为字词分隔符。