setOnClickListener crashs我的Andr​​oid应用程序 [英] setOnClickListener crashs my android app

查看:160
本文介绍了setOnClickListener crashs我的Andr​​oid应用程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在面临一个问题,我试着在我的Andr​​oid应用程序,以实现一个登录界面。用户必须填写用户名和密码字段字符串用户。 每当我运行此code,我的Andr​​oid虚拟设备crashs。它甚至不开放,不幸的是,LoginScreen已停止错误信息出现在手机屏幕。这是我的code:

I am facing a problem as I try to implement a login screen in my Android app. The user has to fill out the login and password field with the string "user". Whenever I run this code, my android virtual device crashs. It doesn't even open and "Unfortunately, LoginScreen has stopped" error message appears on device screen. This is my code:

我Main_Activity

My Main_Activity

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     

fragment_main

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

    <requestFocus />
</EditText>

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

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

我使用虚拟框的Eclipse + ADT 22.3版本。 该应用程序有以下配置

I am using Eclipse + ADT 22.3 version on Virtual Box. The app has the following configuration

Minimum Required SDK: API 18
Target SDK: API 19
Compile With: API 19

请,我究竟哪里做错了?

Please, what am I doing wrong?

推荐答案

正在初始化的观点是在 fragment_main.xml 的活动。相反,你应该初始化它的片段 onCreateView 使用 rootView.findViewById

You are initializing views that is in fragment_main.xml in Activity. Instead you should initialize it in Fragment onCreateView using rootView.findViewById.

findViewById 查找在当前视图层次视图。您已设置 activity_main.xml 到活动。所以,如果你在活动初始​​化失败的意见导致 NullPointerException异常

findViewById looks for a view in the current view hierarchy. You have set activity_main.xml to Activity. So if you initializing views in Activity fails leading to NUllPointerException.

public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
    }
    Button ok = null;
    EditText login = null;
    EditText pass = null;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_main, container,
                false);
        ok = (Button) rootView.findViewById(R.id.ok);  

        login = (EditText) rootView.findViewById(R.id.login_text); 
        pass = (EditText) rootView.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(getActivity(), "Redirecting...", 
                            Toast.LENGTH_SHORT).show();
                        } else {
                            Toast.makeText(getActivity(), "Wrong Credentials",
                            Toast.LENGTH_SHORT).show();
                        }
                }
        } 
     }); 
        return rootView;
    }
}

这篇关于setOnClickListener crashs我的Andr​​oid应用程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆