2014-02-14 56 views
0

所以我想創建一個共享首選項和應用程序工作正常,然後突然它破裂。共享首選項是凍結我的應用程序 - Android手機應用程序

我已經指責它到了破解代碼的確切路線,我不確定如何解決它。

有什麼建議嗎?

這裏是我當前的代碼:

public class studentEmail extends Activity { 
    private WebView mWebview; 

    public static final String PREFS_NAME = "MyPrefsFile"; 

    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
    setContentView(R.layout.student_email) ; 



final SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); 

mWebview = new WebView(this); 


     final EditText emEdit = (EditText)findViewById(R.id.editEmail); 
     final EditText passEdit = (EditText)findViewById(R.id.editPass); 

if(preferences.getString("username", null).isEmpty() && preferences.getString("password", null).isEmpty()) 
    { 

     mWebview = new WebView(this); 

     mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript 

     final Activity activity = this; 

     mWebview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

Toast.makeText(getApplicationContext(), "Preference is empty :)", Toast.LENGTH_SHORT).show(); 

     TextView text = (TextView)findViewById(R.id.textView1); 

     TextView textEmail = (TextView)findViewById(R.id.textViewEmail); 
     TextView textPass = (TextView)findViewById(R.id.textViewPass); 
     TextView textTitle = (TextView)findViewById(R.id.textViewTitle); 
     text.setTypeface(null, Typeface.BOLD_ITALIC); 
     textEmail.setTypeface(null, Typeface.BOLD_ITALIC); 
     textPass.setTypeface(null, Typeface.BOLD_ITALIC); 
     textTitle.setTypeface(null, Typeface.BOLD_ITALIC); 
     text.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX,26); 
     textEmail.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX,26); 
     textPass.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX,26); 
     textTitle.setTextSize(android.util.TypedValue.COMPLEX_UNIT_PX, 40); 
     textTitle.setText("E-mail Login"); 
     text.setText("\n\nPlease enter your Student E-mail and Password.\n"); 
     textEmail.setText("E-Mail:"); 
     textPass.setText("\nPassword:"); 

     final EditText emailValidate = (EditText)findViewById(R.id.editEmail); 
     final TextView emailValMsg = (TextView)findViewById(R.id.emailValidate); 
     final EditText password = (EditText)findViewById(R.id.editPass); 

emailValidate.addTextChangedListener(new TextWatcher() { 

      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

      } 
      public void afterTextChanged(Editable s) { 

       String email = emailValidate.getText().toString().trim(); 
       String emailPattern = "[a-zA-Z0-9._-][email protected]+\\.+itsligo+\\.+ie+"; 

       if (email.matches(emailPattern) && s.length() > 0) 
       { 
        Toast.makeText(getApplicationContext(), "Your e-mail is now valid! :)", Toast.LENGTH_SHORT).show(); 
        // or 
        emailValMsg.setText("Valid email"); 
       } 
       else 
       { 
        emailValMsg.setText("Invalid email !"); 
       } 
      } 
     }); 

final Button button = (Button) findViewById(R.id.loginButton); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       String email = emailValidate.getText().toString().trim(); 
       String emailPattern = "[a-zA-Z0-9._-][email protected][a-z]+\\.+[a-z]+"; 

       if (emailValidate.getText().toString().trim().matches("") || password.getText().toString().trim().matches("")) { 
        if (emailValidate.getText().toString().trim().matches("") && password.getText().toString().trim().matches("")) { 
         Toast.makeText(getApplicationContext(), "Please enter an E-mail Address and Password", Toast.LENGTH_SHORT).show(); 
        } else if (emailValidate.getText().toString().trim().matches("")) { 
         Toast.makeText(getApplicationContext(), "Please enter an E-mail Address", Toast.LENGTH_SHORT).show(); 
        } else if (password.getText().toString().trim().matches("")) { 
         Toast.makeText(getApplicationContext(), "Please enter a Password", Toast.LENGTH_SHORT).show(); 
        } 
       } 

else { 
        mWebview.setWebViewClient(new WebViewClient() { 
         @Override 
         public void onPageFinished(WebView view, String url) { 
          String webUrl = mWebview.getUrl(); 

            CheckBox cbox1 = (CheckBox)findViewById(R.id.checkbox_store); 

          if (cbox1.isChecked()) 
          { 

          } 
          mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_userid_inputtext').value ='" + emEdit.getText() + "';" + "})()"); 
          mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_password_inputtext').value ='" + passEdit.getText() + "';" + "})()"); 
          mWebview.loadUrl("javascript:(function() { " + "document.getElementById('credentials').submit(); return false;" + "})()"); 
         } 
        }); 

        mWebview.loadUrl("http://login.microsoftonline.com"); 
        setContentView(mWebview); 
        Toast.makeText(getApplicationContext(), "This works!", Toast.LENGTH_SHORT).show(); 

       } 

      } 
     }); 
    } 

else 
    { 

     mWebview = new WebView(this); 

     mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript 

     final Activity activity = this; 

     mWebview.setWebViewClient(new WebViewClient() { 
      public void onReceivedError(WebView view, int errorCode, String description, String failingUrl) { 
       Toast.makeText(activity, description, Toast.LENGTH_SHORT).show(); 
      } 
     }); 

     mWebview.setWebViewClient(new WebViewClient() { 
      @Override 
      public void onPageFinished(WebView view, String url) { 
       String webUrl = mWebview.getUrl(); 

       mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_userid_inputtext').value ='" + preferences.getString("username", null).toString() + "';" + "})()"); 
       mWebview.loadUrl("javascript:(function() { " + "document.getElementById('cred_password_inputtext').value ='" + preferences.getString("password", null).toString() + "';" + "})()"); 
       mWebview.loadUrl("javascript:(function() { " + "document.getElementById('credentials').submit(); return false;" + "})()"); 
      } 
     }); 

mWebview.loadUrl("http://login.microsoftonline.com"); 
     setContentView(mWebview); 
     Toast.makeText(getApplicationContext(), "Preferences have something", Toast.LENGTH_SHORT).show(); 

    } 
} 
} 

錯誤:

02-14 19:20:06.130  632-632/com.example.PorjectStudent E/AndroidRuntime﹕ FATAL EXCEPTION: main 
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.PorjectStudent/com.example.PorjectStudent.studentEmail}: java.lang.NullPointerException 
     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
     at android.app.ActivityThread.access$600(ActivityThread.java:130) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
     at android.os.Handler.dispatchMessage(Handler.java:99) 
     at android.os.Looper.loop(Looper.java:137) 
     at android.app.ActivityThread.main(ActivityThread.java:4745) 
     at java.lang.reflect.Method.invokeNative(Native Method) 
     at java.lang.reflect.Method.invoke(Method.java:511) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
     at dalvik.system.NativeStart.main(Native Method) 

Caused by: java.lang.NullPointerException 

02-14 19:20:06.130  632-632/com.example.PorjectStudent E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    at com.example.PorjectStudent.studentEmail.onCreate(studentEmail.java:35) 
    at android.app.Activity.performCreate(Activity.java:5008) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                at android.os.Handler.dispatchMessage(Handler.java:99) 
                at android.os.Looper.loop(Looper.java:137) 
                at android.app.ActivityThread.main(ActivityThread.java:4745) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:511) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                at dalvik.system.NativeStart.main(Native Method) 
02-14 19:42:37.971  677-677/com.example.PorjectStudent E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.PorjectStudent/com.example.PorjectStudent.studentEmail}: java.lang.NullPointerException 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
    at android.app.ActivityThread.access$600(ActivityThread.java:130) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
    at android.os.Handler.dispatchMessage(Handler.java:99) 
    at android.os.Looper.loop(Looper.java:137) 
    at android.app.ActivityThread.main(ActivityThread.java:4745) 
    at java.lang.reflect.Method.invokeNative(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:511) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
    at dalvik.system.NativeStart.main(Native Method) 
Caused by: java.lang.NullPointerException 
at com.example.PorjectStudent.studentEmail.onCreate(studentEmail.java:39) 
    at android.app.Activity.performCreate(Activity.java:5008) 
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 
                at android.app.ActivityThread.access$600(ActivityThread.java:130) 
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195) 
                at android.os.Handler.dispatchMessage(Handler.java:99) 
                at android.os.Looper.loop(Looper.java:137) 
                at android.app.ActivityThread.main(ActivityThread.java:4745) 
                at java.lang.reflect.Method.invokeNative(Native Method) 
                at java.lang.reflect.Method.invoke(Method.java:511) 
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553) 
                at dalvik.system.NativeStart.main(Native Method) 

我真的很爲難和解決方案可能是簡單的,但它已經被衝倒我的大腦所有的日子,所以我決定尋求幫助因爲我在幾小時內沒有進步!

感謝所有幫助:)

+1

你可以添加異常堆棧跟蹤到你的問題? – elevine

+0

是你的**完整** stacktracke嗎?有必要添加它,因爲它現在可能沒有顯示細節。 – nKn

+0

到目前爲止,這是完整的東西!沒有更多的東西出現了! –

回答

2

你必須super.onCreate(savedInstanceState);後,立即打電話setContentView(R.layout.student_email);。你把你的SharedPreferences放在中間,你不能那樣做。

作爲一般規則,這兩個句子必須是任何onCreate()方法中的第一句(至少在Activity之內)。

---- ----編輯

public class studentEmail extends Activity { 
private WebView mWebview; 

public static final String PREFS_NAME = "MyPrefsFile"; 

public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.student_email) ; 

    final SharedPreferences preferences = getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE); //This line breaks the code 

    mWebview = new WebView(this); 

    final EditText emEdit = (EditText)findViewById(R.id.editEmail); 
    final EditText passEdit = (EditText)findViewById(R.id.editPass); 
+0

感謝您的回覆,我不知道這一點。 但是,同樣的問題仍在發生。我會在上面發佈完整的代碼,還有其他任何我沒有看到的問題。 –

+0

只需在您自己的問題中點擊編輯並添加您需要的代碼。不要忘記提供堆棧跟蹤,這非常重要。 – nKn

+0

你仍然有代碼,因爲你不應該!看看我在答案中更新的代碼,就是這樣。 – nKn

0

你必須始終啓動您的自定義代碼後setContentView(R.layout...) ;

相關問題