任何人都可以告诉我有关Context的内容吗?我知道我们需要使用上下文进行UI修改.
有人可以向我简要介绍一下它的含义吗?我看过像getApplicationContext(),getBaseContext()?他们之间有什么区别?
可能重复:
Android中的上下文是什么?
我想知道Android的Context是什么,以及为什么需要它.我知道它与课程有关,每个班级都有独特的背景.我在一些代码中看到,在调用另一个类的方法时会传递一个Context.我不明白为什么需要它.请帮忙.
我很新的Android开发,我试图创建一个视图,它可以很容易地完成alloc,然后initWithFrame ...在对象-与可可触摸,但是在Java中,它使用的new ..()方法,我只能坚持定义变量context,的参数LinearLayout().
我看到有些人this用作参数,即 new LinearLayout(this),但是我不明白这个论点实际上做了什么,如果有人可以给我一些关于如何作为论点的指导,我将不胜感激.
LinearLayout layout = new LinearLayout(context);
Run Code Online (Sandbox Code Playgroud)
应该context是什么?我该如何定义它?它有什么作用?我应该分配什么价值?
可能重复:
Android中的上下文是什么?
任何人都可以告诉我关于android中使用的"上下文"术语.我想知道究竟是什么意思,因为这是我以前在很多地方看到的东西.
我发现它是一个类: - "关于应用程序环境的全局信息的接口",但我现在还不太清楚.
for Instance:public GetCurrentLocation(Context context){this.context = context; }
谢谢,大卫
可能重复:
Android中的上下文是什么?
我认为这是一个非常有趣的问题:Android中的"上下文"含义/概念是什么?
几乎任何事物/对象都需要上下文才能正常工作.什么是"背景"代表?整数?一块内存?? 要么 .....
可能重复:
Android中的上下文是什么?
大家!有人可以向我解释一下Android中的上下文究竟是什么?我不明白android开发者网站上的定义,我必须将它们用于GridView布局.
任何帮助将非常感激.谢谢.
我目前正在开发 Android 应用程序,并处理序列化文件。我有一个方法可以将 Java 对象序列化到带有 Context 参数的文件中。在另一个类中工作时,我可以检索有效的 Context 对象,但是当我在扩展 AppCompatActivity 的类上时,我不太确定该怎么做。我尝试使用 getApplicationContext() 但这仍然为我提供了 Context 的空值。
这是我迄今为止所掌握的基础:
public class BookView extends AppCompatActivity {
private static Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_book);
Book book = Browse.books.get(getIntent().getIntExtra("index", 0));
Browse.book = book;
setUpScreen(book);
context = getApplicationContext();
}
private void setUpChapters(Book book){
ListView chapters = (ListView) findViewById(R.id.chapters);
ChapterAdapter adapter = new ChapterAdapter(this, R.layout.row, book.getChapters());
chapters.setAdapter(adapter);
if (context != null) {
book.serialize(context);
}
else {
System.out.println("Null bookview context");
}
chapters.setOnItemClickListener(new AdapterView.OnItemClickListener() { …Run Code Online (Sandbox Code Playgroud) 我通过互联网查看了很多android教程.在这些教程中,他们this在各处使用上下文.我知道thisJava中的关键字意味着什么,但我不能this在Android编程中使用关键字.例如,在AlertDialog.Builderdeveloper.android.com网站上,在Context的参数中只有一个引用,但我不知道这this意味着什么.
可能重复:
活动上下文和应用程序上下文之间的差异
何时使用Context和Activity.我已经阅读了使用Context和Activity作为参数的代码,如下面的构造函数,请在此清除我
public AmazedView(Context context, Activity activity) {
super(context);
mActivity = activity;
// init paint and make is look "nice" with anti-aliasing.
mPaint = new Paint();
mPaint.setTextSize(14);
mPaint.setTypeface(mFont);
mPaint.setAntiAlias(true);
// setup accelerometer sensor manager.
mSensorManager = (SensorManager) activity.getSystemService(Context.SENSOR_SERVICE);
// register our accelerometer so we can receive values.
// SENSOR_DELAY_GAME is the recommended rate for games
mSensorManager.registerListener(mSensorAccelerometer, SensorManager.SENSOR_ACCELEROMETER,
SensorManager.SENSOR_DELAY_GAME);
// setup our maze and marble.
mMaze = new Maze(mActivity);
mMarble = new Marble(this);
// load array from /res/values/strings.xml
mStrings = …Run Code Online (Sandbox Code Playgroud) package com.example.tictactoeshowgrid;
import android.os.Bundle;
import java.io.*;
import android.widget.Toast;
import android.content.*;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Date;
import android.content.Context;
public class ImportOBJ {
protected void onCreate(String filename) {
try
{
FileInputStream fis = openFileInput(filename);
BufferedReader reader = new BufferedReader(new InputStreamReader(fis));
String line = null, input="";
while ((line = reader.readLine()) != null)
input += line;
reader.close();
fis.close();
//toast("File successfully loaded.");
//return input;
}
catch (Exception ex)
{
//toast("Error loading file: " + ex.getLocalizedMessage());
//return "";
}
}
} …Run Code Online (Sandbox Code Playgroud)