setOnClickListener崩溃我的Android应用程序

时间:2014-04-20 16:06:02

标签: android eclipse

当我尝试在Android应用中实现登录屏幕时,我遇到了问题。用户必须使用字符串" user"填写登录名和密码字段。 每当我运行此代码时,我的android虚拟设备都会崩溃。它甚至没有打开和#34;不幸的是,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     

<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

我在Virtual Box上使用Eclipse + ADT 22.3版本。 该应用程序具有以下配置

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

拜托,我做错了什么?

3 个答案:

答案 0 :(得分:0)

试试这个..

由于ButtonEditText都属于fragment_main.xml,因此您必须在PlaceholderFragment.java

中执行此操作
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;
    }
}

答案 1 :(得分:0)

您正在初始化活动中fragment_main.xml中的视图。相反,您应该使用onCreateView在片段rootView.findViewById中初始化它。

findViewById在当前视图层次结构中查找视图。您已将activity_main.xml设置为“活动”。因此,如果您在Activity中初始化视图失败,则导致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;
    }
}

答案 2 :(得分:0)

添加方法

myMethod() {

//Code hear

}

To Activity和xml add

android:onClick="myMethod"

到你的按钮