2014-04-08 90 views
0

你好,我是新的android系統的深化發展,我做了一個小測試,但它給我的錯誤: 「不幸的是Android的測試已經停止」不幸「APP_NAME」已停止

我找到一些類似的主題在這裏,但沒有一個解決我的問題,這裏是我的代碼:

主要活動:

public class MainActivity extends ActionBarActivity { 

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

     if (savedInstanceState == null) { 
      getSupportFragmentManager().beginTransaction() 
        .add(R.id.container, new PlaceholderFragment()).commit(); 
     } 

     final EditText username = (EditText) findViewById(R.id.username); 
     final EditText password = (EditText) findViewById(R.id.password); 
     Button button = (Button) findViewById(R.id.button1); 

     button.setOnClickListener(new View.OnClickListener() { 

      public void onClick(View v) { 
       if(checkLogin(username.getText().toString(),password.getText().toString())){ 
        Intent i = new Intent(MainActivity.this, HelloActivity.class); 
        startActivity(i); 
       } 
       else{ 
        username.setText(""); 
        password.setText(""); 
       } 

      } 
     }); 
    } 

    private boolean checkLogin(String u,String p){ 
     if (u == "hello" && p == "world") 
      return true; 
     else return 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; 
     } 
    } 

} 

主要佈局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:id="@+id/login" 
    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.androidtest.MainActivity$PlaceholderFragment" > 

    <EditText 
     android:id="@+id/username" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentLeft="true" 
     android:layout_alignParentTop="true" 
     android:layout_marginLeft="16dp" 
     android:ems="10" 
     android:hint="username" > 

     <requestFocus /> 
    </EditText> 

    <EditText 
     android:id="@+id/password" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignRight="@+id/username" 
     android:layout_below="@+id/username" 
     android:ems="10" 
     android:hint="password" 
     android:inputType="textPassword" /> 

    <Button 
     android:id="@+id/button1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/username" 
     android:layout_below="@+id/password" 
     android:text="Button" /> 

</RelativeLayout> 

第二acivity

package com.androidtest; 

import android.app.Activity; 
import android.os.Bundle; 

public class HelloActivity extends Activity { 

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

    } 

} 

其佈局

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <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="193dp" 
     android:text="Afin akhay lpchiiwr" 
     android:textAppearance="?android:attr/textAppearanceLarge" /> 

</RelativeLayout> 

最後清單文件:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="com.androidtest" 
    android:versionCode="1" 
    android:versionName="1.0" > 

    <uses-sdk 
     android:minSdkVersion="8" 
     android:targetSdkVersion="19" /> 

    <application 
     android:allowBackup="true" 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" 
     android:theme="@style/AppTheme" > 
     <activity 
      android:name="com.androidtest.MainActivity" 
      android:label="@string/app_name" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name="com.androidtest.HelloActivity"></activity> 
    </application> 

</manifest> 

感謝名單

+1

請發佈您的logcat。 – dharms

+0

您需要發佈您收到的錯誤。查找一些調試工具。 Eclipse附帶一個。 – jedgard

+1

儘管它不會導致錯誤,但是if(u ==「hello」&& p ==「world」)'不正確。這比較對象,而不是內容。要查看兩個字符串是否相同,請使用'if(u.equals(「hello」)&& p.equals(「world」)'。 – dharms

回答

1

您已經發布第一個XML塊看起來像是從fragment_main.xml文件的。如果是這樣,那麼你的問題是,findViewById()正在尋找活動中的已被充氣到碎片中的視圖。如果你不需要片段,然後將該XML移動到activity_main.xml並刪除片段的代碼。否則,將視圖初始化和方法移到你的Fragment中。

而且,正如Kedarnath指出的,您應該使用String.equals()方法來比較字符串。

+0

這很像magic,thanx! – FrankelStein

0

首先,讓我告訴你,你是比較String以錯誤的方式,

private boolean checkLogin(String u,String p) 
{ 
     if (u == "hello" && p == "world")  // Wrong way 
      return true; 
     else return false; 
} 

您需要以下方式來比較字符串,

private boolean checkLogin(String u,String p) 
{ 
     if (u.equals("hello") && p.equals("world"))  // Right way 
      return true; 
     else return false; 
}