Asked  7 Months ago    Answers:  5   Viewed   66 times

I want to print something in console, so that I can debug it. But for some reason, nothing prints in my Android application.

How do I debug then?

public class HelloWebview extends Activity {
    WebView webview;    
    private static final String LOG_TAG = "WebViewDemo";
    private class HelloWebViewClient extends WebViewClient {
        @Override
        public boolean shouldOverrideUrlLoading(WebView view, String url) {
            view.loadUrl(url);
            return true;
        }
    }

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        webview = (WebView) findViewById(R.id.webview);
        webview.setWebViewClient(new HelloWebViewClient());
        webview.getSettings().setJavaScriptEnabled(true);
        webview.setWebChromeClient(new MyWebChromeClient());
        webview.loadUrl("http://example.com/");    
        System.out.println("I am here");
    }

 Answers

72

Correction:

On the emulator and most devices System.out.println gets redirected to LogCat and printed using Log.i(). This may not be true on very old or custom Android versions.

Original:

There is no console to send the messages to so the System.out.println messages get lost. In the same way this happens when you run a "traditional" Java application with javaw.

Instead, you can use the Android Log class:

Log.d("MyApp","I am here");

You can then view the log either in the Logcat view in Eclipse, or by running the following command:

adb logcat

It's good to get in to the habit of looking at logcat output as that is also where the Stack Traces of any uncaught Exceptions are displayed.

The first Entry to every logging call is the log tag which identifies the source of the log message. This is helpful as you can filter the output of the log to show just your messages. To make sure that you're consistent with your log tag it's probably best to define it once as a static final String somewhere.

Log.d(MyActivity.LOG_TAG,"Application started");

There are five one-letter methods in Log corresponding to the following levels:

  • e() - Error
  • w() - Warning
  • i() - Information
  • d() - Debug
  • v() - Verbose
  • wtf() - What a Terrible Failure

The documentation says the following about the levels:

Verbose should never be compiled into an application except during development. Debug logs are compiled in but stripped at runtime. Error, warning and info logs are always kept.

Tuesday, June 1, 2021
 
uiroshan
answered 7 Months ago
20

So, we inevitably found a way to print to different trays and with different settings, but not directly. We found it impossible to send attributes via the printJob.print method, and that much hasn't changed. However, we were able to set the name of the print job, then intercept the print job with a low-level Perl script, parse the name, and set the tray and duplex settings there. It's an extreme hack, but it works. It still remains true that Java Printer Attributes do not work, and you will need to find another way if you want to set them.

Wednesday, June 30, 2021
 
MannfromReno
answered 6 Months ago
47

FragmentStateAdapter in ViewPager2 is a little bit different than in ViewPager as follows:

  • Instead of implementing getCount(), implement getItemCount()
  • Instead of implementing getItem(int position), implement createFragment(int position)

  • Constructor signature is different by adding Lifecycle argument which should be included in super as well

So replace your ViewPagerFragmentAdapter with below

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.lifecycle.Lifecycle;
import androidx.viewpager2.adapter.FragmentStateAdapter;

import java.util.ArrayList;


public class ViewPagerFragmentAdapter extends FragmentStateAdapter {

    private ArrayList<Fragment> arrayList = new ArrayList<>();

    public ViewPagerFragmentAdapter(@NonNull FragmentManager fragmentManager, @NonNull Lifecycle lifecycle) {
        super(fragmentManager, lifecycle);
    }


    public void addFragment(Fragment fragment) {
        arrayList.add(fragment);
    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    @NonNull
    @Override
    public Fragment createFragment(int position) {
        // return your fragment that corresponds to this 'position'
        return arrayList.get(position);
    }

}

Also make sure to import/extebd the right adapter of ViewPager2

import androidx.viewpager2.adapter.FragmentStateAdapter;

Hope this helpful, and will be glad for more illustration

Sunday, October 17, 2021
 
suneet
answered 2 Months ago
94

After a bit of investigation it turns out that notifyItemChanged only works when RecyclerView is attached and actually has completed onLayout which happens after onCreate.

internally during RecyclerView.onLayout() which is called by notifyItemChanged -> requestLayout(), processAdapterUpdatesAndSetAnimationFlags() is called which checks if the item that is to be updated has an available ViewHolder which in this case is null because during onCreate(), RecyclerView is not attached to the window, thus no measurement and layout has done to RecyclerView

Apparently complete drawing of RecyclerView happens sometime after onCreate() and OnResume()

@Override
protected void onResume() {
    super.onResume();
    boolean a = list.isAttachedToWindow();//this is false!
}

So to make notifyItemChanged work on onCreate

list.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
        @Override
        public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
            adapter.notifyItemChanged(1, new Object());//this will update the item
        }
    });



new Thread((new Runnable() {
        @Override
        public void run() {
            try {
                Thread.sleep(28);//small amount of delay,below 20 doesn't seem to work
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    adapter.notifyItemChanged(1, new Object());//also works
                }
            });
        }
    })).start();

I guess the reason for this is ViewRootImpl.performTraversal() is controlled by the system and it happens sometime after DecorView is attached to the window which is during onCreate

I'm assuming this can only happen during onCreate and onResume, it may not happen if called later.

Wednesday, November 3, 2021
 
GSree
answered 1 Month ago
61
    int val=1;
    for(int i=0;i<6;i++){
        for(int j=1;j<i;j++){
             System.out.print(val);
            val++;
        }
        System.out.print("n");
    }

initially val is equal to 1 . Inside the first for loop i=0 and j with increase from 1, but when i=0 second for loop doesn't run. then you get the first value as 1. Then it will point to new line.

When i=1,j still 1 so second for loop runs 1 time and print 2, because val has increment(val++). when j=2 in inside for loop it is not running only print the new value (3) of val there.

so on this will work

Tuesday, November 23, 2021
 
Limon
answered 2 Weeks ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :  
Share