开发者

Null pointer exception on getParent()

hi i have the code below i have a null pointer exception (file name below is Home.java)

package com.sof.android;


import android.app.AlertDialog;
import android.app.TabActivity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;

public class Home extends TabActivity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
        setContentView(R.layout.home);
        getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.home_title);


        TextView username  = (TextView) findViewById(R.id.usernameBar);
        username.setText(Global.username);






        //Logout 

        Button logOut = (Button) this.findViewById(R.id.logoutBar);
        logOut.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                AlertDialog.Builder builder = new AlertDialog.Builder(Home.this);
                builder.setMessage("Are you sure you want to Log Out?")
                       .setCancelable(false)
                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                finish();
                           }
                       })
                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                           public void onClick(DialogInterface dialog, int id) {
                                dialog.cancel();
                           }
                       });
                AlertDialog alert = builder.create();
                alert.show();



            }
        });

        Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, a.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("artists").setIndicator("Home",
                          res.getDrawable(R.drawable.a))
                      .setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, b.class);
        spec = tabHost.newTabSpec("albums").setI开发者_Go百科ndicator("Ask",
                          res.getDrawable(R.drawable.b))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, c.class);
        spec = tabHost.newTabSpec("songs").setIndicator("Questions",
                          res.getDrawable(R.drawable.c))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, c.class);
        spec = tabHost.newTabSpec("tutorial").setIndicator("Tutorials",
                          res.getDrawable(R.drawable.d))
                      .setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, e.class);
        spec = tabHost.newTabSpec("account").setIndicator("Account",
                          res.getDrawable(R.drawable.e))
                      .setContent(intent);
        tabHost.addTab(spec);
        tabHost.setCurrentTab(0);
    }

    public boolean onKeyDown(int keyCode, KeyEvent event) {
         if (keyCode == KeyEvent.KEYCODE_BACK) {

         //preventing default implementation previous to android.os.Build.VERSION_CODES.ECLAIR
             Toast.makeText(getApplicationContext(), "Please Use Log Out.", Toast.LENGTH_LONG).show();   
         return true;

         }

         return super.onKeyDown(keyCode, event);    
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.icon:     Intent previewMessage = new Intent(getParent(), b.class);
                                TabGroupActivity parentActivity = (TabGroupActivity)getParent();
                                parentActivity.startChildActivity("askquestion", previewMessage);

                                break;
            case R.id.text:     AlertDialog.Builder builder = new AlertDialog.Builder(Home.this);
                                builder.setMessage("Are you sure you want to Log Out?")
                                       .setCancelable(false)
                                       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                           public void onClick(DialogInterface dialog, int id) {
                                                finish();
                                           }
                                       })
                                       .setNegativeButton("No", new DialogInterface.OnClickListener() {
                                           public void onClick(DialogInterface dialog, int id) {
                                                dialog.cancel();
                                           }
                                       });
                                AlertDialog alert = builder.create();
                                alert.show();
                                break;

            case R.id.icontext: Intent Account = new Intent(getParent(), e.class);
                                TabGroupActivity parentActivityE = (TabGroupActivity)getParent();
                                parentActivityE.startChildActivity("myaccount", Account);

                                break;
        }
        return true;
    }
}


Use Home.this instead of getParent()

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜