我在尝试在Android应用中实现登录屏幕时遇到问题.用户必须使用字符串“ user”填写登录名和密码字段.

每当我运行此代码时,我的android虚拟设备就会崩溃.它甚至没有打开,并且设备屏幕上出现“不幸的是,LoginScreen已停止”错误消息.这是我的代码:

我的主要活动

package com.example.loginscreen;

import android.app.Activity;

import android.app.Fragment;

import android.os.Bundle;

import android.view.LayoutInflater;

import android.view.Menu;

import android.view.MenuItem;

import android.view.View;

import android.view.ViewGroup;

import android.widget.Button;

import android.widget.EditText;

import android.widget.Toast;

public class MainActivity extends Activity {

Button ok = null;

EditText login = null;

EditText pass = null;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

if (savedInstanceState == null) {

getFragmentManager().beginTransaction()

.add(R.id.container, new PlaceholderFragment()).commit();

}

ok = (Button) findViewById(R.id.ok);

login = (EditText) findViewById(R.id.login_text);

pass = (EditText) findViewById(R.id.pass_text);

ok.setOnClickListener(new View.OnClickListener() {

public void onClick(View view) {

String loginStr = login.getText().toString();

String passStr = pass.getText().toString();

if (loginStr != null && loginStr.equals("user")) {

if (passStr != null && passStr.equals("user")) {

Toast.makeText(getApplicationContext(), "Redirecting...",

Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(getApplicationContext(), "Wrong Credentials",

Toast.LENGTH_SHORT).show();

}

}

}

});

}

@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 boolean onOptionsItemSelected(MenuItem item) {

// Handle action bar item clicks here. The action bar will

// automatically handle clicks on the Home/Up button, so long

// as you specify a parent activity in AndroidManifest.xml.

int id = item.getItemId();

if (id == R.id.action_settings) {

return true;

}

return super.onOptionsItemSelected(item);

}

/**

* A placeholder fragment containing a simple view.

*/

public static class PlaceholderFragment extends Fragment {

public PlaceholderFragment() {

}

@Override

public View onCreateView(LayoutInflater inflater, ViewGroup container,

Bundle savedInstanceState) {

View rootView = inflater.inflate(R.layout.fragment_main, container,

false);

return rootView;

}

}

}

fragment_main

????

android:id="@+id/login_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentTop="true"

android:layout_centerHorizontal="true"

android:layout_marginTop="117dp"

android:ems="10"

android:inputType="textPersonName" >

android:id="@+id/pass_text"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/login_text"

android:layout_centerHorizontal="true"

android:layout_marginTop="72dp"

android:ems="10"

android:inputType="textPassword" />

android:id="@+id/ok"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_below="@+id/pass_text"

android:layout_centerHorizontal="true"

android:layout_marginTop="56dp"

android:text="@string/ok" />

我的LogCat

04-20 11:50:39.501: D/AndroidRuntime(2471): Shutting down VM

04-20 11:50:39.501: W/dalvikvm(2471): threadid=1: thread exiting with uncaught exception (group=0xb1a21ba8)

04-20 11:50:39.511: E/AndroidRuntime(2471): FATAL EXCEPTION: main

04-20 11:50:39.511: E/AndroidRuntime(2471): Process: com.example.loginscreen, PID: 2471

04-20 11:50:39.511: E/AndroidRuntime(2471): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginscreen/com.example.loginscreen.MainActivity}: java.lang.NullPointerException

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.ActivityThread.access$800(ActivityThread.java:135)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.os.Handler.dispatchMessage(Handler.java:102)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.os.Looper.loop(Looper.java:136)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.ActivityThread.main(ActivityThread.java:5017)

04-20 11:50:39.511: E/AndroidRuntime(2471): at java.lang.reflect.Method.invokeNative(Native Method)

04-20 11:50:39.511: E/AndroidRuntime(2471): at java.lang.reflect.Method.invoke(Method.java:515)

04-20 11:50:39.511: E/AndroidRuntime(2471): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)

04-20 11:50:39.511: E/AndroidRuntime(2471): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)

04-20 11:50:39.511: E/AndroidRuntime(2471): at dalvik.system.NativeStart.main(Native Method)

04-20 11:50:39.511: E/AndroidRuntime(2471): Caused by: java.lang.NullPointerException

04-20 11:50:39.511: E/AndroidRuntime(2471): at com.example.loginscreen.MainActivity.onCreate(MainActivity.java:38)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.Activity.performCreate(Activity.java:5231)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)

04-20 11:50:39.511: E/AndroidRuntime(2471): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)

04-20 11:50:39.511: E/AndroidRuntime(2471): ... 11 more

我在Virtual Box上使用Eclipse ADT 22.3版本.

该应用程序具有以下配置

Minimum Required SDK: API 18

Target SDK: API 19

Compile With: API 19

拜托,我在做什么错?

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐