调用方法从不同的类文件执行任务

时间:2014-07-09 08:23:24

标签: java android nullpointerexception

所以我的主活动类populateButton中有一个按钮,它调用另一个类(.java文件)中的方法并传递一个数组。这个主类文件还有一个向TextView添加一些数据的方法,名为addNumber:

public class CheckerActivity extends Activity {

public TextView displayArray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_checker);UMBERS_ARRAY);

    displayArray = (TextView) findViewById(R.id.currentNumbers);
}

public void populateButton(View view) {

    try {
        String[] arrayOfLatestWinner = {"1", "2", "3", "4", "5", "6", "7", "8", "9"};
        variousMethods giveArrayOfLatestWinners = new variousMethods();
        String[] tester = giveArrayOfLatestWinners.checkNumbers(CheckerActivity.this, arrayOfLatestWinner);

    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void addNumber(String[] numbersToAdd) {

    displayArray.setText(Arrays.toString(numbersToAdd));

}

我还有另一个类(.java文件),其中包含一个名为checkNumbers的方法,该方法是从上面的populateButton中调用的。

public class variousMethods {

public String[] checkNumbers(Context context, String[] latestNumbersArray) {
try {
        File myNumbersFile = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + context.getString(R.string.my_lotto_numbers_file))).toString());
        BufferedReader myNumbersFileReader = new BufferedReader(new FileReader(myNumbersFile));

        String inputLineOfMyNumbersFile;
        inputLineOfMyNumbersFile = myNumbersFileReader.readLine();

        while (inputLineOfMyNumbersFile != null) {
        String[] arrayOfInputLine = inputLineOfMyNumbersFile.split(" ");

        // Bad Code
        CheckerActivity addNumbers = new CheckerActivity();
        addNumbers.addNumber(arrayOfInputLine);

        //SORRY, MISSED THIS LINE
        inputLineOfMyNumbersFile = myNumbersFileReader.readLine();
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

当推送populatButton时,它会发送String [] arrayOfLatestWinner到differentMethods中的checkNumbers。没关系。打开一个文件,我想要发生的是,一次返回文件的一行,并通过addNumbers(String [] numbersToAdd)在TextView中显示它。 (我在这段代码中只有最后一行最终会被显示,最终会填充一个GridView) 但是对addNumber的调用给出了NullPointerException。

我哪里出错了?

logcat是:

07-09 18:18:54.310  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ java.lang.NullPointerException
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.addNumber(CheckerActivity.java:84)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.variousMethods.checkNumbers(variousMethods.java:55)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:51)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ java.lang.ArrayIndexOutOfBoundsException: length=0; index=0
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.variousMethods.checkNumbers(variousMethods.java:93)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at au.com.acent.ash.basiclottochecker.CheckerActivity.populateButton(CheckerActivity.java:51)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$1.onClick(View.java:3809)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View.performClick(View.java:4421)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.view.View$PerformClick.run(View.java:17903)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.handleCallback(Handler.java:730)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Handler.dispatchMessage(Handler.java:92)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.os.Looper.loop(Looper.java:213)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at android.app.ActivityThread.main(ActivityThread.java:5225)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invokeNative(Native Method)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at java.lang.reflect.Method.invoke(Method.java:525)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:741)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
07-09 18:18:54.320  29028-29028/au.com.acent.ash.basiclottochecker W/System.err﹕ at dalvik.system.NativeStart.main(Native Method)`

3 个答案:

答案 0 :(得分:2)

CheckerActivity是一个Actiivty类。它不是一个普通的java类。所以你永远不应该创建一个Actiivty类的isntance。

CheckerActivity addNumbers = new CheckerActivity();
addNumbers.addNumber(arrayOfInputLine);

错误导致NullPointerException

更多信息阅读Raghav Sood的回答@

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

你做什么,你真的需要另一堂课吗?您可以在活动类中创建一个方法,并随时调用它。

编辑:

您可以创建一个Utility类

    public class Utility
    { 
        public String doSomething()
        {
            return "did something";
        }

    }       

活动

  Utility ul = new Utility();
  String result = ul.doSomething(); 
  // update ui with result  

答案 1 :(得分:1)

在这里返回数组

public class variousMethods {

public void checkNumbers(Context context, TextView displayArray, String[] latestNumbersArray) {
try {
        File myNumbersFile = new File((Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS + context.getString(R.string.my_lotto_numbers_file))).toString());
        BufferedReader myNumbersFileReader = new BufferedReader(new FileReader(myNumbersFile));

        String inputLineOfMyNumbersFile;
        inputLineOfMyNumbersFile = myNumbersFileReader.readLine();

        while (inputLineOfMyNumbersFile != null) {
        String[] arrayOfInputLine = inputLineOfMyNumbersFile.split(" ");

        inputLineOfMyNumbersFile = myNumbersFileReader.readLine();
        while (inputLineOfMyNumbersFile != null) {
            displayArray.setText(Arrays.toString(inputLineOfMyNumbersFile));
            inputLineOfMyNumbersFile = myNumbersFileReader.readLine();
        }

        }
    } catch (Exception e) {
        e.printStackTrace();
    }
  }
}

您可以删除添加号码方法。

variousMethods giveArrayOfLatestWinners = new variousMethods();
giveArrayOfLatestWinners.checkNumbers(CheckerActivity.this, displayArray, arrayOfLatestWinner);

答案 2 :(得分:0)

在这种情况下你必须使用单例,使用getinstance方法你可以调用活动方法,如:

在您的活动中

 static YourActivity _activity;

public static Activity getInstance(){       return _activity;      }

在oncreate中:

   _activity = this

如果您想使用此活动的任何方法,请调用如下:

_activity.getInstance().callmethod();