[英]How do I pass in a context from an activity to an ArrayAdapter in another class that's not an activity?
I'm trying to use an array of movies and put them in a listview. 我正在尝试使用一系列电影并将其放入列表视图。 After debugging, the only thing that makes the activity crash is using getContext(). 调试之后,使活动崩溃的唯一方法是使用getContext()。 I tried getApplicationContext() and that also crashed the app. 我尝试了getApplicationContext(),这也使应用程序崩溃了。 I'm not sure what to do. 我不确定该怎么办。 Here's a method in one class. 这是一类的方法。
private void displayMovies(List<Movie> movies) {
ArrayAdapter adapter = new
ArrayAdapter(DisplayMoviesActivity.getContext(),
android.R.layout.simple_list_item_1, movies);
ListView listView = DisplayMoviesActivity.getListView();
}
This is a different class. 这是另一类。
public class DisplayMoviesActivity extends AppCompatActivity {
static ListView displayMoviesView;
static Context context;
public DisplayMoviesActivity(Context context) {
this.context = context;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_movies);
displayMoviesView = (ListView) findViewById(R.id.displayMoviesView);
String searchText = (getIntent().getStringExtra("text"));
RottenTomatoesJSON RTJSON = new RottenTomatoesJSON(this);
RTJSON.searchMovieByName(searchText, 12);
}
public static Context getContext() {
return context;
}
public static ListView getListView() {
return displayMoviesView;
}
}
The app crashes whenever I try to start the activity. 每当我尝试启动活动时,应用程序就会崩溃。 This is the class I use to start the activity. 这是我用来开始活动的课程。
public void searchMovies(View v){
SearchView searchBar = (SearchView) findViewById(R.id.searchView);
if (searchBar.isIconified() || searchBar.getQuery() == null || searchBar.getQuery() == "") {
Context context = getApplicationContext();
CharSequence text = "Please enter movie to search!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
} else {
String searchText = searchBar.getQuery().toString();
Intent intent = new Intent(this, DisplayMoviesActivity.class);
intent.putExtra("text",searchText);
startActivity(intent);
finish();
}
}
I think this is the relevant logcat. 我认为这是相关的日志。
I/art: Background sticky concurrent mark sweep GC freed 2773(224KB)
AllocSpace objects, 0(0B) LOS objects, 17% free, 931KB/1135KB, paused
5.134ms total 40.550ms
W/art: Suspending all threads took: 5.956ms
D/OpenGLRenderer: Render dirty regions requested: true
D/Atlas: Validating map...
I/OpenGLRenderer: Initialized EGL, version 1.4
D/OpenGLRenderer: Enabling debug mode 0
W/EGL_emulation: eglSurfaceAttrib not implemented
W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa638a320, error=EGL_SUCCESS
Remove the constructor with Context
parameter. 使用Context
参数删除构造函数。
public DisplayMoviesActivity(Context context) {
this.context = context;
}
instead, pass the Context
to your object of the displayMovies
method. 而是将Context
传递给displayMovies
方法的对象。 Please provide the code of your class. 请提供您课程的代码。
NOTE 注意
You code makes clear that person who wrote it lacks common android development knowledge. 您的代码清楚地表明,编写该代码的人缺乏通用的android开发知识。
END NOTE 结束注
Example: 例:
Helper helper = new Helper(this);
helper.displayMovies(//);
A close look at the docs for Activity
shows the following inheritance hierarchy: 仔细查看Activity
文档会显示以下继承层次结构:
java.lang.Object
↳ android.content.Context
↳ android.content.ContextWrapper
↳ android.view.ContextThemeWrapper
↳ android.app.Activity
Note that Context
is an ancestor of Activity
. 请注意, Context
是Activity
的祖先。 This means that an Activity
is a Context
. 这意味着Activity
是 Context
。 There is no need for a constructor in an Activity
sublcass which accepts a Context
. 在Activity
不需要接受Context
的构造函数。 (Besides, this constructor will never be called.) (此外,将永远不会调用此构造函数。)
If the code which needs a Context
occurs in a method inside your Activity
subclass, you can just use this
to refer to the current Activity
object (which is also a Context
): 如果需要Context
的代码出现在Activity
子类内部的方法中,则可以使用this
来引用当前的Activity
对象(也是Context
):
ArrayAdapter adapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, movies);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.