2016-02-29 159 views
-3

我真的不會在哪裏發生NullPointerException ... 我檢查了我的xml文件和它的declaration..still有一個NULL ...無法啓動活動ComponentInfo {com.ceit.worldofgravity/com.ceit.worldofgravity.MainMenu}::java.lang.NullPointerException

這個..目錄下載

02-29 00:15:12.988: E/AndroidRuntime(1287): FATAL EXCEPTION: main 
02-29 00:15:12.988: E/AndroidRuntime(1287): java.lang.RuntimeException:Unable to start activity ComponentInfo{com.ceit.worldofgravity/com.ceit.worldofgravity.MainMenu}: java.lang.NullPointerException 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.ActivityThread.access$600(ActivityThread.java:141) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.os.Looper.loop(Looper.java:137) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.ActivityThread.main(ActivityThread.java:5041) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at java.lang.reflect.Method.invoke(Method.java:511) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at dalvik.system.NativeStart.main(Native Method) 
02-29 00:15:12.988: E/AndroidRuntime(1287): Caused by: java.lang.NullPointerException 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at com.ceit.worldofgravity.MainMenu.onCreate(MainMenu.java:33) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.Activity.performCreate(Activity.java:5104) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144) 
02-29 00:15:12.988: E/AndroidRuntime(1287):  ... 11 more 

主要Menu.java

============= ================================================== ==

package com.ceit.worldofgravity; 


import android.app.Activity; 
import android.content.Intent; 
import android.graphics.Typeface; 
import android.os.Bundle; 
import android.view.MotionEvent; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.view.View.OnTouchListener; 
import android.widget.ImageView; 
import android.widget.RelativeLayout; 
import android.widget.TextView; 

public class MainMenu extends Activity{ 

RelativeLayout Btn; 
ImageView ImageButton; 
TextView txt; 

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

    Btn = (RelativeLayout) findViewById(R.id.btn_start); 
    ImageButton = (ImageView) findViewById(R.id.imageView2); 


    Typeface Custom = Typeface.createFromAsset(getAssets(),"Sketch 3D.otf"); 

    txt.setTypeface(Custom); 

    Btn.setOnTouchListener(new OnTouchListener() { 


     @Override 
     public boolean onTouch(View arg0, MotionEvent arg1) { 

      return false; 
     } 
    }); 

    Btn.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View v) { 

      Intent myIntent = new Intent(MainMenu.this, Game.class); 
      startActivities(myIntent); 
     } 
    }); 
} 

protected void startActivities(Intent myIntent) { 

} 
} 

main.xml中

<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/bb" 
android:paddingBottom="@dimen/activity_vertical_margin" 
android:paddingLeft="@dimen/activity_horizontal_margin" 
android:paddingRight="@dimen/activity_horizontal_margin" 
android:paddingTop="@dimen/activity_vertical_margin" 
tools:context=".MainMenu" > 

<ImageView 
    android:id="@+id/imageView1" 
    android:layout_width="550dp" 
    android:layout_height="160dp" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:src="@drawable/title" android:contentDescription="TODO"/> 

<RelativeLayout 
    android:id="@+id/btn_start" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_below="@+id/imageView1" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="18dp" > 

    <ImageView 
     android:id="@+id/imageView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:src="@drawable/play" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_centerHorizontal="true" 
     android:layout_centerVertical="true" 
     android:text="@string/start_game" 
     android:textAppearance="?android:attr/textAppearanceMedium" /> 

</RelativeLayout> 

回答

-1

產生NullPointerException在MainMenu.java:33拋出,因爲txt成員沒有設置任何東西(所以是空)

你忘了初始化,例如。附:

txt = (TextView) findViewById(R.id.textView1); 
相關問題