我正在使用下面发布的布局。我在 Res/layout 和 Res/layout-land 中都有相同的文件(也有相同的文件名)。每当我在模拟器中切换方向时,它总是会抛出 NullPointerExeption。logcat 说它有一个“FATAL ERROR”和一个“NullPointerException”。当我没有 layout-land 文件时它起作用了,所以我不确定问题是什么。我是否应该将文件放在 Res/layout-port 中进行纵向而不是默认的 Res/layout 文件夹?提前致谢。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:id="@+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ListView
android:id="@+id/tasklistview"
android:layout_width="match_parent"
android:layout_height="246dp"
android:layout_alignParentTop="true"
android:layout_toRightOf="@+id/enterbtn" >
</ListView>
</LinearLayout>
</ScrollView>
<EditText
android:id="@+id/entertaskfield"
android:layout_width="470dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:ems="10" />
<Button
android:id="@+id/send"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@drawable/send" />
public class MainActivity extends Activity {
public MenuDrawer mDrawer;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mDrawer = MenuDrawer.attach(this, Position.BOTTOM);
mDrawer.setContentView(R.layout.activity_main);
mDrawer.setMenuView(R.layout.rightmenu);
ListView myListView = (ListView)findViewById(R.id.tasklistview);
final EditText myEditText = (EditText)findViewById(R.id.entertaskfield);
Button enter = (Button)findViewById(R.id.enterbtn);
Button clearfull = (Button)findViewById(R.id.clearlist);
Button settings = (Button)findViewById(R.id.aboutbtn);
// Create the array list of to do items
final ArrayList todoItems = new ArrayList();
// Create the array adapter to bind the array to the listview
final ArrayAdapter aa;
aa = new ArrayAdapter(this,
android.R.layout.simple_list_item_1,
todoItems);
// Bind the array adapter to the listview.
myListView.setAdapter(aa);
enter.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
todoItems.add(0, myEditText.getText().toString());
aa.notifyDataSetChanged();
myEditText.setText("");
};
});
clearfull.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Deletion");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete all tasks?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("Yep", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
aa.clear();
aa.notifyDataSetChanged();
// Write your code here to invoke YES event
Toast.makeText(getApplicationContext(), "Tasks Removed", Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();
}
});
settings.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent myIntent = new Intent(MainActivity.this, About.class);
MainActivity.this.startActivity(myIntent);
overridePendingTransition(R.anim.push_up_in, R.anim.push_up_out);
}
});
}
}