-1

我的主要课程:

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>

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

我是android新手,谁能告诉我如何获取catlog文件?

关于 xml 文件上的字符串,我还是新手,我收到关于“硬编码字符串”或类似内容的错误,它说我需要将它添加到 res 文件夹中找到的字符串文件中,所以我添加字符串,然后添加@string

猫日志文件:

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" />
4

2 回答 2

2

从您的 LogCat 中:

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

这意味着您可能将类名更改为SampleLoginfromMainActivity但忘记更新清单。

将清单更新为如下所示:

<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: "正如我在评论中提到的那样,您才能再次编译您的应用程序。)

于 2012-12-28T22:14:14.220 回答
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" 

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

于 2012-12-28T21:51:21.737 回答