2012-09-16 45 views
1

我正在構建一個應用程序,並且我有一個問題。當我試圖運行我的應用程序,我得到這些錯誤:E/AndroidRuntime(21484)無法啓動活動ComponentInfo {lt.prasom/lt.prasom.MainMenu}:java.lang.NullPointerException

>09-16 14:07:35.768: E/AndroidRuntime(21484): FATAL EXCEPTION: main 
>09-16 14:07:35.768: E/AndroidRuntime(21484): java.lang.RuntimeException: Unable to start activity ComponentInfo{lt.prasom/lt.prasom.MainMenu}: java.lang.NullPointerException 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.os.Handler.dispatchMessage(Handler.java:99) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.os.Looper.loop(Looper.java:130) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.app.ActivityThread.main(ActivityThread.java:3683) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at java.lang.reflect.Method.invokeNative(Native Method) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at java.lang.reflect.Method.invoke(Method.java:507) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at dalvik.system.NativeStart.main(Native Method) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): Caused by: java.lang.NullPointerException 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at lt.prasom.MainMenu.onCreate(MainMenu.java:19) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611) 
>09-16 14:07:35.768: E/AndroidRuntime(21484): ... 11 more 

我的清單:

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="lt.prasom" 
    android:versionCode="1" 
    android:versionName="1.0" 
    android:installLocation="preferExternal" >" 

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

    <application 
     android:icon="@drawable/ic_launcher" 
     android:label="@string/app_name" > 
     <activity 
      android:name=".MainMenu" 
      android:label="@string/app_name" 
      android:theme="@android:style/Theme.Black.NoTitleBar" > 
      <intent-filter> 
       <action android:name="android.intent.action.MAIN" /> 

       <category android:name="android.intent.category.LAUNCHER" /> 
      </intent-filter> 
     </activity> 
     <activity android:name=".GoodOffers" android:theme="@android:style/Theme.Black.NoTitleBar" /> 
     <activity android:name=".Search" android:theme="@android:style/Theme.Black.NoTitleBar" /> 
     <activity android:name=".Groups" android:theme="@android:style/Theme.Black.NoTitleBar" /> 
     <activity android:name=".Specialmenu" android:theme="@android:style/Theme.Black.NoTitleBar" /> 
    </application> 
    <uses-permission android:name="android.permission.INTERNET" /> 
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 
</manifest> 

我爲我的活動,是在lt.prasom包。當我重命名包名稱時出現錯誤。

MainMenu.java:

package lt.prasom; 

import lt.prasom.R; 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 

public class MainMenu extends Activity { 
    /** Called when the activity is first created. */ 

    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button search = (Button) findViewById(R.id.searchbutt); 
     search.setOnClickListener(new OnClickListener(){ 

      public void onClick(View arg0) { 
       Intent i = new Intent(MainMenu.this.getApplicationContext(), Search.class); 
       startActivity(i); 

      } 

     }); 
     Button groups = (Button) findViewById(R.id.groupsbutt); 
     groups.setOnClickListener(new OnClickListener(){ 

      public void onClick(View arg0) { 
       Intent i = new Intent(MainMenu.this.getApplicationContext(), Groups.class); 
       startActivity(i); 

      } 

     }); 
     Button goods = (Button) findViewById(R.id.specialbutt); 
     goods.setOnClickListener(new OnClickListener(){ 

      public void onClick(View arg0) { 
       Intent i = new Intent(MainMenu.this.getApplicationContext(), Specialmenu.class); 
       startActivity(i); 

      } 

     }); 
    } 
} 

的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:paddingTop="10dp" 
    android:orientation="vertical" 
    xmlns:ui="http://schemas.android.com/apk/res/lt.prasom" xmlns:app="http://schemas.android.com/apk/res/lt.prasom" 

    android:background="#f8f9fe" > 
    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:paddingTop="20dp" 
     android:layout_gravity="center" 
     android:src="@drawable/logo" /> 

<Button android:id="@+id/goodsbutt" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="10dp" 
        android:text="Geri pasiūlymai" 

        /> 
<Button android:id="@+id/akcijabutt" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="10dp" 
        android:text="Akcijinės prekės" 

        /> 
<Button android:id="@+id/newsbutt" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:padding="10dp" 
        android:text="Naujos prekės" 

        /> 



</LinearLayout> 

謝謝!

+1

你有MainMenu.onCreate一個空指針異常(MainMenu的。的java:19)。確保你沒有引用空對象。請發佈代碼MainMenu – nandeesh

+0

您必須發佈MainMenu – weakwire

+0

現在發佈它... –

回答

1

searchbutt id未在當前活動的main.xml中聲明。你必須選擇當前顯示BE的setContentView元素()

所以Button search = (Button) findViewById(R.id.searchbutt);搜索null

search.setOnClickListener(new OnClickListener(){ 

拋出異常

相關問題