2017-04-14 86 views
1

我敢肯定,我昨晚沒有做錯或錯誤的事情。昨天晚上,我仍然能夠Toast.makeText(getApplicationContext(), "Some String", Toast.LENGTH_SHORT);突然getApplicationContext(),getApplication(),this.getApplicationContext()丟失

但今天早上,當我嘗試再次調試它以確保應用程序仍然像上次測試一樣工作時,它顯示一個錯誤。 「無法解析...」。

當我檢查我的MainActivity.java代碼時,getApplicationContext()是紅色文本。 (我使用素材ui主題和紅色文本表示錯誤)。然後我嘗試重新輸入它。但是,它沒有顯示getApplicationContext()或那種類型的自動完成。我不確定我的代碼有什麼問題。我在新項目中嘗試它,並且工作正常。

這裏是我的文件代碼:

AndroidManifest.xml中

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

    <uses-permission android:name="android.permission.INTERNET" /> 

    <application 
      android:allowBackup="true" 
      android:icon="@mipmap/ic_launcher" 
      android:label="@string/app_name" 
      android:supportsRtl="true" 
      android:theme="@style/AppTheme"> 

     <activity android:name=".MainActivity"> 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN"/> 

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

</manifest> 

activity_main.xml中

<?xml version="1.0" encoding="utf-8"?> 
<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:orientation="vertical" 
       tools:context="com.belajar.belajar1.MainActivity" android:weightSum="1"> 

    <EditText android:layout_width="match_parent" 
       android:layout_height="wrap_content" 
       android:textDirection="firstStrong" 
       android:hint="Title" 
       android:id="@+id/inputTitle" 
      /> 

    <EditText 
      android:layout_width="match_parent" 
      android:layout_height="0sp" 
      android:layout_weight="1" 
      android:gravity="top" 
      android:hint="Your Note" 
      android:id="@+id/inputContent"/> 

    <Button 
      android:layout_width="match_parent" 
      android:layout_height="wrap_content" 
      android:text="SAVE" 
      android:onClick="cmdProcess"/> 
</LinearLayout> 

MainActivity.java

package com.belajar.belajar1; 

import android.widget.Toast; 
import android.os.Bundle; 
import android.support.v7.app.AppCompatActivity; 
import android.view.View; 
import android.widget.EditText; 
import com.google.firebase.database.*; 

import java.util.HashMap; 
import java.util.Map; 

public class MainActivity extends AppCompatActivity 
{ 
    DatabaseReference root = FirebaseDatabase.getInstance().getReference(); 
    DatabaseReference TestLab1; 
    EditText mInputTitle, mInputContent; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     mInputTitle = (EditText)findViewById(R.id.inputTitle); 
     mInputContent = (EditText)findViewById(R.id.inputContent); 

     Toast.makeText(getApplicationContext(), "Some String", Toast.LENGTH_SHORT).show(); 
    } 

    public void cmdProcess(View v) 
    { 
     Map<String, Object> conditionalMap = new HashMap<>(); 
     Map<String, Object> map = new HashMap<String, Object>(); 

     map.put(root.push().getKey(), 
       new Note(mInputTitle.getText().toString(),mInputContent.getText().toString())); 

     if (TestLab1 == null) 
     { 
      conditionalMap.put("TestLab1", map); 
      root.updateChildren(conditionalMap); 
     } 
     else 
     { 
      TestLab1.updateChildren(map); 
     } 
    } 

    @Override 
    protected void onStart() 
    { 
     super.onStart(); 

     root.addValueEventListener(new ValueEventListener() 
     { 
      @Override 
      public void onDataChange(DataSnapshot dataSnapshot) 
      { 
       if (!dataSnapshot.hasChild("TestLab1")) 
       { 
        TestLab1 = null; 
       } 
       else if (dataSnapshot.hasChild("TestLab1")) 
       { 
        TestLab1 = dataSnapshot.child("TestLab1").getRef(); 
       } 
      } 

      @Override 
      public void onCancelled(DatabaseError databaseError) 
      { 

      } 
     }); 
    } 
} 

截圖證據: Cannot resolve method ScreenShot Evidence

+1

試圖重建項目? – Nerd

回答

1

我看到您的Android工作室。 有時候會出現這個奇怪的問題,項目乾淨爲我解決了這個問題。

如果你確定你的代碼是正確的,這似乎是一個IDE問題,請嘗試乾淨的項目和無效的IDE緩存。