我正在使用android studio 2.2.2從Android Tutorial for Beginners 8 # wrap_content, fill_parent, Password Field and Toast in Android 學習android。無法解析getActivity()和getApplicationContext()
我已經做了相同的教程中提到,但無法理解爲什麼getApplicationContext()和getActivity方法不可解析。
我試過getActivity(),getActivity()。getApplicationContext(),MainActivity.this ....等等。但Toast.makeText方法無法識別上下文。
任何人都可以告訴我什麼是確切的問題?我該如何解決它?
下面是我的代碼:
package com.example.programingknowledge.myfirstapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private EditText pass_word;
private Button btn_sbm;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
pass_word = (EditText) findViewById(R.id.editText);
btn_sbm = (Button) findViewById(R.id.button);
final CharSequence passText = pass_word.getText().toString();
btn_sbm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), passText, Toast.LENGTH_SHORT).show();
}
});
}
}
AndroidManifest.xml中
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.programingknowledge.myfirstapp">
<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"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
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.example.programingknowledge.myfirstapp.MainActivity"
android:background="@android:color/black">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="@+id/editText" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:id="@+id/button"
android:layout_below="@+id/editText"
android:layout_centerHorizontal="true" />
</RelativeLayout>
包名是哪裏? – Umer
getActivity()將在片段中工作 – Redman
'getActivity()'對於Activity沒有意義。它已經是一個活動。對於getApplicationContext(),最好使用Activity Context:this。 –