Reputation: 365
I am using the layout posted below. I have the same file (same filename as well) in both Res/layout and Res/layout-land. Whenever I switch the orientation in my emulator, it always throws a NullPointerExeption. The logcat says it has a "FATAL ERROR" and a "NullPointerException". It worked when I didn't have the layout-land file, so I'm not sure what the issue is. Am I supposed to put the file in a Res/layout-port for portrait instead of the default Res/layout folder? Thanks in advance.
<?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);
}
});
}
}
Upvotes: 0
Views: 1290
Reputation: 918
You are dying because
you call
Button enter = (Button)findViewById(R.id.enterbtn);
but there is no View with id of enterbtn in this view.
So you get a null pointer exception on this line
enter.setOnClickListener(new OnClickListener() {
Upvotes: 1