2014-05-15 31 views
0

我想設置onclick偵聽器,將觸發對話框,但在我的代碼中出現了一些錯誤。這是我錯誤地指派了聽衆。因爲它看起來具有null pointerexception錯誤,它是怎麼發生的?錯誤與clicklistener召喚對話框功能

這是我的logcat錯誤列表。

05-15 18:42:38.273: E/AndroidRuntime(27345): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.checklist/com.example.checklist.MainActivity}: java.lang.NullPointerException 

05-15 18:55:35.361: E/AndroidRuntime(28319): Caused by: java.lang.NullPointerException 
05-15 18:55:35.361: E/AndroidRuntime(28319): at com.example.checklist.MainActivity.onCreate(MainActivity.java:55) 

這裏是我的代碼

public class MainActivity extends Activity implements OnClickListener{ 

ArrayList<Product> products = new ArrayList<Product>(); 

ListAdapter boxAdapter; 


    /** Called when the activity is first created. */ 
    public void onCreate(final Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.listview); 
    fillData(); 
    boxAdapter = new ListAdapter(this, products); 

    ListView lvMain = (ListView) findViewById(R.id.lvMain); 
    lvMain.setAdapter(boxAdapter); 


    Button btn = (Button) findViewById(R.id.insert); 

    OnClickListener listener = new OnClickListener() {   
     @Override 
     public void onClick(View v) {        

      Dialog d = onCreateDialog(savedInstanceState); 
      d.show(); 
     } 
    }; 

    /** Setting the event listener for the add button */ 
    btn.setOnClickListener(listener);  



    } 

    void fillData() { 

     String[] dataArray = getResources().getStringArray(R.array.ChecklistData); 
     for(String productName : dataArray) 
     { 
     products.add(new Product(productName,false)); 
     } 

    } 







    public Dialog onCreateDialog(Bundle savedInstanceState) { 
     AlertDialog.Builder builder = new AlertDialog.Builder(this); 
     // Get the layout inflater 
     LayoutInflater inflater = this.getLayoutInflater(); 

     // Inflate and set the layout for the dialog 
     // Pass null as the parent view because its going in the dialog layout 
     builder.setView(inflater.inflate(R.layout.dialog, null)) 
     // Add action buttons 
      .setPositiveButton(R.string.Insert, new DialogInterface.OnClickListener() { 
       @Override 
       public void onClick(DialogInterface dialog, int id) { 
        //get the text from the setText and insert it into the arrayList name products 


       } 
      }) 
      .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() { 
       public void onClick(DialogInterface dialog, int id) { 
        // DialogFragment.this.getDialog().cancel(); 
       } 
      });  
     return builder.create(); 
    } 

@Override 
public void onClick(View v) { 
    // TODO Auto-generated method stub 

} 






    /*public void showResult(View v) { 
    String result = "Selected Product are :"; 
    int totalAmount=0; 
    for (Product p : boxAdapter.getBox()) { 
     if (p.box){ 
     result += "\n" + p.name; 
     //totalAmount+=p.price; 
     } 
    } 
    // Toast.makeText(this, result+"\n"+"Total Amount:="+totalAmount, Toast.LENGTH_LONG).show(); 
    }*/ 
} 

這裏是我的dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="wrap_content" 
android:layout_height="wrap_content" 
android:orientation="vertical" > 


<ImageView 
    android:src="@drawable/ic_launcher" 
    android:layout_width="match_parent" 
    android:layout_height="64dp" 
    android:scaleType="center" 
    android:background="#FFFFBB33" 
    android:contentDescription="@string/app_name" /> 

    <EditText 
    android:id="@+id/insert" 
    android:inputType="text" 
    android:layout_width="match_parent" 
    android:layout_height="wrap_content" 
    android:layout_marginTop="16dp" 
    android:layout_marginLeft="4dp" 
    android:layout_marginRight="4dp" 
    android:layout_marginBottom="4dp" 
    android:hint="@string/ItemName" /> 
    </LinearLayout> 
+0

是什麼'MainActivity.java' 55行? – Raghunandan

+0

btn.setOnClickListener(listener); – newbie

+0

post'listview.xml' – Raghunandan

回答

0

您沒有在您發佈的listview.xml中有編號爲insert的按鈕。因此btn爲空,您將在空對象上調用setOnClickListener(listener)

添加

<Button 
android:id="@+id/insert" 
android:layout_width="wrap_content" 
1

listview.xml佈局不具有ID insert的視圖。

你可能想

android:id="@+id/insert" 

添加到Button那裏。

也從XML中刪除onClick屬性,因爲您已將代碼註釋掉了。


編輯基於評論:

插入與我dialog.xml

然後調用findViewById()等上,而創建對話框膨脹的佈局,例如

View layout = inflater.inflate(R.layout.dialog, null); 
Button btn = layout.findViewById(...); 
btn.setOnClickListener(...); 
builder.setView(layout) 

編輯2基於dialog.xml:

insertEditText,而不是一個Button。請下定決心:)

+0

insert with my dialog.xml – newbie

+0

dialog.xml發佈 – newbie