Friday, August 5, 2016

Android Learning Note: Never Set 2 Different Layout To 1 Activity

This is the original post of mine, which is wrong in fact.



Just add the following codes behind "super.onCreate(savedInstanceState);" sentence in the onCreate(Bundle savedInstanceState) methed of the main Activity.


//Entering Splash Screen. Here I use the layout file welcome.xml
setContentView(R.layout.welcome);

// Creating a Handler object and operate it like this. You may set another waiting time
new Handler() {
    @Override    public void handleMessage(Message msg) {
        setContentView(R.layout.activity_main);
    }
}.sendEmptyMessageDelayed(1,2000);

After short enjoyness, I got into trouble caused by the codes below. Crap say no more, I used the methed setContentView() twice in the codes below, which caused serious NullPointerException in the following codes.
Maybe this is a stupid mistake for many of you, I thgink it valuable for me, for I have debuged the program and resulted others to make sense of it. And I'm impressed on this error, believing that I'll never make this kind of mistake.

Never Set 2 Different Layout To 1 Activity.

Wednesday, August 3, 2016

Android Studio Project: Classic 2048 Game

Classic 2048 Game on Android. Just enjoy it!

Click To Download On Amazon Appstore

Project site

Android Studio Project: Calculator

This is an arithmetic calculator with brackets on Android.

It transforms the inputted infix expression to a postfix expression firstly, then computes the value of this postfix expression. Yeah, it is based on the thought of stack, an important kind of data structure. And the main layout of "Calculator" is "GridLayout".

Though it seems very simple, I learned a lot durng development.

A lightweight calculator on Android. Why not have a try?

Installation package

Project site

Tuesday, May 10, 2016

LeetCode Online Judge-2. Add Two Numbers

You are given two linked lists representing two non-negative numbers. The digits are stored in reverse order and each of their nodes contain a single digit. Add the two numbers and return it as a linked list.
Input: (2 -> 4 -> 3) + (5 -> 6 -> 4)
Output: 7 -> 0 -> 8

//C Program: Add Two Numbers
/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     struct ListNode *next;
 * };
 */
struct ListNode* addTwoNumbers(struct ListNode* l1, struct ListNode* l2) {
    int val;
    struct ListNode* q = l1;
    struct ListNode* p;
    while (l1&&l2)
    {
        p = l1;
        val = l1->val + l2->val;
        l1->val = val % 10;
        if (val >= 10)
        {
            if (!l1->next)
            {
                l1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
                l1->next->val = 0;
                l1->next->next = NULL;
                if (!l2->next)
                {
                    l2->next = (struct ListNode*)malloc(sizeof(struct ListNode));
                    l2->next->val = 0;
                    l2->next->next = NULL;
                }
            }
            l1->next->val += 1;
        }
        l1 = l1->next;
        l2 = l2->next;
    }
    while (l1&&l1->val>=10)
    {
        l1->val %= 10;
        if (!l1->next)
        {
            l1->next = (struct ListNode*)malloc(sizeof(struct ListNode));
            l1->next->val = 1;
            l1->next->next = NULL;
            break;
        }
        l1->next->val += 1;
        l1 = l1->next;
    }
    if (l2)
    {
        p->next = l2;
    }
    return q;
}