2014-01-24 41 views
0

每當我點擊我的兩個圖像按鈕(EditButton,SaveButton)之一,我的應用程序崩潰。ImageButton崩潰的應用程序

的錯誤是一樣的,每次:

java.lang.IllegalStateException:找不到在活動課 com.example.groceryrunner.MainActivity爲的onClick處理程序的方法 onSaveClicked(視圖)和視圖 類的android.widget.ImageButton與ID'SaveButton'

事情是我什至沒有onSaveClicked(視圖)方法在任何事件在我的XML。我嘗試了各種組合方式,按鈕應該去點擊(onCreateLGClick),但它不影響任何事情,因爲我的應用程序永遠不會到達那裏。此外,唯一可以工作的按鈕並不一致,很多時候沒有任何反應,或者需要10次點擊才能使其事件停止,即使它之前已經工作(CreateLG按鈕)。

createlgmenu(XML):

<?xml version="1.0" encoding="utf-8"?> 
    <menu xmlns:android="http://schemas.android.com/apk/res/android" > 
    <item 
     android:id="@+id/Create_List" 
     android:title="@string/Create_List"/> 

    <item 
     android:id="@+id/Create_Food_Group" 
     android:title="@string/Create_Food_Group"/> 
    </menu> 

菜單(XML):

<menu xmlns:android="http://schemas.android.com/apk/res/android" > 
<item 
    android:id="@+id/action_settings" 
    android:orderInCategory="100" 
    android:showAsAction="never" 
    android:title="@string/action_settings"/> 
</menu> 

活動主(XML):

<TextView 
    android:id="@+id/GetStarted" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/ListName" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="86dp" 
    android:text="Select or Create a list to get started!" 
    android:textSize="20sp" /> 

<ImageButton 
    android:id="@+id/EditButton" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_alignRight="@+id/SaveButton" 
    android:layout_alignTop="@+id/ListName" 
    android:layout_marginRight="32dp" 
    android:background="@null" 
    android:scaleType="centerInside" 
    android:src="@drawable/edit_button" 
    android:onClick="onCreateLGClick" /> 

<Button 
    android:id="@+id/CreateLG" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignTop="@+id/ListName" 
    android:layout_toRightOf="@+id/ListsButton" 
    android:background="@null" 
    android:text="+" 
    android:textSize="40sp" 
    android:onClick="onCreateLGClick" /> 

<TextView 
    android:id="@+id/ListName" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignParentTop="true" 
    android:layout_centerHorizontal="true" 
    android:layout_marginTop="26dp" 
    android:text="Grocery Runner" 
    android:textSize="22sp" 
    android:onClick="onCreateLGClick" /> 

<Button 
    android:id="@+id/ListsButton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:layout_alignBottom="@+id/EditButton" 
    android:layout_alignLeft="@+id/GetStarted" 
    android:background="@null" 
    android:text="≡" 
    android:textSize="40sp" /> 

<ImageButton 
    android:id="@+id/SaveButton" 
    android:layout_width="50dp" 
    android:layout_height="50dp" 
    android:layout_alignRight="@+id/GetStarted" 
    android:layout_below="@+id/EditButton" 
    android:background="@null" 
    android:onClick="onCreateLGClick" 
    android:scaleType="centerInside" 
    android:src="@drawable/save_disk" /> 

</RelativeLayout> 

MainActivity.java:

package com.example.groceryrunner; 

import android.os.Bundle; 
import android.app.Activity; 
import android.app.AlertDialog; 
import android.view.Menu; 
import android.view.MenuItem; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.PopupMenu; 
import android.widget.TextView; 
import android.app.ActionBar; 
import android.content.DialogInterface; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 

public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    //Button Save = (Button) this.findViewById(R.id.SaveButton); 
} 


@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; 
} 

public void onCreateLGClick(View v) { 
    final int id = v.getId(); 
    /*switch (id) { 
    case R.id.CreateLG: 
     findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE); 
     createLGMenu(v); 
     break; 
    case R.id.ListsButton: 
     findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE); 
     createLGMenu(v); 
     break; 
    }*/ 
} 


public void createLGMenu(View v) { 
    PopupMenu LGMenu = new PopupMenu(this, v); 
    LGMenu.getMenuInflater().inflate(R.menu.createlgmenu, LGMenu.getMenu()); 
    LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 

     @Override 
     public boolean onMenuItemClick(MenuItem item) { 
      String choice = new String((String) item.getTitle()); 
      if (choice == "Create_List") { 
       createListDialog(); 
      } 
      else if (choice == "Create_Group") { 
       createListDialog(); 
      } 
      return false; 
     } 
    }); 
    LGMenu.show(); 
} 

public AlertDialog.Builder dialogBuilder; 
private void createListDialog() { 
    dialogBuilder = new AlertDialog.Builder(this); 
    EditText textInput = new EditText(this); 

    dialogBuilder.setTitle("Create list"); 
    dialogBuilder.setMessage("Name your list: "); 
    dialogBuilder.setView(textInput); 
    dialogBuilder.setPositiveButton("Create", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      findViewById(R.id.ListName).setVisibility(View.INVISIBLE); 
      //Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT); 
      // add list to ListsButton 
      //findViewById(R.id.ListName). -> Change ListName text to created list 
     } 
    }); 
    dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      //Toast.makeText(getApplicationContent(), "List has been created.", toast.LENGTH_SHORT); 
     } 
    }); 
    // Output 
    AlertDialog dialogue = dialogBuilder.create(); 
    dialogue.show(); 

    } 

} 
+0

安置自己的createLGMenu.xml,也被稱爲後的main.xml – Ogen

+0

發佈兩個菜單XML。謝謝。 – user2100364

+0

您是否嘗試清潔您的項目? – Ogen

回答

0

好的,我發現我的修復!首先,由於遇到其他錯誤,我只保存了我的代碼並刪除了Eclipse,並再次從Android的網站重新安裝/提取。該按鈕不再崩潰我的應用程序。

此外,那些沒有被承認是另外一個很大的障礙,我固定從ADAMM幫助的XML文件:)註釋掉線

1)我刪除將R進口

2中是給我的錯誤,承認我的XML文件

3)重啓動Eclipse的&內置所有

1

在你

@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; 
} 

你誇大了main.xml菜單。你確定它不是你需要膨脹的createlgmenu.xml嗎?

0

嘗試下面的工作罰款在我身邊。希望也會爲你工作。

public class MainActivity extends Activity { 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 
     // Button Save = (Button) this.findViewById(R.id.SaveButton); 
    } 

    @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; 
    } 

    public void onCreateLGClick(View v) { 
     final int id = v.getId(); 

     switch (v.getId()) { 
     case R.id.CreateLG: 
      findViewById(R.id.GetStarted).setVisibility(View.INVISIBLE); 
      createLGMenu(v); 
      break; 
     case R.id.ListsButton: 
      findViewById(R.id.GetStarted).setVisibility(View.VISIBLE); 
      createLGMenu(v); 
      break; 
     } 

    } 

    public void createLGMenu(View v) { 
     PopupMenu LGMenu = new PopupMenu(this, v); 
     LGMenu.getMenuInflater().inflate(R.menu.createmenu, LGMenu.getMenu()); 
     LGMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { 

      @Override 
      public boolean onMenuItemClick(MenuItem item) { 
       switch (item.getItemId()) { 
       case R.id.Create_List: 
        createListDialog(); 
        break; 
       case R.id.Create_Food_Group: 
        createListDialog(); 
        break; 

       default: 
        break; 
       } 
       /* 
       * String choice = new String((String) item.getTitle()); if 
       * (choice.equalsIgnoreCase("Create_List")){ createListDialog(); 
       * } else if (choice.equalsIgnoreCase("Create_Group")) { 
       * createListDialog(); } 
       */ 
       return true; 
      } 
     }); 
     LGMenu.show(); 
    } 

    public AlertDialog.Builder dialogBuilder; 

    private void createListDialog() { 
     dialogBuilder = new AlertDialog.Builder(this); 
     EditText textInput = new EditText(this); 

     dialogBuilder.setTitle("Create list"); 
     dialogBuilder.setMessage("Name your list: "); 
     dialogBuilder.setView(textInput); 
     dialogBuilder.setPositiveButton("Create", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         findViewById(R.id.ListName).setVisibility(
           View.INVISIBLE); 
         // Toast.makeText(getApplicationContent(), 
         // "List has been created.", toast.LENGTH_SHORT); 
         // add list to ListsButton 
         // findViewById(R.id.ListName). -> Change ListName text 
         // to created list 
        } 
       }); 
     dialogBuilder.setNegativeButton("Cancel", 
       new DialogInterface.OnClickListener() { 

        @Override 
        public void onClick(DialogInterface dialog, int which) { 
         // Toast.makeText(getApplicationContent(), 
         // "List has been created.", toast.LENGTH_SHORT); 
        } 
       }); 
     // Output 
     AlertDialog dialogue = dialogBuilder.create(); 
     dialogue.show(); 

    } 

} 
+0

我會嘗試一次,我可以,不幸的是eclipse不會讓我嘗試任何事情,因爲bin/resource.ap_「不存在」。我正在嘗試各種解決方案,但還沒有解決它。我會重新安裝Eclipse,但它是我的文件夾缺少問題的文件。 – user2100364