2014-04-20 101 views
1

我面臨一個問題,因爲我嘗試在我的Android應用程序中實現登錄屏幕。用戶必須使用字符串「user」填寫登錄名和密碼字段。 每當我運行此代碼時,我的android虛擬設備崩潰。它甚至沒有打開,並且「不幸的是,LoginScreen已停止」錯誤消息出現在設備屏幕上。這是我的代碼:setOnClickListener崩潰我的Android應用程序

我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

<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 

請,我究竟做錯了什麼?

+0

'產生的原因:在com。示例顯示java.lang.NullPointerException。 loginscreen.MainActivity.onCreate(MainActivity.java:38)'。這意味着行38. – keyser

回答

0

要初始化的觀點是在fragment_main.xml的活動。相反,您應該使用rootView.findViewById將其初始化爲片段onCreateView

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; 
    } 
} 
+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; 
    } 
} 
+0

它的工作。非常感謝你! –

0

add方法

myMethod() { 

//Code hear 

} 

的活動,並在您的XML添加

android:onClick="myMethod" 

爲了您的按鈕