2013-06-21 23 views
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); 
    } 
} 

logcat的

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 

僅錯誤,當我啓動的應用程序是:應用程序意外終止

回答

0

活動不能被實例化像text t = new text();但他們必須通過IntentstartActivity啓動。

+0

和?如何初始化類? – user1814358

+0

你必須仔細閱讀Android Activity LifeCycle。你有一套系統將調用的方法。選擇一個最適合您需要 – Blackbelt

0

錯誤涉及到線路的text類9。你不能叫

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

而無需先調用setContentView()

你應該初始化您LinearLayout lViewonCreate()方法中,調用後的setContentView();

這裏有一個如何正確填寫活動視圖的例子:你有什麼建議http://developer.android.com/training/basics/activity-lifecycle/starting.html#Create

+0

所以我需要設置onCreate方法在test.java,並初始化onCreate方法直接在線性佈局?我現在這樣做的情況一樣 – user1814358

+0

正確。你也必須遵循@blackbelt的建議。如果你仍然收到錯誤,你應該發佈新的堆棧跟蹤/打開一個新的問題。 –

+0

哦,我看到@blackbelt建議,我讀到活動的生命週期,但我沒有看到任何有益,我在有關活動的生命週期的文章。 – user1814358