2012-05-29 115 views
1

我是相當新的Android和應用程序創建後立即關閉。任何幫助將不勝感激!Android應用程序關閉創建?

一些關於該應用程序,我創建一個基於文本的冒險,我把所有的代碼編碼到Java已經,我知道了Java代碼的工作,因爲我有一個JavaGui創建和它的工作。我現在試圖把這個java遊戲帶到android。

這是我的錯誤日誌

05-29 09:19:01.989: E/AndroidRuntime(539): FATAL EXCEPTION: main 
05-29 09:19:01.989: E/AndroidRuntime(539): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.coalbolttproductions.russell/com.coalbolttproductions.russell.AdventureToCandyIslandActivity}: java.lang.NullPointerException 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1880) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.access$600(ActivityThread.java:123) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.os.Handler.dispatchMessage(Handler.java:99) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.os.Looper.loop(Looper.java:137) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.main(ActivityThread.java:4424) 
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.reflect.Method.invokeNative(Native Method) 
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.reflect.Method.invoke(Method.java:511) 
05-29 09:19:01.989: E/AndroidRuntime(539): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784) 
05-29 09:19:01.989: E/AndroidRuntime(539): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551) 
05-29 09:19:01.989: E/AndroidRuntime(539): at dalvik.system.NativeStart.main(Native Method) 
05-29 09:19:01.989: E/AndroidRuntime(539): Caused by: java.lang.NullPointerException 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.Activity.findViewById(Activity.java:1794) 
05-29 09:19:01.989: E/AndroidRuntime(539): at com.coalbolttproductions.russell.AdventureToCandyIslandActivity.<init>(AdventureToCandyIslandActivity.java:15) 
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.Class.newInstanceImpl(Native Method) 
05-29 09:19:01.989: E/AndroidRuntime(539): at java.lang.Class.newInstance(Class.java:1319) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.Instrumentation.newActivity(Instrumentation.java:1023) 
05-29 09:19:01.989: E/AndroidRuntime(539): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1871) 
05-29 09:19:01.989: E/AndroidRuntime(539): ... 11 more 

這是我的main.xml,我認爲是我的問題

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" 
    android:padding="25dp"> 

    <EditText 
     android:id="@+id/etCommands" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:hint="Type a Command" 
     android:password="true" /> 

    <Button 
     android:id="@+id/bResults" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="Try Command" /> 

    <TextView 
     android:id="@+id/tvResults" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:gravity="center" 
     android:text="TextView" /> 

</LinearLayout> 

由於這裏大衆需求的真正原因是從Java代碼我冒險類。 線15按鈕chkCmd

public class AdventureToCandyIslandActivity extends Activity implements View.OnClickListener{ 
    /** Called when the activity is first created. */ 

    Button chkCmd = (Button) findViewById(R.id.bResults); 
    EditText input; 
    TextView display; 

    Game gameModel; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 

     //Run the methods 
     createVars(); 
     chkCmd.setOnClickListener(this); 

    } 

    private void createVars(){ 
     gameModel = new Game(); 
     input = (EditText) findViewById(R.id.etCommands); 
     display = (TextView) findViewById(R.id.tvResults); 
     display.setText(gameModel.printWelcome()); 
     display.setGravity(Gravity.LEFT); 
     display.setTextColor(Color.WHITE); 

    } 

    public void onClick(View v) { 
     String inputLine = input.getText().toString(); 
     Command command = Command.getCommand(inputLine); 
     String result = gameModel.processCommand(command); 
     if(result.equals("EXIT")) { 
      System.exit(0); 
     } else { 
      display.append("--------------------------------\n" + result + "\n"); 
     } 

    } 


} 
+1

在這裏添加Java代碼,請。 – Sajmon

+0

作爲堆棧跟蹤說,活動'AdventureToCandyIslandActivity'失敗,因爲一個空指針的地方開始。如果你能從這裏顯示代碼,它會幫助我們很多。 – Cheesebaron

+0

'AdventureToCandyIslandActivity.java'看行沒有15 –

回答

2

無論大家認爲你是建立在返回null你有雙重檢查的ID是正確的,setContentView(R.layout.xxx)在正確的文件指向?

此外,如果你有縱向和橫向定義不同的佈局,這可能會導致類似的問題,如果你的意見ID不match.AdventureToCandyIslandActivity.java看行沒有15

編輯

Button chkCmd = (Button) findViewById(R.id.bResults); 你初始化的類加載器的按鈕,如果你在同一個地方的其他意見初始化它會被罰款。

+0

嗯,我只使用垂直景觀在我的佈局,我相信我的setContentView是正確的。我上面粘貼的xml標題爲main.xml,這是我在ATCI活動中打開的 –

0

CHANGE

Button chkCmd = (Button) findViewById(R.id.bResults); 
EditText input; 
TextView display; 

Game gameModel; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

TO THIS

Button chkCmd; 
EditText input; 
TextView display; 

Game gameModel; 


@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 
    chkCmd = (Button) findViewById(R.id.bResults); 

如設置佈局之前,你無法初始化按鈕,這是佈局的一部分。

0
Button chkCmd = (Button) findViewById(R.id.bResults); 

您已經使用findViewById(R.id.bResults)設置內容查看取出後如此使用下面的代碼之前:

setContentView(R.layout.main); 
chkCmd = (Button) findViewById(R.id.bResults);