2013-04-28 49 views
0

我正在使用下面发布的布局。我在Res/layout和Res/layout-land中都有相同的文件(相同的文件名)。每当我在模拟器中切换方向时,它总是抛出一个NullPointerExeption。 logcat说它有一个“致命错误”和一个“NullPointerException”。它工作时,我没有布局土地文件,所以我不知道这是什么问题。我是否应该将该文件放在Res/layout-port用于纵向而不是默认的Res/layout文件夹?提前致谢。从肖像切换到风景NullPointerException

<?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); 
     } 
    }); 

} 


} 
+0

后得到一个空指针异常 两种布局 – 2013-04-28 22:30:46

回答

1

你死去,因为

你叫

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

但与这种观点enterbtn的编号观。

所以,你在这一行

enter.setOnClickListener(new OnClickListener() {

+0

好吧,实际上使得有很大的意义。那么,我该如何解决这个问题?对不起,我以前只处理过一个方向(我对Android编程还是有点新鲜的)。谢谢 – ThatGuyThere 2013-04-28 21:45:08

+0

你需要用'android:id =“@ + id/enterbtn”在你的布局中添加一个按钮''你的肖像布局中可能已经有了这个按钮。 – 2013-04-29 13:29:47

+0

它看起来像发送按钮将是你的输入按钮。将你的android:id =“@ + id/send”'改成'android:id = @ + id/enterbtn' – 2013-04-29 13:39:18