Android:获取错误java.lang.NullPointerException

时间:2012-10-18 09:50:52

标签: android android-tabhost

我是android的新手,我创建了一个项目。在我的活动中,我有ListView和Tabhost,以及我的活动范围listactivity。当我运行我的项目然后它给出错误

10-18 14:26:13.053: E/AndroidRuntime(278): FATAL EXCEPTION: main
10-18 14:26:13.053: E/AndroidRuntime(278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.shareslab/com.example.shareslab.MessageList}: java.lang.NullPointerException
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.os.Looper.loop(Looper.java:123)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.app.ActivityThread.main(ActivityThread.java:4627)
10-18 14:26:13.053: E/AndroidRuntime(278):  at java.lang.reflect.Method.invokeNative(Native Method)
10-18 14:26:13.053: E/AndroidRuntime(278):  at java.lang.reflect.Method.invoke(Method.java:521)
10-18 14:26:13.053: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
10-18 14:26:13.053: E/AndroidRuntime(278):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
10-18 14:26:13.053: E/AndroidRuntime(278):  at dalvik.system.NativeStart.main(Native Method)
10-18 14:26:13.053: E/AndroidRuntime(278): Caused by: java.lang.NullPointerException
10-18 14:26:13.053: E/AndroidRuntime(278):  at com.example.shareslab.MessageList.onCreate(MessageList.java:107)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-18 14:26:13.053: E/AndroidRuntime(278):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)

我的活动就在这里

package com.example.shareslab;


import java.util.ArrayList;
import java.util.Arrays;


import com.example.shareslab.R;

import android.app.Dialog;
import android.app.ListActivity;
import android.app.LocalActivityManager;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;

import android.os.Bundle;
import android.preference.PreferenceManager;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TabHost;

import android.widget.TabHost.TabSpec;
import android.widget.TextView;


public class MessageList extends ListActivity {
    LocalActivityManager mLocalActivityManager;



    public static String singleDescription;
    public static String title,URLToPost,imageURL;
    public static ArrayList<String> galleryImages;
    private static class EfficientAdapter extends BaseAdapter 
    {    
        private LayoutInflater mInflater;
        public EfficientAdapter(Context context)
        {
            mInflater = LayoutInflater.from(context);
        }
        @Override
        public int getCount() {
            System.out.println("description COUNT : "+SplashActivity.description.size());
            return SplashActivity.description.size();
        }
        @Override
        public Object getItem(int position) {
            return position;
        }
        @Override
        public long getItemId(int position) {
            return position;
        }
        @Override
        public View getView(final int position, View convertView, ViewGroup parent) 
        {

            ViewHolder holder;
            if (convertView == null)
            {
                convertView = mInflater.inflate(R.layout.inflate_list_item, null);
                holder = new ViewHolder();              
                holder.title = (TextView) convertView.findViewById(R.id.inflate_title);
                holder.des = (TextView) convertView.findViewById(R.id.inflate_description);
                holder.im=    (ImageView) convertView.findViewById(R.id.inflate_image);
                convertView.setTag(holder);           
            }
            else 
            {
                holder = (ViewHolder) convertView.getTag();

            }   

            UrlImageViewHelper.setUrlDrawable(holder.im, SplashActivity.imageURLAmit.get(position),null);
            holder.title.setText(SplashActivity.titles.get(position));
            holder.des.setText(SplashActivity.description.get(position));

            return convertView;
        }   

        public static class ViewHolder {
            TextView title,des;
            ImageView im;
        }

    } // close class Efficent adapter


    @Override
    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.main);




        ListView listView = getListView();
        listView.setTextFilterEnabled(true);
        this.setListAdapter(new EfficientAdapter(this));
        listView.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
                // When clicked, show a toast with the TextView text
                galleryImages=new ArrayList<String>();
                singleDescription=SplashActivity.description.get(position);
                title=SplashActivity.titles.get(position);
                URLToPost=SplashActivity.link.get(position);
                imageURL=SplashActivity.imageURLAmit.get(position);
                System.out.println("ON CLICK URL: "+URLToPost);
                galleryImages.addAll(Arrays.asList(SplashActivity.arrays[position]));               
                startActivity(new Intent(MessageList.this,MessageListDetail.class));
            }
        });

        TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);
        mLocalActivityManager = new LocalActivityManager(this, false);
        tabHost.setup(mLocalActivityManager);
        mLocalActivityManager.dispatchCreate(icicle); //after the tab's setup is called, you have to call this or it wont work

        TabHost.TabSpec spec;
        Intent intent;

        tabHost.getTabWidget().setDividerDrawable(R.drawable.tab_divider);


        intent = new Intent().setClass(this, Home.class);
        spec = tabHost.newTabSpec("tagname1").setIndicator("HOME").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, SocialMedia.class);
        spec = tabHost.newTabSpec("tagname2").setIndicator("SOCIAL MEDIA").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Tech.class);
        spec = tabHost.newTabSpec("tagname3").setIndicator("TECH").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, USWorld.class);
        spec = tabHost.newTabSpec("tagname4").setIndicator("US & WORLD").setContent(intent);
        tabHost.addTab(spec);
        intent = new Intent().setClass(this, Business.class);
        spec = tabHost.newTabSpec("tagname5").setIndicator("BUSINESS").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Fashion.class);
        spec = tabHost.newTabSpec("tagname6").setIndicator("FASHION").setContent(intent);
        tabHost.addTab(spec);
        intent = new Intent().setClass(this, People.class);
        spec = tabHost.newTabSpec("tagname7").setIndicator("PEOPLE").setContent(intent);
        tabHost.addTab(spec);

        intent = new Intent().setClass(this, Political.class);
        spec = tabHost.newTabSpec("tagname8").setIndicator("POLITICAL").setContent(intent);
        tabHost.addTab(spec);
}

@Override
protected void onPause() {
        super.onPause();
        mLocalActivityManager.dispatchPause(isFinishing()); //you have to manually dispatch the pause msg
}

@Override
protected void onResume() {
        super.onResume();
        mLocalActivityManager.dispatchResume(); //you have to manually dispatch the resume msg
}







    private static View createTabView(final Context context, final String text) {
        View view = LayoutInflater.from(context).inflate(R.layout.tabs_bg, null);
        TextView tv = (TextView) view.findViewById(R.id.tabsText);
        tv.setText(text);
        return view;
    }

    }

1 个答案:

答案 0 :(得分:0)

您的主xml是否包含tabhost视图?