2017-04-14 81 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缓存。