简体   繁体   English

如何将上下文从活动传递到不是活动的另一个类中的ArrayAdapter?

[英]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 . 请注意, ContextActivity的祖先。 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.

相关问题 如何将arrayadapter传递给另一个活动? - How can I pass an arrayadapter to another activity? 如何从 arrayadapter 中获取值以将其传递给另一个活动 - How can I get value from an arrayadapter to pass it another activity 我如何将价值从一项活动传递到另一项活动 - How do i pass value from one activity to another activity 如何将信息从一个活动的EditText传递到另一个活动,以将URL加载到该新活动的WebView中? - How do I pass info from one activity's EditText to another for the url to be loaded in that new activity's WebView? 如何从主要活动中调用ArrayAdapter对象的函数? - How do I call an ArrayAdapter object's function from my main activity? 如何使用arrayAdapter中的intent进入第三个活动? - How do I use intent from arrayAdapter to go into a third activity? 如何从活动类中获取上下文? - How do I get the context from within an activity class? 如何将ArrayList中的类从一个活动传递给另一个活动 - How to pass Class of Class in ArrayList from one Activity to Another Activity 如何从另一个活动调用 ArrayAdapter 以使用 strings.xml 数组从 ArrayAdapter 中删除项目? - How can I call an ArrayAdapter from another activity to delete item from ArrayAdapter with strings.xml array? 将上下文从活动内的片段传递到另一个类 - Pass the context from a fragment inside an activity to another class
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM