MainActivity.java
package com.example.ch02_dialogdemo;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
public class MainActivity extends Activity implements OnClickListener{
private LinearLayout line;
private View myLoginView=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater layoutInflater=LayoutInflater.from(this );
myLoginView=layoutInflater.inflate(R.layout.login,line);
Button button=(Button)findViewById(R.id.bt1);
button.setOnClickListener(this);
}
@Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.bt1:
AlertDialog alertDialog=new AlertDialog.Builder(this ).create();
alertDialog.setTitle("用戶登錄");
alertDialog.setView(myLoginView);
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE,"確定",listener);
alertDialog.setButton(AlertDialog.BUTTON_NEGATIVE,"取消",liss);
alertDialog.show();
break;
default:break;
}
}
DialogInterface.OnClickListener listener=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
};
DialogInterface.OnClickListener liss=new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog,int which)
{
}
};
@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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在你的onClick()方法裡面有錯,
根據這個報錯,應該是你的Dialog設置布局的時候出問題了,好好檢查檢查