2014-01-19 3 views
-1

Это мой код для MainActivity.java Здесь я создаю очень простой LoginApp. При запуске приложения, он дает "к сожалению, перестал работать ERROR ..."Приложение, к сожалению, перестало работать

package com.example.loginapp; 
import android.os.Bundle; 
import android.app.Activity; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.Toast; 

public class MainActivity extends Activity implements OnClickListener { 
EditText t1,t2; 
Button b1; 

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

    b1= (Button) b1.findViewById(R.id.button1); 
    t1=(EditText) t1.findViewById(R.id.editText1); 
    t2=(EditText) t2.findViewById(R.id.editText2); 

} 

@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 void onClick(View arg0) 
{ 
    //String user;  
    //user = t1.setText("user"); 

    if (arg0.findViewById(R.id.button1)==b1) 
    { 

    if ((boolean)((t1.getText().toString()=="user")) && (t2.getText().toString()=="user")) 

    { 
     Toast.makeText(this, "Login Sucessful", Toast.LENGTH_SHORT).show(); 

    } 
    else 
    { 
     Toast.makeText(this, "Sorry", Toast.LENGTH_SHORT).show(); 
     t1.setText(""); 
     t2.setText(""); 

    } 
    } 
} 
} 

Ниже код activity_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:gravity="center" 
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=".MainActivity" > 

<TextView 
    android:id="@+id/textView1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignTop="@+id/editText1" 
    android:text="Username"   
    android:textAppearance="?android:attr/textAppearanceLarge" /> 

<TextView 
    android:id="@+id/textView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignLeft="@+id/textView1" 
    android:layout_centerVertical="true" 
    android:text="Password" 
    android:textAppearance="?android:attr/textAppearanceLarge" /> 



<EditText 
    android:id="@+id/editText1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentLeft="true" 
    android:layout_alignParentTop="true" 
    android:layout_marginLeft="115dp" 
    android:layout_marginTop="22dp" 
    android:ems="10" > 


</EditText> 

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

<Button 
    android:id="@+id/button1" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/textView2" 
    android:layout_marginTop="46dp" 
    android:layout_toRightOf="@+id/textView2" 
    android:clickable="true" 
    android:onClick="onClick" 
    android:text="Login" /> 

</RelativeLayout> 

код для AndroidManifest.xml

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

<uses-sdk 
    android:minSdkVersion="14" 
    android:targetSdkVersion="18" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.loginapp.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> 
</application> 

</manifest> 
+0

Здравствуйте, Добро пожаловать в сообщество StackOverflow, можете ли вы указать, какое полное исключение или ошибка вы получите и на какой строке? – Marek

+0

Прочитайте logcat, Luke .... –

+0

@Marek: Сэр, я не получаю ошибок компиляции. Некоторые предупреждения сохраняются, но не относятся к этой ошибке. – linthum

ответ

0

Привет пожалуйста, заполните форму onCreate как

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

    b1= (Button) findViewById(R.id.button1); 
    t1=(EditText) findViewById(R.id.editText1); 
    t2=(EditText) findViewById(R.id.editText2); 
} 

Это поможет вам. После изменения кода он отлично работает для меня. Пожалуйста, попробуйте и дайте мне знать.

+0

Спасибо большое Джеба. Это очень помогло мне. Мое приложение работает правильно !!! – linthum

+0

Не могли бы вы рассказать мне, почему это вызвало ошибку? – linthum

+0

Вы можете создать компонент Button, как показано ниже: b1 = (кнопка) findViewById (R.id.button1); Но вы пытаетесь создать компонент Button с помощью Button bl; b1 = (кнопка) b1.findViewById (R.id.button1); Элемент Button b1 имеет значение null. Но вы пытаетесь использовать b1.findViewById (R.id.button1) (т.е. null.findViewById (R.id.button1)) Так что только приложение разбито. Спасибо – Jeba

Смежные вопросы