从一项活动扩展另一个类使用方法 [英] Using a method from another class that Extends from an Activity

查看:164
本文介绍了从一项活动扩展另一个类使用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须使用来自从活动来延长另一个类的方法的问题。
我有一个code,需要在每一个活动重复,所以我创建了一个类与此code和我得到一个错误,当我tryied使用其他类中的方法
对于exmple:

第一类:

 公共类先伸出活动
    公共无效的onCreate(){
        第二个S =新的第二();
        s.myMethod();
    }

第二个类:

 公共类第二扩展活动
    公共无效myMethod的(){}

这个方式,我得到NullPointerExption。

第一类:

 公共类先伸出活动
    公共无效的onCreate(){
        上下文的背景下= getApplicationContext();
        第二S =((第二)上下文);
        s.myMethod(上下文);
    }

第二个类:

 公共类第二扩展活动
    上下文语境;
    公共无效myMethod的(上下文的背景下){
        this.context =背景;
    }

这个方式,我得到的InvocationTargetException。

这是我的code:

MainActivity - 第一类:

 公共类MainActivity延伸活动{    @覆盖
    保护无效的onCreate(捆绑savedInstanceState){
        super.onCreate(savedInstanceState);
        的setContentView(R.layout.activity_main);
    } //的onCreate
    公共无效ClickMe(查看V){
        Toast.makeText(这一点,ClickMe,Toast.LENGTH_SHORT).show();
        上下文的背景下= getApplicationContext();
        menuSetup MYMENU =((menu​​Setup)上下文);
        myMenu.SetMenuListView();    }
    @覆盖
    公共布尔onCreateOptionsMenu(菜单菜单){
        //充气菜单;如果是present这增加了项目操作栏。
        。getMenuInflater()膨胀(R.menu.main,菜单);
        返回true;
    }    公共布尔onOptionsItemSelected(菜单项项){//动作栏按钮
        开关(item.getItemId()){
        案例R.id.action_settings:
            SlidingDrawer角=(SlidingDrawer)findViewById(R.id.slidingDrawer1);
            myS.animateToggle();
            打破;
        }
        返回super.onOptionsItemSelected(项目);
    }
}

menulList - 二等:

 公共类menuSetup延伸活动{
    MenuAdapter适配器;
    ListView控件myList中;
    INT myPosiition;    公共无效SetMenuListView(){
        myList中=(ListView控件)findViewById(R.id.listView1);
        适配器=新MenuAdapter(本);
        myList.setAdapter(适配器);        myList.setOnItemClickListener(新OnItemClickListener(){
            @覆盖
            公共无效onItemClick(适配器视图<>母公司,视图V,INT位置,长的id){
                myPosiition =位置;
                StartActivityMethod();            }
        });    }    私人无效StartActivityMethod(){
        开关(myPosiition){
        情况下0:
            意图ITEM2 =新意图(这一点,Item2.class);
            startActivity(ITEM2);
            完();
            打破;
        情况1:
            意向项目3 =新意图(这一点,Item3.class);
            startActivity(项目3);
            完();
            打破;
        案例2:
            Toast.makeText(这一点,缺页,Toast.LENGTH_SHORT).show();
            打破;        }
    }}


解决方案

您不应该创建一个Activity类的一个实例。它不是一个普通的类。它有它的生命周期持有。您只需在清单中声明活动。

而不是做一个Uitlity类,你可以通过上下文中的实用工具类的构造函数,并用它在那里。

引用拉哈夫苏德


  

通过一种治疗活动作为一个正常的Java类,你结束了一个空
  上下文。如在活动中的大多数方法都呼吁其上下文,你
  会得到一个空指针异常,这就是为什么你的应用程序崩溃。


<一个href=\"http://stackoverflow.com/questions/14956018/can-i-create-the-object-of-a-activity-in-other-class\">Can我创建其他类活动的对象呢?

I have a problem using a method from another class that Extends from an Activity. I have a code that needs to repeat in every single Activity, So i created a class with this code and I get an error when I tryied to use the method at other class For exmple:

First Class:

public class first extends Activity
    public void onCreate(){
        second s = new second();
        s.myMethod();
    }

Second Class:

public class second extends Activity
    public void myMethod(){}

This way I get NullPointerExption.

First Class:

public class first extends Activity
    public void onCreate(){
        Context context = getApplicationContext();
        second s = ((second)context);
        s.myMethod(context );
    }

Second Class:

public class second extends Activity
    Context context ;
    public void myMethod(Context context){
        this.context = context;
    }

This way I get InvocationTargetException.

This is my code:

MainActivity - First Class:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }//onCreate


    public void ClickMe(View  V) {
        Toast.makeText(this, "ClickMe", Toast.LENGTH_SHORT).show();
        Context context = getApplicationContext();
        menuSetup myMenu = ((menuSetup)context);
        myMenu.SetMenuListView();

    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item) {// Action Bar buttons
        switch (item.getItemId()) {
        case R.id.action_settings:
            SlidingDrawer myS = (SlidingDrawer) findViewById(R.id.slidingDrawer1);
            myS.animateToggle();
            break;
        }
        return super.onOptionsItemSelected(item);
    }
}

menulList - Second Class:

public class menuSetup extends Activity {
    MenuAdapter adapter;
    ListView myList;
    int myPosiition;

    public void SetMenuListView() {
        myList = (ListView) findViewById(R.id.listView1);
        adapter = new MenuAdapter(this);
        myList.setAdapter(adapter);

        myList.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View v,  int position, long id) {
                myPosiition = position;
                StartActivityMethod();

            }
        });

    }

    private void StartActivityMethod() {
        switch (myPosiition) {
        case 0:
            Intent item2 = new Intent(this, Item2.class);
            startActivity(item2);
            finish();
            break;
        case 1:
            Intent item3 = new Intent(this, Item3.class);
            startActivity(item3);
            finish();
            break;
        case 2:
            Toast.makeText(this, "Missing Page", Toast.LENGTH_SHORT).show();
            break;

        }
    }

}

解决方案

You should not create an instance of a Activity class. Its not a normal class. It has a lifecycle of it own. You only declared Activity in manifest.

Instead make a Uitlity class and you can pass the Context to the constructor of Utility class and use it there.

Quoting Raghav Sood

By treating an Activity as a normal Java class, you end up with a null context. As most methods in an Activity are called on its Context, you will get a null pointer exception, which is why your app crashes.

Can i Create the object of a activity in other class?

这篇关于从一项活动扩展另一个类使用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆