Java 应用程序意外停止-致命异常:main

Java 应用程序意外停止-致命异常:main,java,android,Java,Android,我不能为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执行我的命理学应用程序,我完全是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;
    }

}
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);
    }
}
package com.nezic.numerology;
导入android.os.Bundle;
导入android.app.Activity;
导入android.view.Menu;
//导入android.view.MotionEvent;
导入android.view.view;
导入android.widget.Button;
导入android.widget.DatePicker;
//导入android.widget.DatePicker.OnDateChangedListener;
//导入android.widget.TextView;
公共类主要扩展活动{
text t=新文本();
私有void打印文本(int num){
开关(num)
{
案例1:
t、 幸运的是();
打破
案例2:
t、 幸运的二();
打破
案例3:
t、 幸运的三个();
打破
案例4:
t、 幸运的四();
打破
案例5:
t、 幸运的五();
打破
案例6:
t、 幸运的六个();
打破
案例7:
t、 幸运七();
打破
案例8:
t、 幸运的是();
打破
案例9:
t、 幸运的是();
打破
}
}
//逻辑
私有void GetNum(){
//TextView tv=(TextView)findViewById(R.id.textView1);
按钮b=(按钮)findViewById(R.id.button1);
b、 setOnClickListener(新视图。OnClickListener(){
@凌驾
公共void onClick(视图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(月)+String.valueOf(日)+String.valueOf(年);
char[]c=sdate.toCharArray();

对于(int i=0;i活动,不能像
text t=new text();
那样实例化,但它们必须通过
Intent
startActivity
启动。错误与
text
类的第9行有关。您不能调用

LinearLayout lView = (LinearLayout)findViewById(R.id.ScreenLayout);
无需首先调用
setContentView()

调用setContentView()后,应在
onCreate()
方法中初始化
LinearLayout lView


下面是一个如何正确填充活动视图的示例:

以及您的建议?如何初始化类?您必须仔细阅读Android活动生命周期。您有一组系统将调用的方法。选择一个最适合您需要的方法。我需要在test.java中设置onCreate方法,并在e onCreate方法对吗?我经常在同样的情况下这样做,对吗?你还必须遵循下面@blackbelt的建议。如果你仍然有错误,你应该发布新的stacktrace/打开一个新问题。哦,我看到@blackbelt建议,我读了关于活动生命周期的文章,但我在关于ac的文章中没有看到任何对我有用的东西活动生命周期。我开始认为你的
text
类根本不应该扩展活动。看起来它更像是你想在
MainActivity
类中使用的助手类。我认为你应该在
MainActivity
中创建你的linearlayout,然后将linearlayout传递给你的text类,并将你的text视图添加到文本类。文本类不是一个活动,只是一个帮助类。希望这有意义。哎哟,我在test.java中创建了构造函数,它有两个参数要获取,LinearLayout和TextView,现在当我按click时,我有相同的错误,但启动时不存在错误。
LinearLayout lView = (LinearLayout)findViewById(R.id.ScreenLayout);