2012-08-16 5 views
-2

나는 안드로이드에 새로운 사람이다. 나는 그것에 게임을 만들려고 노력하고있다.하지만 에뮬레이터에서 코드를 실행할 때 "강제 종료"메시지를 보여주는 것보다 코드를 실행한다. 나는 스플래시 화면과 메뉴 코드를 같은 클래스에 쓴다. 코드에서 onclicklistner를 추가하지 않았지만 주 코드에서 버튼 액션을 추가하려고 할 때 괜찮습니다. 여기 자바에서 안드로이드 게임

다음은 MAINMENU 코드

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:background="@drawable/tile" > 


    <Button 
     android:id="@+id/button1" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignParentTop="true" 
     android:layout_centerHorizontal="true" 
     android:layout_marginTop="81dp" 
     android:text="Start Game" /> 




    <Button 
     android:id="@+id/button2" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_alignRight="@+id/button1" 
     android:layout_below="@+id/button1" 
     android:layout_marginTop="15dp" 
     android:text="Help" /> 




    <Button 
     android:id="@+id/button3" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button2" 
     android:layout_alignRight="@+id/button2" 
     android:layout_below="@+id/button2" 
     android:layout_marginTop="14dp" 
     android:text="Setting" /> 




    <Button 
     android:id="@+id/button4" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button3" 
     android:layout_alignRight="@+id/button3" 
     android:layout_below="@+id/button3" 
     android:layout_marginTop="14dp" 
     android:text="Credits" /> 





    <Button 
     android:id="@+id/button5" 
     style="?android:attr/buttonStyleSmall" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button4" 
     android:layout_alignRight="@+id/button4" 
     android:layout_below="@+id/button4" 
     android:layout_marginTop="16dp" 
     android:text="Exit" /> 

</RelativeLayout> 

내 주요 클래스 코드

package com.gillidanda; 

import android.os.AsyncTask; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ProgressBar; 
import android.widget.TextView; 
import android.widget.ViewSwitcher; 

public class GilliActivity extends Activity implements OnClickListener{ 

//creates a ViewSwitcher object, to switch between Views 
    private ViewSwitcher viewSwitcher; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 

     //Initialize a LoadViewTask object and call the execute() method 
     new LoadViewTask().execute(); 
     // setContentView(R.layout.activity_gilli); 

     Button b1 = (Button) findViewById(R.id.button1); 
     b1.setOnClickListener(this); 
    } 

    public void onClick(View v) { 
     // TODO Auto-generated method stub 
     if(v.getId()==R.id.button1){ 
      //Log.d("Amit","Button clicked"); 

         startActivity(new Intent(GilliActivity.this,StartGame.class)); 
     } 
    } 

    //To use the AsyncTask, it must be subclassed 
    private class LoadViewTask extends AsyncTask<Void, Integer, Void> 
    { 
     //A TextView object and a ProgressBar object 
     private TextView tv_progress; 
     private ProgressBar pb_progressBar; 

     //Before running code in the separate thread 
     @Override 
     protected void onPreExecute() 
     { 
      //Initialize the ViewSwitcher object 
      viewSwitcher = new ViewSwitcher(GilliActivity.this); 
      /* Initialize the loading screen with data from the 

         'loadingscreen.xml' layout xml file. 
      * Add the initialized View to the viewSwitcher.*/ 


        viewSwitcher.addView(ViewSwitcher.inflate(GilliActivity.this,   

        R.layout.activity_gilli, null)); 

      //Initialize the TextView and ProgressBar instances -  

           IMPORTANT: call findViewById() from viewSwitcher. 
      tv_progress = (TextView) 

          viewSwitcher.findViewById(R.id.tv_progress); 
      pb_progressBar = (ProgressBar) 

          viewSwitcher.findViewById(R.id.pb_progressbar); 
      //Sets the maximum value of the progress bar to 100    
      pb_progressBar.setMax(100); 

      //Set ViewSwitcher instance as the current View. 
      setContentView(viewSwitcher); 
     } 

     //The code to be executed in a background thread. 
     @Override 
     protected Void doInBackground(Void... params) 
     { 
      /* This is just a code that delays the thread execution 4 

           times, 
      * during 850 milliseconds and updates the current 

           progress. This 
      * is where the code that is going to be executed on a 

           background 
      * thread must be placed. 
      */ 
      try 
      { 
       //Get the current thread's token 
       synchronized (this) 
       { 
        //Initialize an integer (that will act as a 

               counter) to zero 
        int counter = 0; 
        //While the counter is smaller than four 
        while(counter <= 4) 
        { 
         //Wait 850 milliseconds 
         this.wait(850); 
         //Increment the counter 
         counter++; 
         //Set the current progress. 
         //This value is going to be passed 

                to the onProgressUpdate() method. 
         publishProgress(counter*25); 
        } 
       } 
      } 
      catch (InterruptedException e) 
      { 
       e.printStackTrace(); 
      } 
      return null; 
     } 

     //Update the TextView and the progress at progress bar 
     @Override 
     protected void onProgressUpdate(Integer... values) 
     { 
      //Update the progress at the UI if progress value is 

          smaller than 100 
      if(values[0] <= 100) 
      { 
       tv_progress.setText("Progress: " + 

            Integer.toString(values[0]) + "%"); 
       pb_progressBar.setProgress(values[0]); 
      } 
     } 

     //After executing the code in the thread 
     @Override 
     protected void onPostExecute(Void result) 
     { 
      /* Initialize the application's main interface from the 

            'main.xml' layout xml file. 
      * Add the initialized View to the viewSwitcher.*/ 


         viewSwitcher.addView(ViewSwitcher.inflate(GilliActivity.this, 

           R.layout.mainmenu, null)); 
      //Switch the Views 
      viewSwitcher.showNext(); 
     } 
    } 

    //Override the default back key behavior 
    @Override 
    public void onBackPressed() 
    { 
     //Emulate the progressDialog.setCancelable(false) behavior 
     //If the first view is being shown 
     if(viewSwitcher.getDisplayedChild() == 0) 
     { 
      //Do nothing 
      return; 
     } 
     else 
     { 
      //Finishes the current Activity 
      super.onBackPressed(); 
     } 
    } 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    getMenuInflater().inflate(R.menu.activity_gilli, menu); 
    return true; 
} 

} 여기

가 시작 화면 코드 여기

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
xmlns:tools="http://schemas.android.com/tools" 
android:layout_width="match_parent" 
android:layout_height="match_parent" 
android:background="@drawable/images" > 

<ProgressBar 
    android:id="@+id/pb_progressbar" 
    style="?android:attr/progressBarStyleHorizontal" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:layout_alignParentBottom="true" 
    android:layout_alignParentLeft="true" 
    android:layout_marginBottom="18dp" /> 

<TextView 
    android:id="@+id/tv_progress" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/pb_progressbar" 
    android:layout_centerHorizontal="true" 
    android:shadowColor="#000000" 
    android:shadowDx="2.0" 
    android:shadowDy="2.0" 
    android:shadowRadius="3.0" 
    android:text="Progress: 0%" 
    android:textAppearance="?android:attr/textAppearanceSmall" 
    android:textColor="#ffffffff" /> 

<TextView 
    android:id="@+id/tv_loadingtext" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_above="@+id/pb_progressbar" 
    android:layout_centerHorizontal="true" 
    android:layout_marginBottom="19dp" 
    android:text="Loading, please wait..." 
    android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

입니다입니다입니다 매니 페스트 C ode

<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.gillidanda" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="8" 
    android:targetSdkVersion="15" /> 

<application 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name=".GilliActivity" 
     android:label="@string/title_activity_gilli" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 

    <activity 
     android:name=".StartGame" 
     android:label="@string/title_activity_gilli" > 
     <intent-filter> 
      <action android:name="android.intent.action.StartGame" /> 
      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

pls 나에게 유용한 답을주세요. 답장을 보내 주셔서 감사합니다.

이것은 당신이 한 OnCreate 그래서 같이 findViewById의 된 setContentView 일을 실패하지 않은 내 로그 코드

08-13 16:05:02.257: D/AndroidRuntime(450): Shutting down VM 
08-13 16:05:02.257: W/dalvikvm(450): threadid=1: thread exiting with uncaught exception 

(group=0x40015560) 
08-13 16:05:02.276: E/AndroidRuntime(450): FATAL EXCEPTION: main 
08-13 16:05:02.276: E/AndroidRuntime(450): java.lang.RuntimeException: Unable to start 

activity ComponentInfo{com.gillidanda/com.gillidanda.GilliActivity}: 

java.lang.NullPointerException 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.app.ActivityThread.access$1500(ActivityThread.java:117) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.os.Handler.dispatchMessage(Handler.java:99) 
08-13 16:05:02.276: E/AndroidRuntime(450): at android.os.Looper.loop(Looper.java:123) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.app.ActivityThread.main(ActivityThread.java:3683) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

java.lang.reflect.Method.invokeNative(Native Method) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

java.lang.reflect.Method.invoke(Method.java:507) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-13 16:05:02.276: E/AndroidRuntime(450): at dalvik.system.NativeStart.main(Native 

Method) 
08-13 16:05:02.276: E/AndroidRuntime(450): Caused by: java.lang.NullPointerException 
08-13 16:05:02.276: E/AndroidRuntime(450): at com. 
gillidanda.GilliActivity.onCreate(GilliActivity.java:27) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-13 16:05:02.276: E/AndroidRuntime(450): at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
08-13 16:05:02.276: E/AndroidRuntime(450): ... 11 more 
08-13 16:05:49.157: D/AndroidRuntime(459): Shutting down VM 
08-13 16:05:49.176: W/dalvikvm(459): threadid=1: thread exiting with uncaught exception 

(group=0x40015560) 
08-13 16:05:49.216: E/AndroidRuntime(459): FATAL EXCEPTION: main 
08-13 16:05:49.216: E/AndroidRuntime(459): java.lang.RuntimeException: Unable to start 

activity ComponentInfo{com.gillidanda/com.gillidanda.GilliActivity}: 

java.lang.NullPointerException 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

android.app.ActivityThread.access$1500(ActivityThread.java:117) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

android.os.Handler.dispatchMessage(Handler.java:99) 
08-13 16:05:49.216: E/AndroidRuntime(459): at android.os.Looper.loop(Looper.java:123) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

android.app.ActivityThread.main(ActivityThread.java:3683) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

java.lang.reflect.Method.invokeNative(Native Method) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

java.lang.reflect.Method.invoke(Method.java:507) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
08-13 16:05:49.216: E/AndroidRuntime(459): at dalvik.system.NativeStart.main(Native 

Method) 
08-13 16:05:49.216: E/AndroidRuntime(459): Caused by: java.lang.NullPointerException 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

com.gillidanda.GilliActivity.onCreate(GilliActivity.java:27) 
08-13 16:05:49.216: E/AndroidRuntime(459): at 

android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
08-13 16:05:49.216: E/AndroidRuntime(459): at andr 
oid.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
08-13 16:05:49.216: E/AndroidRuntime(459): ... 11 more 
08-13 16:05:53.677: I/Process(459): Sending signal. PID: 459 SIG: 9 
+0

로그에 오류 출력이 표시됩니까? 그렇다면 세부 정보를 포함 시키십시오. –

+0

코드가 실제로 실패 할 때를 나타내는 주석을 추가하십시오. –

+0

예외는 무엇입니까? 여기에 성명서를 적어주십시오. – android

답변

1

입니다.

setContentView(R.layout.mainmenu) 
+0

나는 이것을 또한했다 그러나 다시 동일한 과실을 준다 – amit

0

코드를 실행했습니다. findViewById를 당신이 찾고있는 버튼이 현재보기에 속하지 않기 때문이다 null를 돌려 내가 왜 당신의

b1.setOnClickListener(this)  

유일한 이유에 널 포인터 예외를 얻고있다. 당신이 당신의에서 onCreate에

setContentView(R.layout.mainmenu)  

를 추가하는 경우()는 예외없이 작동합니다.