我是 android 编程的新手,我编写了一个程序,在其中创建了一个按钮..单击它应该导航到其他页面我没有收到任何错误,但是当我启动应用程序时,它会立即打开并立即显示“不幸停止”并关闭该程序。
这是我的代码
package com.hotel;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class HotelActivity extends Activity implements OnClickListener {
Button btnClick=null, button1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hotel);
button1 = (Button)findViewById(R.id.button1);
btnClick.setOnClickListener(this);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_hotel, menu);
return true;
}
@Override
public void onClick(View v) {
if(R.id.button1==v.getId())
{
Intent i= new Intent(HotelActivity.this,MenuActivity.class);
startActivity(i);
}
// TODO Auto-generated method stub
}
}
这是它的相应 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"
tools:context=".HotelActivity" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="162dp"
android:text="@string/start" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="@string/hello_world" />
</RelativeLayout>
这是第二页的代码
package com.hotel;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
@SuppressLint("ShowToast") public class MenuActivity extends Activity implements OnItemClickListener{
/** Called when the activity is first created. */
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
lv = (ListView)findViewById(R.id.menu_settings);
lv.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String selValue= (String)lv.getItemAtPosition(arg2);
Toast.makeText(getApplicationContext(), "Selected Item = "+selValue, 1000).show();
// TODO Auto-generated method stub
}
}
这是它各自的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:orientation="vertical" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="@array/courses">
</ListView>
</LinearLayout>