Android 如何在另一个';这不是一项活动吗?

Android 如何在另一个';这不是一项活动吗?,android,Android,我正在尝试使用一系列电影,并将它们放在列表视图中。 调试后,唯一导致活动崩溃的是使用getContext()。我尝试了getApplicationContext(),这也使应用程序崩溃。我不知道该怎么办。这里有一个类中的方法 private void displayMovies(List<Movie> movies) { ArrayAdapter adapter = new ArrayAdapter(DisplayMoviesActivity.getContext

我正在尝试使用一系列电影,并将它们放在列表视图中。 调试后,唯一导致活动崩溃的是使用getContext()。我尝试了getApplicationContext(),这也使应用程序崩溃。我不知道该怎么办。这里有一个类中的方法

private void displayMovies(List<Movie> movies) {
     ArrayAdapter adapter = new
     ArrayAdapter(DisplayMoviesActivity.getContext(),
         android.R.layout.simple_list_item_1, movies);
     ListView listView = DisplayMoviesActivity.getListView();
}
每当我尝试启动活动时,应用程序就会崩溃。 这是我用来开始活动的课程

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/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

使用
上下文
参数删除构造函数

public DisplayMoviesActivity(Context context) {
    this.context = context;
}
相反,将
上下文
传递给
displaymoves
方法的对象。 请提供您的班级代码

注意

您的代码清楚地表明,编写它的人缺乏常见的android开发知识

结束注释

示例:

Helper helper = new Helper(this);
helper.displayMovies(//);

使用
上下文
参数删除构造函数

public DisplayMoviesActivity(Context context) {
    this.context = context;
}
相反,将
上下文
传递给
displaymoves
方法的对象。 请提供您的班级代码

注意

您的代码清楚地表明,编写它的人缺乏常见的android开发知识

结束注释

示例:

Helper helper = new Helper(this);
helper.displayMovies(//);
仔细查看显示了以下继承层次结构:

java.lang.Object
   ↳    android.content.Context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity
请注意,
上下文
活动
的祖先。这意味着
活动
是一个
上下文
。在接受
上下文的
活动
子容器中不需要构造函数。(此外,永远不会调用此构造函数。)

如果需要
上下文
的代码出现在
活动
子类中的某个方法中,您可以使用
引用当前
活动
对象(它也是
上下文
):

仔细查看显示了以下继承层次结构:

java.lang.Object
   ↳    android.content.Context
       ↳    android.content.ContextWrapper
           ↳    android.view.ContextThemeWrapper
               ↳    android.app.Activity
请注意,
上下文
活动
的祖先。这意味着
活动
是一个
上下文
。在接受
上下文的
活动
子容器中不需要构造函数。(此外,永远不会调用此构造函数。)

如果需要
上下文
的代码出现在
活动
子类中的某个方法中,您可以使用
引用当前
活动
对象(它也是
上下文
):


当应用程序崩溃时,显示logcat欢迎顶部。我应该知道你的id是如何使用函数的函数的,我们知道类是对象的描述,所以,在设置方法时,函数非常有用,当应用程序崩溃时,显示日志。我应该知道你的id如何处理函数的函数,我们知道类是对象的描述,所以,在setter方法中,函数是非常有用的,我不能通过静态引用它context@dirtysocks45你在哪里使用方法
displayMovies
?我相信这不是一个活动。我在一个小组里工作,是别人做的。@dirtysocks45你应该在那个类中传递
上下文,然后在那里使用它。我不能通过静态引用它context@dirtysocks45你在哪里使用方法
displayMovies
?我相信这不是一个活动。我在一个小组里工作,是别人做的。@dirtysocks45你应该在那个类中传递
上下文
,然后在那里使用它。