NullPointerException during a performclick() - java

I'm doing a unit test of an activity. My activity has a button that calls a simple method (no activities, services or anything else).
java.lang.NullPointerException at
com.android.my.test.MyActivityTest.testPerformClick(MyActivity.java:41) at
java.lang.reflect.Method.invokeNative(Native Method) at
android.test.InstrumentationTestCase.runMethod(InstrumentationTestCase.java:205)
at
android.test.InstrumentationTestCase.runTest(InstrumentationTestCase.java:195)
at
android.test.ActivityInstrumentationTestCase2.runTest(ActivityInstrumentationTestCase2.java:175)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:169)
at android.test.AndroidTestRunner.runTest(AndroidTestRunner.java:154)
at
android.test.InstrumentationTestRunner.onStart(InstrumentationTestRunner.java:430)
at
android.app.Instrumentation$InstrumentationThread.run(Instrumentation.java:1447)
My test is this:
public class MyActivityTest extends ActivityInstrumentationTestCase2<MyActivity> {
MyActivity a;
public MyActivityTest() {
super("com.android.my", MyActivity.class);
}
#Override
public void setUp() throws Exception {
super.setUp();
a = this.getActivity();
}
#Override
public void tearDown() throws Exception {
super.tearDown();
}
public void testPerformClick() {
a.getButton().performClick();
}
}
And my activity is this.
public class MyActivity extends Activity implements OnClickListener {
private Button b;
#Override
public void onCreate(Bundle savedInstanceState) {
button = new Button();
button.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
method();
}
private void method() {
// [...]
}
public void getButton() {
return button();
}
}

My guess is that you need to change MyActivity to this. Your test is getting a NULL for the method getButton()
public class MyActivity extends Activity implements OnClickListener
{
private Button b;
#Override
public void onCreate(Bundle savedInstanceState)
{
b=new Button();
b.setOnClickListener(this);
}
#Override
public void onClick(View arg0)
{
method();
}
private void method()
{
...
}
public Button getButton()
{
return b;
}
}

Related

How to implement admob rewarded video ads in a list view?

I want to know how to implement AdMob rewarded video ads in a list view?
I'm using the source code from here
and I want to use it in this class StickerPackDetailsActivity.java
and the layout is gonna like this
![layout][1]
I want to lock add to WhatsApp and unlock it by watching video reward.
but this stickerdetails showed from listview from
![here][2]
so, how to implement video reward ads only in 1 specified item of the listview not all of them?
public class MainActivity extends AppCompatActivity implements RewardedVideoAdListener {
private RewardedVideoAd mRewardedVideoAd;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,"ca-app-pub111111111");
mRewardedVideoAd = MobileAds.getRewardedVideoAdInstance(this);
listitem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadRewardedVideoAd();
}
});
}
private void loadRewardedVideoAd() {
mRewardedVideoAd.loadAd("ca-app-pub-",
new AdRequest.Builder().build());
}
#Override
public void onRewardedVideoAdLoaded() {
}
#Override
public void onRewardedVideoAdOpened() {
}
#Override
public void onRewardedVideoStarted() {
}
#Override
public void onRewardedVideoAdClosed() {
loadRewardedVideoAd();
}
#Override
public void onRewarded(RewardItem rewardItem) {
}
#Override
public void onRewardedVideoAdLeftApplication() {
}
#Override
public void onRewardedVideoAdFailedToLoad(int i) {
}
#Override
public void onRewardedVideoCompleted() {
}
#Override
protected void onPause() {
mRewardedVideoAd.pause(this);
super.onPause();
}
#Override
protected void onResume() {
mRewardedVideoAd.resume(this);
super.onResume();
}
}

Cannot call method from another activity

I cannot call method from another activity in OnNotificationPosted. I also tried to make instance of activity but failed. I searched alot but no success. please help
NotificationListener
public class NotificationListener extends NotificationListenerService {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
otherActivity oth = new otherActivity();
oth.loc();
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg", "Notification Removed");
}
}
otherActivity
public class OtherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
}
public void loc() {
//code body
}
}
public class OtherActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_other);
}
public static void loc(){
//code body
}
}
how to using : otherActivity.loc();
public class NotificationListener extends NotificationListenerService {
#Override
public void onCreate() {
super.onCreate();
}
#Override
public void onNotificationPosted(StatusBarNotification sbn) {
otherActivity.loc();
}
#Override
public void onNotificationRemoved(StatusBarNotification sbn) {
Log.i("Msg","Notification Removed");
}

Calling method in main activity when a variable is changed in dialog box

I have listed the code below, What i am trying to achieve is the int i to be updated from a dialog box (dialog2). I then want to check in the main activity if it has changed and if it has changed then call a method. How would i do this?
Main Activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i = 0;
Dialog1 dialog1 = new Dialog1(this, i);
dialog1.show();
}
//Want to call this method whenever I is modified
private void iModified(){
}
}
Dialog 1 Class (This dialog just passes it to dialog2)
public class Dialog1 extends Dialog{
int integerI;
Button button;
public Dialog1(final Activity activity, final int i){
super(activity);
setContentView(R.layout.dialog1);
integerI = i;
button = findViewById(R.id.dialog1Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog2 dialog2 = new Dialog2(activity ,integerI);
dialog2.show();
closeDialog();
}
});
}
private void closeDialog(){
this.dismiss();
}
}
Dialog 2: This is where the integer is going to be changed and then i want it to be sent to the main activity and checked if it has changed and if so then replace the old integer with the new one.
public class Dialog2 extends Dialog {
int newI;
Button button;
public Dialog2(Activity activity, int i) {
super(activity);
setContentView(R.layout.dialog2);
newI= i + 12345;
button = findViewById(R.id.dialog2Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
close();
}
});
}
private void close(){
this.dismiss();
}
}
You can use interface.
your activity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int i = 0;
Dialog1 dialog1 = new Dialog1(this, i, new ModifiedListener() {
#Override
public void notify(int i) {
iModified();
}
});
dialog1.show();
}
//Want to call this method whenever I is modified
private void iModified(){
}
}
your dialog1:
public class Dialog1 extends Dialog {
int integerI;
Button button;
public Dialog1(final Activity activity, final int i, final ModifiedListener listener) {
super(activity);
setContentView(R.layout.dialog1);
integerI = i;
button = findViewById(R.id.dialog1Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog2 dialog2 = new Dialog2(activity, integerI);
dialog2.show();
closeDialog();
}
});
}
private void closeDialog() {
this.dismiss();
}
}
your dialog 2:
public class Dialog2 extends Dialog {
int newI;
Button button;
public Dialog2(Activity activity, int i, final ModifiedListener listener) {
super(activity);
setContentView(R.layout.dialog2);
newI = i + 12345;
//when you modify int you have to call
listener.notify(newI);
button = findViewById(R.id.dialog2Button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
close();
}
});
}
private void close() {
this.dismiss();
}
}
define interface similar to class:
public interface ModifiedListener {
public void notify(int i);
}

AsyncTaskLoader - how to?

I use loader to do task in back ground in method loadInBackground i added for loop to print log in logcat 50 time , so when i click the button start print log in logcat
but when i click the button i don't have any action and i don't have ant error , what is problem ?
mainActicity
public class MainActivity extends AppCompatActivity implements android.app.LoaderManager.LoaderCallbacks<String> {
TextView textView;
android.app.LoaderManager loaderManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loaderManager=getLoaderManager();
textView=findViewById(R.id.tv_result);
if (loaderManager.getLoader(0) ==null){
loaderManager.initLoader(0,null,this);
}
}
public void StartMysynstack (View view){
loaderManager.initLoader(0,null,this);
}
#Override
public android.content.Loader<String> onCreateLoader(int i, Bundle bundle) {
return new Myloader(this);
}
#Override
public void onLoadFinished(android.content.Loader<String> loader, String s) {
textView.setText(s);
}
#Override
public void onLoaderReset(android.content.Loader<String> loader) {
}
}
my loader
public class Myloader extends AsyncTaskLoader<String> {
int i;
public Myloader(Context context) {
super(context);
}
#Override
public String loadInBackground() {
for( i=0;i<=100;i++){
try {
Thread.sleep(50);
Log.d("waad","loadInBackground"+i);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return "Task result";
}
}
you can try calling it manually.
getLoaderManager().initLoader(0, null, this).forceLoad();
so according to your code you can make following changes.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loaderManager=getLoaderManager();
textView=findViewById(R.id.tv_result);
if (loaderManager.getLoader(0) ==null){
loaderManager.initLoader(0,null,this).forceLoad();;
}
}

How do I know the state of the ToggleButton from another activity?

How do I know the state of the ToggleButton from another activity?
i have class
public class MainScreen extends Activity{
protected ToggleButton tgbutton;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
tgbutton = (ToggleButton)findViewById(R.id.advice);
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(getApplicationContext());
tgbutton.setChecked(sharedPreferences.getBoolean("toggleButton", false));
}
public void oneClick(View v) {
if(tgbutton.isChecked() == false) { playSound(mSound); }
else { pauseSound(mSound); }
}
}
and
public class SmallprintA extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smallprint_a);
OnClickListener SendOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if(tgbutton.isChecked() == true){ playSound(mEmpty); }
else { playSound(mEmpty); }
}
};
}
I need in the class SmallprintA determine the state of the ToggleButton tgbutton. This is possible without class inheritance?
I did tgbutton is static
public class MainScreen extends Activity{
protected static ToggleButton tgbutton; // STATIC
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
tgbutton = (ToggleButton)findViewById(R.id.advice);
}
public void oneClick(View v) {
if(tgbutton.isChecked() == false) { playSound(mSound); }
else { pauseSound(mSound); }
}
}
and
public class SmallprintA extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.smallprint_a);
OnClickListener SendOnClickListener = new OnClickListener() {
#Override
public void onClick(View v) {
if(MainScreen.tgbutton.isChecked() == true){ playSound(mEmpty); }
else { playSound(mEmpty); }
}
};
}

Categories

Resources