Reputation: 457
When I write like this MyActivity activity = Robolectric.setupActivity(MyActivity.class);
It always throw like this:
java.lang.NullPointerException
at com.printer.ui.activity.MyActivity.initView(MyActivity.java:309)
at com.ui.activity.MyActivity.onCreate(MyActivity.java:114)
That line is like this:
fileTextView.setText(AppUtil.getFileNameNoEx(AppConfig.printFile.getName()));
And AppConfig.printFile is null,how can I test that activity?
Upvotes: 2
Views: 117
Reputation: 20140
So quick fix for your test do:
AppConfig.printFile = new File();
MyActivity activity = Robolectric.setupActivity(MyActivity.class);
The proper fix would be passing the path in activity intent:
public class MyActivity {
public static final String FILE_PATH = "FILE_PATH";
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
String path = getIntent().getStringExtra(FILE_PATH);
...
}
...
}
Intent myActivityIntent = new Intent(this, MyActivity.class);
intent.putExtra(MyActivity.FILE_PATH, "path");
startActivity(intent);
Also try to avoid statics as much as possible http://www.endran.nl/blog/death-to-statics/
Upvotes: 1