开发者

How can I " printf " variables in android to check my code?

I have a problem which i'm trying to solve, I've been trying to fetch my data from an intent using mRowId below which I've been constantly returned a NullPointerException from fetchDrug() on LogCat.

Doing some troubleshooting using debugging, I've realized my variable mRowId is not Null and is pointing to something but I don't know what its pointing.

How can i check what is my variable mRowId pointing ? Or will normally such variables be gibberish that is incomprehensible?

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.drug_info);

        //Defining the views
        mDrugText = (TextView) findViewById(R.id.drug);
        mContentText = (TextView) findViewById(R.id.content);

        Bundle ext开发者_运维技巧ras = getIntent().getExtras();

        mRowId = extras.getLong(DrugsDbAdapter.KEY_ROWID);

        if(mRowId == null) {

            Log.e("mRowId is null", "ok");

        } else {

        Cursor drug = mDbHelper.fetchDrug(mRowId);

        //Managing Cursor to pluck values to display
        startManagingCursor(drug);

        mDrugText.setText(drug.getString(drug.getColumnIndexOrThrow(DrugsDbAdapter.KEY_DRUG)));

        mContentText.setText(drug.getString(drug.getColumnIndexOrThrow(DrugsDbAdapter.KEY_CONTENT)));

    }
    }


Instead of printf, use the existing log functionality in the SDK.

Output will appear in the logcat window of Eclipse (probably best option, since it's reasonably real-time and you can filter the display), or you can pull the logs off the device/emulator manually.

For example:

Log.i ( "MyApp", "Drug = " + drug.getString ( drug.getColumnIndexOrThrow ( DrugsDbAdapter.KEY_DRUG ) ) );


Use the debug function of eclipse and walk trough the code step by step. It gives the values of all variables.


Often objects will have a "toString()" method to represent the object in a human readable form; you could pass that into a Log.e() call. In your case, it appears to be a numeric (long) rather than an object, so you can directly display it in your log.

Once you know the row id you're trying to fetch, you could verify that its valid by examining your database (using the sqlite3 command which is built into the emulator, or extracting the file to your development machine and using any of myriad sqlite tools).

If the row id is valid, I'd suggest looking into your database helper code to see how the fetchDrug() method is using it. It's possible the null pointer has nothing to do with the parameter, but with other items in that method.


Here are a couple of options to consider:

Option 1: Set a breakpoint in onCreate in Eclipse. You can find more detail on debugging Android apps in Eclipse here: http://developer.android.com/guide/developing/debugging/debugging-projects.html

Option 2: Add Log statements to your code and view the output in logcat. You can read more about logging in Android here: http://developer.android.com/reference/android/util/Log.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜