2013-04-28 46 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