应用程序在启动时崩溃

时间:2015-01-07 13:15:49

标签: java android eclipse android-layout nullpointerexception

我的应用程序崩溃时要膨胀DashActivity。我正在从布局文件中膨胀视图,如图所示。它包含gridview。

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"    
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="horizontal"
        android:background="@drawable/dashbackground" >

 <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"            
        android:orientation="horizontal"
        android:baselineAligned="false" > 


        <LinearLayout
        android:id="@+id/dummy1" 
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.01"
        android:orientation="horizontal">
        </LinearLayout>

        <LinearLayout
        android:id="@+id/container"
        android:layout_width="0dip"
        android:layout_height="wrap_content"
        android:layout_weight="0.98"
        android:layout_margin="0dp"
        android:orientation="vertical">         

                    <LinearLayout 
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"            
                        android:orientation="horizontal"
                        android:layout_marginTop="3dp"                          
                        android:baselineAligned="false"
                        android:background="@drawable/dashpurplestrip">


                        <LinearLayout android:layout_width="0dip"
                                android:layout_height="wrap_content"
                                android:layout_weight="0.5"
                                android:orientation="horizontal"
                                android:baselineAligned="false"
                                android:layout_marginLeft="5dip">                               

                            <ImageView
                                    android:id="@+id/dashsilhouette" 
                                    android:contentDescription="@string/logo"
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:src="@drawable/dashshadow"
                                    android:layout_marginTop="10dip"
                                    android:layout_marginLeft="10dip" />

                        </LinearLayout>    

                        <RelativeLayout 
                                android:layout_width="0dip"
                                android:layout_height="wrap_content"
                                android:layout_weight="0.5"
                                android:orientation="horizontal"
                                android:baselineAligned="false"
                                android:layout_marginLeft="5dip"
                                android:layout_marginTop="10dip">                               


                                <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/dashleft1" />

                                <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/dashleft2"
                                android:layout_below="@id/dashleft1" />

                                <TextView
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:id="@+id/dashleft3"
                                android:layout_below="@id/dashleft2" />
                            </RelativeLayout>                           

                    </LinearLayout>
                <TextView
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:id="@+id/toplabel"
                    android:background="@drawable/categorystrip" />

                <GridView
                    android:id="@+id/gridview"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:verticalSpacing="4dp"
                    android:horizontalSpacing="4dp"
                    android:stretchMode="columnWidth"
                    android:numColumns="3"
                    android:layout_marginTop="4dip"
                    android:background="@drawable/dashbackground" />        


            </LinearLayout> 

        <LinearLayout
        android:id="@+id/dummy2" 
        android:layout_width="0dip"
        android:layout_height="match_parent"
        android:layout_weight="0.01"
        android:orientation="horizontal">
        </LinearLayout>

   

gridview中的切片从xml文件中膨胀      

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/dashtileimage"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:scaleType="centerCrop"
            android:contentDescription="@string/logo" />



        <TextView
                android:id="@+id/tilecontent"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:textSize="16sp"
                android:textStyle="bold"
                android:layout_marginLeft="10dip"
                android:layout_toRightOf="@id/dashtileimage"
                />        

</RelativeLayout>

运行应用程序时,我得到以下logcat。

01-07 13:47:52.298: E/AndroidRuntime(2163): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.test.test/com.test.test.BoardActivity}: android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2225)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.ActivityThread.access$600(ActivityThread.java:151)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.os.Handler.dispatchMessage(Handler.java:99)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.os.Looper.loop(Looper.java:153)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.ActivityThread.main(ActivityThread.java:5096)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at java.lang.reflect.Method.invokeNative(Native Method)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at java.lang.reflect.Method.invoke(Method.java:511)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:790)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at dalvik.system.NativeStart.main(Native Method)
01-07 13:47:52.298: E/AndroidRuntime(2163): Caused by: android.view.InflateException: Binary XML file line #3: Error inflating class <unknown>
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.LayoutInflater.createView(LayoutInflater.java:613)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:56)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:410)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.Activity.setContentView(Activity.java:2094)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at com.test.test.BoardActivity.onCreate(BoardActivity.java:82)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.Activity.performCreate(Activity.java:5244)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1082)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
01-07 13:47:52.298: E/AndroidRuntime(2163):     ... 11 more
01-07 13:47:52.298: E/AndroidRuntime(2163): Caused by: java.lang.reflect.InvocationTargetException
01-07 13:47:52.298: E/AndroidRuntime(2163):     at java.lang.reflect.Constructor.constructNative(Native Method)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.LayoutInflater.createView(LayoutInflater.java:587)
01-07 13:47:52.298: E/AndroidRuntime(2163):     ... 23 more
01-07 13:47:52.298: E/AndroidRuntime(2163): Caused by: java.lang.OutOfMemoryError
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.graphics.BitmapFactory.nativeDecodeAsset(Native Method)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:519)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.graphics.BitmapFactory.decodeResourceStream(BitmapFactory.java:372)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.graphics.drawable.Drawable.createFromResourceStream(Drawable.java:781)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.content.res.Resources.loadDrawable(Resources.java:1969)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.content.res.TypedArray.getDrawable(TypedArray.java:601)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.View.<init>(View.java:3348)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.view.ViewGroup.<init>(ViewGroup.java:427)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.widget.FrameLayout.<init>(FrameLayout.java:101)
01-07 13:47:52.298: E/AndroidRuntime(2163):     at android.widget.FrameLayout.<init>(FrameLayout.java:97)
01-07 13:47:52.298: E/AndroidRuntime(2163):     ... 26 more

以下是显示onCreate方法的整个活动

public class BoardActivity extends Activity{

TextView tv1, toplabel; 
TextView dashl1,dashl2,dashl3;  
ImageView dS;
GridView grid;      
SharedPreferences prefs;
String username = "";
String appId = "";
String userAppStatus = "";
String phoneNumber = "";    
Preferences myPref;     
HashMap<String, String> gridmap;    
Utilities ut;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.dashboard);
}

@SuppressWarnings("unchecked")
@Override
protected void onStart() 

{
    // TODO Auto-generated method stub
    super.onStart();

    prefs = getApplicationContext().getSharedPreferences("App", Context.MODE_PRIVATE);
    username = prefs.getString("username", "");
    appId = prefs.getString("appID", "");
    phoneNumber = prefs.getString("phone", "0000000000");       

    ut = new Utilities(getApplicationContext());
    myPref = new Preferences();
    Maps mps = new Maps();
    gridmap = mps.getMap();

    dS = (ImageView)findViewById(R.id.dashsilhouette);

    dashl1 = (TextView)findViewById(R.id.dashleft1);
    dashl2 = (TextView)findViewById(R.id.dashleft2);
    dashl3 = (TextView)findViewById(R.id.dashleft3);

    toplabel = (TextView)findViewById(R.id.toplabel);
    toplabel.setText("CATEGORIES");     

    dashl1.setText(username);
    dashl1.setGravity(Gravity.CENTER);
    dashl2.setText(phoneNumber);
    dashl2.setGravity(Gravity.CENTER);  


    grid=(GridView)findViewById(R.id.gridview);

    MyGridAdapter adapter = new MyGridAdapter(gridmap);
    grid.setAdapter(adapter);

}   

        //========================== gridview hashmap adapter ==============================

        public class MyGridAdapter extends BaseAdapter {
            private final ArrayList mData;                  

            public MyGridAdapter(Map<String, String> map) {
                mData = new ArrayList();
                mData.addAll(map.entrySet());                 
            }

            @Override
            public int getCount() {
                return mData.size();
            }

            @Override
            public Map.Entry<String, String> getItem(int position) {
                return (Map.Entry) mData.get(position);
            }

            @Override
            public long getItemId(int position) {
                // TODO implement you own logic with ID
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                final View result;

                if (convertView == null) {
                    result = LayoutInflater.from(parent.getContext()).inflate(R.layout.hometile,    parent, false);
                } else {
                    result = convertView;
                }

                final Map.Entry<String, String> item = getItem(position);

                TextView text=(TextView)result.findViewById(R.id.tilecontent);
                TextView count = (TextView)result.findViewById(R.id.count);
                ImageView image=(ImageView)result.findViewById(R.id.tileimage);

                text.setText(item.getKey());
                count.setText("  " + String.valueOf(position));
                image.setImageResource(ut.getId(item.getValue(), BoardActivity.this));


                result.setOnClickListener(new OnClickListener(){
                    @Override
                    public void onClick(View v) {           
                            {
                                //get request here
                            }                           
                    }
                });
                return result;
            }
        }



//###########################################################################################
    @Override
    protected void onResume()
    {
        super.onResume();
    }

    @Override
    protected void onPause()
    {
        super.onPause();
    }

}       

0 个答案:

没有答案