0

我開發登錄頁面,它表明,不幸的是LoginC已停止工作的錯誤。以下消息 logcat的顯示錯誤:不幸的是<應用程序名稱>已停止在Android模擬器

03-31 07:24:35.896: D/AndroidRuntime(1263): Shutting down VM 
03-31 07:24:35.906: W/dalvikvm(1263): threadid=1: thread exiting with uncaught exception (group=0xb3a1bba8) 
03-31 07:24:35.946: E/AndroidRuntime(1263): FATAL EXCEPTION: main 
03-31 07:24:35.946: E/AndroidRuntime(1263): Process: com.example.loginc, PID: 1263 
03-31 07:24:35.946: E/AndroidRuntime(1263): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.loginc/com.example.loginc.MainActivity}: java.lang.NullPointerException 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.ActivityThread.access$800(ActivityThread.java:135) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.os.Handler.dispatchMessage(Handler.java:102) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.os.Looper.loop(Looper.java:136) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.ActivityThread.main(ActivityThread.java:5017) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at java.lang.reflect.Method.invokeNative(Native Method) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at java.lang.reflect.Method.invoke(Method.java:515) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at dalvik.system.NativeStart.main(Native Method) 
03-31 07:24:35.946: E/AndroidRuntime(1263): Caused by: java.lang.NullPointerException 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at com.example.loginc.MainActivity.onCreate(MainActivity.java:35) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.Activity.performCreate(Activity.java:5231) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159) 
03-31 07:24:35.946: E/AndroidRuntime(1263):  ... 11 more 

MainActivity.java

public class MainActivity extends ActionBarActivity { 

    private EditText username=null; 
    private EditText password=null; 
    private TextView attempts; 
    private Button login; 
    int counter = 3; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     username = (EditText)findViewById(R.id.editText1); 
     password = (EditText)findViewById(R.id.editText2); 
     attempts = (TextView)findViewById(R.id.textView5); 
     attempts.setText(Integer.toString(counter)); 
     login = (Button)findViewById(R.id.button1); 

    } 

    public void login(View view){ 
      if(username.getText().toString().equals("admin") && 
      password.getText().toString().equals("admin")){ 
      Toast.makeText(getApplicationContext(), "Redirecting...", 
      Toast.LENGTH_SHORT).show(); 
     }  
     else{ 
      Toast.makeText(getApplicationContext(), "Wrong Credentials", 
      Toast.LENGTH_SHORT).show(); 
      attempts.setBackgroundColor(Color.RED); 
      counter--; 
      attempts.setText(Integer.toString(counter)); 
      if(counter==0){ 
      login.setEnabled(false); 
      } 

     } 

    } 

    @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; 
     } 
    } 

} 

上面寫的mainactivity.java文件中的代碼

和下面細節被寫入fragment_main.xml文件

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context="com.example.loginc.MainActivity$PlaceholderFragment" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="18dp" 
    android:text="@string/hello_world" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_below="@+id/textView1" 
    android:layout_marginTop="50dp" 
    android:text="@string/username" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/textView2" 
    android:layout_marginLeft="32dp" 
    android:layout_toRightOf="@+id/textView2" 
    android:ems="10" 
    android:inputType="text" > 

    <requestFocus /> 
</EditText> 

<TextView 
    android:id="@+id/textView3" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView2" 
    android:layout_below="@+id/textView2" 
    android:layout_marginTop="38dp" 
    android:text="@string/password" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<EditText 
    android:id="@+id/editText2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/textView3" 
    android:layout_alignLeft="@+id/editText1" 
    android:ems="10" 
    android:inputType="textPassword" /> 

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/editText2" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="94dp" 
    android:onClick="login" 
    android:text="@string/Login" /> 

<TextView 
    android:id="@+id/textView4" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView3" 
    android:layout_below="@+id/textView3" 
    android:layout_marginLeft="30dp" 
    android:layout_marginTop="48dp" 
    android:text="@string/attempts" 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

<TextView 
    android:id="@+id/textView5" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignRight="@+id/textView1" 
    android:layout_alignTop="@+id/textView4" 
    android:text="TextView" /> 


</RelativeLayout> 
+0

請將您的Java代碼和您的佈局XML一起發佈。 –

+1

發佈MainActivity.java – Hariharan

+0

也發佈您的activity_main.xml。 – Hariharan

回答

0

一個空PointerException意味着你竭力試圖o不要對被設置爲一個null值的操作(基本上未初始化)。你可以想象一個沒有像未編程的信用卡一樣的設置的價值,你可以隨身攜帶,但如果你嘗試使用它,它將無法工作。那麼,你的onCreate函數中什麼值仍然可以爲空?

我強烈懷疑該行35是這一個,如果不是的話,那麼這個答案將不適用。你的錯誤是第35行,或至少涉及到:

attempts.setText(Integer.toString(counter)); 

我假設attempts是有點空。這個集合在哪裏?

attempts = (TextView)findViewById(R.id.textView5); 

好了,讓我們來看看文檔findViewById

public View findViewById (int id)

Added in API level 1

Finds a view that was identified by the id attribute from the XML that was processed in onCreate(Bundle).

Returns: The view if found or null otherwise.

所以,如果認爲無法找到某種方式,然後嘗試可能爲空。我們如何判斷它是否爲空?讓我們嘗試一個日誌聲明,包括後findViewById

Log.v("Main","attempts="+attempts); 

好這個權利,我們假設它實際上是空值。那我們做什麼?那麼,根據文檔,這意味着它沒有被發現。那麼,讓我們來看看的setContentView聲明:

setContentView(R.layout.activity_main); 

好了,您認爲應該在activity_main.xml。我看到它在您的代碼中,但您稱爲文件fragment_main.xml。底線是,您試圖從錯誤的XML文件中獲取attempts值。好的,那麼這裏有什麼解決方案?

  1. 最好的辦法是管理這個片段中的所有內容,並列出fragment_main.xml。這可能需要一些工作,但最終會給你最好的表現。
  2. 如果您確定您永遠不會想要使用片段(不支持平板電腦,屏幕旋轉表現不佳,以及其他後果可能跟隨),那麼你可以剛剛替補R.layout.fragment_mainsetContentView

在任何情況下,選擇其中的一個,和你的問題就會消失。

如果您選擇第一個,您需要將代碼放入onCreateView,並通過根視圖(What onCreateView returns)引用。類似於view.findViewById

+0

我知道你想說什麼..如果我選擇第一個選項,那麼將需要什麼樣的變化? – user3480948

+0

好的......你能告訴我一下fragment_main.xml和activity_main.xml的區別嗎。這兩個文件都是爲了什麼? – user3480948

0
那些ID(一個或多個)是在 fragment_main.xml,而不是在 activity_main.xml,因此NPE

移動這些到onCreateView

username = (EditText)findViewById(R.id.editText1); 
    password = (EditText)findViewById(R.id.editText2); 
    attempts = (TextView)findViewById(R.id.textView5); 
    attempts.setText(Integer.toString(counter)); 
    login = (Button)findViewById(R.id.button1); 

另外,在那裏使用rootView.findViewById

相關問題