从纵向切换到横向NullPointerException

时间:2013-04-28 21:32:45

标签: android android-layout nullpointerexception

我正在使用下面发布的布局。我在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);
        }
    });

}


}

1 个答案:

答案 0 :(得分:1)

你快死了,因为

你打电话

Button enter = (Button)findViewById(R.id.enterbtn);

但此视图中没有带有enterbtn标识的View。

所以你得到一个空指针异常 在这一行

enter.setOnClickListener(new OnClickListener() {