2017-05-19 181 views
0

我對android編程很陌生。 我在將Onclicklistener添加到按鈕時遇到了很多麻煩。 當我刪除該部分時,該應用程序運行良好。 當我添加它時,應用程序立即崩潰。 這裏是我的主要活動代碼:添加onclicklistener時應用程序崩潰

public class LoginActivity extends Activity { 

    Button pwrecovery; 
    Context context = getApplicationContext(); 
    CharSequence text ="hello toast!"; 
    int duration = Toast.LENGTH_LONG; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     //set up notitle 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     //set up full screen 
     getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, 
       WindowManager.LayoutParams.FLAG_FULLSCREEN); 
     setContentView(R.layout.activity_login); 

    pwrecovery=(Button) findViewById(R.id.pwrecovery); 
     pwrecovery.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Toast toast = Toast.makeText(context, text, duration); 
       toast.show(); 
      } 
     }); 

沒有人有任何想法,爲什麼它呢?

在此先感謝你們。

<LinearLayout 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:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    tools:context=".LoginActivity" 
    android:orientation="vertical" 
    android:background="@color/blue"> 


    <ImageView 
     android:layout_width="99dp" 
     android:layout_height="91dp" 
     android:id="@+id/imageView" 
     android:src="@drawable/logo" 
     android:layout_gravity="center" 
     android:background="#02496b" 
     android:layout_marginTop="10dp"/> 

    <TextView 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textAppearance="?android:attr/textAppearanceMedium" 
     android:text="@string/quote_eng" 
     android:id="@+id/textView" 
     android:layout_gravity="center" 
     android:layout_marginTop="15dp" 
     android:singleLine="true" 
     android:textColor="#ffffffff" /> 



    <EditText 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:inputType="textEmailAddress" 
     android:ems="10" 
     android:textColorHint="#ffffff" 
     android:textColor="#ffffff" 
     android:id="@+id/editText" 
     android:capitalize="none" 
     android:hint="E-mail" 
     android:singleLine="true" 
     android:layout_marginTop="30dp" 
     android:layout_gravity="center" 
     android:paddingLeft="15dp" 
     /> 

    <EditText 
     android:layout_width="250dp" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     android:inputType="textPassword" 
     android:ems="10" 
     android:id="@+id/editText2" 
     android:hint="Password" 
     android:fontFamily="" 
     android:layout_marginTop="5dp" 
     android:layout_gravity="center" 
     android:textColorHint="#ffffff" 
     android:paddingLeft="15dp"/> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/sign_in" 
     android:text="@string/sign_in" 
     android:textColor="#ffffff" 
     android:background="@drawable/button_signin4" 
     android:layout_gravity="center" 
     android:layout_marginTop="25dp"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:textColor="#ffffff" 
     android:paddingLeft="35dp" 
     android:id="@+id/sign_in_fd" 
     android:background="@drawable/button_fblogin_small2" 
     android:text="@string/logfb" 
     android:textSize="13dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="35dp"/> 

    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:id="@+id/sign_in_fb" 
     android:background="@drawable/button_glogin_small" 
     android:text="@string/logg" 
     android:textColor="#ffffff" 
     android:paddingLeft="35dp" 
     android:layout_gravity="center" 
     android:layout_marginTop="10dp"/> 

    <RelativeLayout 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:gravity="bottom" 
    > 




    <Button 
     android:id="@+id/pwrecovery" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="@string/lost_password" 
     android:textColor="#ffffff" 
     android:gravity="left" 
     android:textSize="18dp" 
     android:clickable="true"/> 


    <TextView 
     android:id="@+id/signup" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:text="@string/sign_up" 
     android:textColor="#ffffff" 
     android:gravity="right" 
     android:textSize="18dp" 
     android:clickable="true"/> 
    </RelativeLayout> 




    </LinearLayout> 
+1

你可以從崩潰中發佈logcat嗎?尋找一條開始'E /'的行,並且包含短語'Caused By'。我的猜測是'findViewById()'找不到'R.id.pwrecovery'。 –

+3

你可以顯示你的activity_login.xml嗎? –

+0

爲什麼你設置'Context context = getApplicationContext();'。不好的方法。刪除'getApplicationContext()'和oncreate section聲明'context = getApplicationContext()'並確保你的ID .show xml –

回答

0

,你是無法發佈logcat的

​​
+1

謝謝,修復它!所以問題出現在我的聲明中? – jonamar

+0

似乎你還沒有正確地聲明'context'。 – sam

+0

@jonamar你能接受這個答案嗎? –

1

請使用替代方面完全合格的活動對象試試這一個。

pwrecovery.setOnClickListener(new View.OnClickListener() { 
     @Override 
     public void onClick(View v) { 
      Toast toast = Toast.makeText(LoginActivity .this, text, duration); 
      toast.show(); 
     } 
    }); 
0

相反,你也可以使用這樣的吐司:

Toast.makeText(getApplicationContext(), "Please Enter Your Code & Passkey", Toast.LENGTH_LONG).show(); 
0

只需從

Toast toast = Toast.makeText(context, text, duration); 
      toast.show(); 

只是改變要

Toast toast = Toast.makeText(LoginActivity.this, text, Toast.LENGTH_LONG).show(); 
0

您之前稱爲一個實例方法實例已完全創建,而y OU已經存儲作爲變量的成員

public class LoginActivity extends Activity { 

    Context context = getApplicationContext(); 

活動語境對象。該變量不是必需的,應該替換爲LoginActivity.this

相關問題