0

我无法为 Android 执行我的命理应用程序,我完全是 Android 和 Java 的初学者,但没有编程。

这是源代码:

Main.java 代码

package com.nezic.numerology;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
//import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
//import android.widget.DatePicker.OnDateChangedListener;
//import android.widget.TextView;

public class Main extends Activity {

    text t = new text();
    private void PrintText(int num){
        switch(num)
        {
        case 1:
            t.LuckyOne();
            break;
        case 2:
            t.LuckyTwo();
            break;
        case 3:
            t.LuckyThree();
            break;  
        case 4:
            t.LuckyFour();
            break;
        case 5:
            t.LuckyFive();
            break;
        case 6:
            t.LuckySix();
            break;
        case 7:
            t.LuckySeven();
            break;
        case 8:
            t.LuckyEight();
            break;
        case 9:
            t.LuckyNine();
            break;
    }
}

    //logic
    private void GetNum(){
        //TextView tv = (TextView)findViewById(R.id.textView1);

        Button b = (Button)findViewById(R.id.button1);

        b.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                    DatePicker dp = (DatePicker)findViewById(R.id.datePicker1);
                    int day = dp.getDayOfMonth();
                    int month = dp.getMonth();
                    int year = dp.getYear();
                    int num = 0;
                    int nnum=0;

                    String sdate = String.valueOf(month) + String.valueOf(day) + String.valueOf(year);

                    char[] c = sdate.toCharArray();


                    for(int i=0; i<c.length;i++)
                    {
                        num+=Character.getNumericValue(c[i]);
                    }

                    String snum = String.valueOf(num+1);
                    char[] cnum = snum.toCharArray();

                    for(int j=0; j<cnum.length; j++)
                    {
                        nnum+=Character.getNumericValue(cnum[j]);
                    }
                    setContentView(R.layout.screen);
                    PrintText(nnum);
                }
            });
            }

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

        setContentView(R.layout.main); //view   
        try
        {
            GetNum();
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

text.java 代码

package com.nezic.numerology;

import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.TextView;

public class text extends Activity{

    LinearLayout lView = (LinearLayout)findViewById(R.id.ScreenLayout);
    TextView tv = new TextView(this);
    public void LuckyOne()
    {
        tv.setText
        (" Your lucky number is <b>1</b> \n" +  
        "The 1 is a doer, a powerful force that produces results and does not allow anything or anyone to limit its potential. The 1 is aggressive, a necessary energy for creating and producing. The 1 is always in the forefront: a spearpoint directing and leading others. The shape of the number 1, just like the shape of all other symbols, reflects its meaning; it walks upright with pride and purpose. Strong, determined, unwavering and with specific goals in mind, the 1 can turn dreams and ideas into reality. It pushes obstacles aside or simply drills right through them."+
        "The 1 grows, transforms and improves in perpetuity, precisely the way Mother Nature does. After all, nature's primal force and most basic building block is the number 1. Nature knows no mercy and its cruelty can be heartbreaking. However, it knows balance and gives life indiscriminately and without judgment. Scientists sometimes compete with nature, genetically altering fruits or breeding bugs for specific purposes. An understanding of nature teaches us that this is never a good idea and almost always produces unexpected backlashes. The number 1 has a similar built-in perfection and balance; you can force a change here or there, but negative repercussions are likely.");
        lView.addView(tv);  
    }

    public void LuckyTwo()
    {
        tv.setText
        (" Your lucky number is <b>2</b> \n"+
        "If you look at the shape of the 2, however, you will recognize a symbolic representation of the ultimate survivor and an extremely resilient force." + 
        "Her shape, as if bent on a knee with her head and back bowed in humility and servitude, makes it easy to dismiss her as weak and powerless." + 
        "This is in stark contrast to the power and pride of the mighty and masculine 1, who will not bow for anyone at any price. However, when enough pressure is applied to the powerful warrior we see in the number 1, he will break and shatter and be done with."+
        "On the other hand, when the humble and feminine 2 finds herself under attack and burdened with a crushing weight, she will bend, she will bend as much as needed. And when the weight is removed, her elastic, flexible nature allows her to come right back up, seemingly with little harm done, and she will continue to play her role."+
        "Her strength and power is resilient and lasting, just as the shape of the number 2 reflects that so beautifully. There is much more to her than meets the eye; she is often the true power behind the throne. As we stand in awe to the leadership and decisive actions of the 1, his strength and cunning relies on the clever and insightful advice of his greatest ally, the number 2."
        );
        lView.addView(tv);
    }

    public void LuckyThree()
    {
        tv.setText("Your lucky number is <b>3</b>\n"+
        "The number 3 is like a gifted teenager who is still under the protection of its parents: a bit spoiled, certainly scattered and perpetually in need of guidance. However, the most obvious traits of the 3 are in the creative field. A powerful need to express feelings, ideas and visions of the imagination, coupled with an extroverted personality, makes it likely that a person with 3s in key points of their Numerology chart will seek a career in art, especially the verbal arts. His or her social skills are also excellent. Charm, wit and a sense of humor help a 3 individual along his or her path, and if that weren't enough, good looks and compelling charisma make this 'kid' particularly attractive."+
        "No wonder so many are drawn to those with 3s in their charts. Followers are even willing to forgive less favorable traits exhibited by 3s, like a lack of focus and direction, a tendency to procrastinate, an inability to finish projects and an unwillingness to take responsibility. On the other hand, there is a superficial side to the 3 that can be harder to look past: a narcissistic streak, a vanity, a need to be the center of attention. It is easy for the optimistic 3 to enjoy day-to-day life as long as all is well, but when challenging issues arise, it can become quickly apparent that most of the 3's focus has been on that sunny exterior, leaving its internal fortitude lacking. Without much moral strength or spiritual depth, a 3 can easily succumb to difficulties unless friends and family move in to support it."+
        "For the 3 to become a well-rounded, balanced and happy person, it must learn discipline. Some lucky 3s who exhibit talent early in life (such as gifted dancers or musical prodigies) are placed in an environment with just the sort of discipline that a 3 needs to protect these talents. Another unique quality of the 3 is its tendency to be 'lucky', or rather, to be in the right place at the right time. This may be connected to its innate sense of rhythm; timing can be measured in seconds or in years, by the beating of a heart or by the movement of the stars. It is all only a matter of scale, either way, the 3 seems to be in tune with the cyclical nature of our surroundings."
        );
        lView.addView(tv);
    }

    public void LuckyFour()
    {
        lView.addView(tv);
    }

    public void LuckyFive()
    {
        lView.addView(tv);
    }

    public void LuckySix()
    {
        lView.addView(tv);
    }

    public void LuckySeven()
    {
        lView.addView(tv);
    }

    public void LuckyEight()
    {
        lView.addView(tv);
    }

    public void LuckyNine()
    {
        lView.addView(tv);
    }
}

日志猫

06-21 13:16:41.406: E/AndroidRuntime(500): FATAL EXCEPTION: main
06-21 13:16:41.406: E/AndroidRuntime(500): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.nezic.numerology/com.nezic.numerology.Main}: java.lang.NullPointerException
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.os.Looper.loop(Looper.java:123)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-21 13:16:41.406: E/AndroidRuntime(500):  at java.lang.reflect.Method.invokeNative(Native Method)
06-21 13:16:41.406: E/AndroidRuntime(500):  at java.lang.reflect.Method.invoke(Method.java:521)
06-21 13:16:41.406: E/AndroidRuntime(500):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-21 13:16:41.406: E/AndroidRuntime(500):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-21 13:16:41.406: E/AndroidRuntime(500):  at dalvik.system.NativeStart.main(Native Method)
06-21 13:16:41.406: E/AndroidRuntime(500): Caused by: java.lang.NullPointerException
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.Activity.findViewById(Activity.java:1637)
06-21 13:16:41.406: E/AndroidRuntime(500):  at com.nezic.numerology.text.<init>(text.java:9)
06-21 13:16:41.406: E/AndroidRuntime(500):  at com.nezic.numerology.Main.<init>(Main.java:15)
06-21 13:16:41.406: E/AndroidRuntime(500):  at java.lang.Class.newInstanceImpl(Native Method)
06-21 13:16:41.406: E/AndroidRuntime(500):  at java.lang.Class.newInstance(Class.java:1429)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.Instrumentation.newActivity(Instrumentation.java:1021)
06-21 13:16:41.406: E/AndroidRuntime(500):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2577)
06-21 13:16:41.406: E/AndroidRuntime(500):  ... 11 more

我启动应用程序时唯一的错误是:应用程序已意外停止

4

2 回答 2

0

该错误与您text班级的第 9 行有关。你不能打电话

LinearLayout lView = (LinearLayout)findViewById(R.id.ScreenLayout);

无需先调用setContentView()

在调用 setContentView(); 之后,你应该LinearLayout lView在你的方法中初始化你的。onCreate()

以下是如何正确填充活动视图的示例:http: //developer.android.com/training/basics/activity-lifecycle/starting.html#Create

于 2013-06-21T13:39:54.083 回答
0

Activity 不能被实例化, text t = new text();但它们必须通过Intentand启动startActivity

于 2013-06-21T13:37:08.157 回答