布局中的空指针异常

时间:2014-08-03 20:32:35

标签: java android android-layout

我在布局文件中发现了Null Pointer Exception。

我使用ViewPager因为我发现NullPointerException的原因。

以下是布局的xml代码。

<?xml version="1.0" encoding="utf-8"?>
<com.mindsoulmatter.iaffirmme.ViewPagerParallax xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewpager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#70000000"
    android:paddingBottom="2dp"
    android:textColor="#D7D6D6" >

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/__Weekly_pager_title_strip"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#70000000"
        android:paddingTop="18dp"
        android:textColor="#FFFFFF"
        android:textSize="26sp" />

    <View
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#ffffff" />

</com.mindsoulmatter.iaffirmme.ViewPagerParallax>

我在图形布局中遇到以下错误:

java.lang.NullPointerException
Exception details are logged in Window > Show View > Error Log

java.lang.NullPointerException
    at android.graphics.Canvas.throwIfCannotDraw(Canvas.java:1083)
    at android.graphics.Canvas.drawBitmap(Canvas.java:1201)
    at com.mindsoulmatter.iaffirmme.ViewPagerParallax.onDraw(ViewPagerParallax.java:204)
    at android.view.View.draw(View.java:14465)
    at android.support.v4.view.ViewPager.draw(ViewPager.java:2171)
    at android.view.View.draw(View.java:14350)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14348)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14468)
    at android.view.View.draw(View.java:14350)
    at android.view.ViewGroup.drawChild(ViewGroup.java:3103)
    at android.view.ViewGroup.dispatchDraw(ViewGroup.java:2940)
    at android.view.View.draw(View.java:14468)

我可以运行应用程序但是当我打开这个布局时,应用程序会崩溃。

使用以下片段活动调用此布局:

package com.mindsoulmatter.iaffirmme;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import java.util.Vector;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.util.Log;
import android.view.Window;
import android.widget.TextView;

public class Activity_Fragments extends FragmentActivity {
    List<Fragment> fragments;
    Adapter_ViewPager adapter;
    ViewPagerParallax pager;
    String restoredText;
    private static String[] titles = new String[] { "Custom", "Affirmations",
            "Favorites" };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.requestWindowFeature(Window.FEATURE_NO_TITLE);

        setContentView(R.layout.adapter_fragment);
        RateThisApp.onStart(this);
        // Show a dialog if criteria is satisfied
        RateThisApp.showRateDialogIfNeeded(this);

        overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_left);
        fragments = new Vector<Fragment>();
        fragments.add(Fragment.instantiate(this,
                Fragment_Affirm_Custom.class.getName()));
        fragments.add(Fragment.instantiate(this,
                Fragment_Affirm.class.getName()));
        fragments.add(Fragment.instantiate(this,
                Fragment_Affirm_Favourites.class.getName()));

        // registerReceiver(broadcastCoverReceiverpara, new IntentFilter(
        // Music_service.BROADCAST_COVERPARALLAX));

        SharedPreferences prefs = getApplicationContext().getSharedPreferences(
                "udata", MODE_PRIVATE);
        this.restoredText = prefs.getString("language", null);

        if (restoredText == "english") {
            titles = new String[] { "Custom", "Affirmations", "Favorites" };
        } else if (restoredText == "spanish") {
            titles = new String[] { "Costumbre", "Afirmaciones", "Favoritos " };
        } else if (restoredText == "french") {
            titles = new String[] { "Coutume", "Affirmations", "Favoris" };
        } else {
            titles = new String[] { "Custom", "Affirmations", "Favorites" };
        }

        adapter = new Adapter_ViewPager(super.getSupportFragmentManager(),
                fragments, titles);

        pager = (ViewPagerParallax) super.findViewById(R.id.viewpager);

        pager.set_max_pages(3);
        PagerTitleStrip _Title = (PagerTitleStrip) findViewById(R.id.__Weekly_pager_title_strip);
        Typeface font = Typeface.createFromAsset(this.getAssets(),
                "fonts/julius-sans-one.ttf");
        for (int counter = 0; counter < _Title.getChildCount(); counter++) {

            if (_Title.getChildAt(counter) instanceof TextView) {
                ((TextView) _Title.getChildAt(counter)).setTypeface(font);
            }
        }
        /*int arr[] = { R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a4, R.raw.a5,
                R.raw.a6, R.raw.a7, R.raw.a8, R.raw.a9, R.raw.a10, R.raw.a11,
                R.raw.a12, R.raw.a13, R.raw.a14, R.raw.a15, R.raw.a16,
                R.raw.a17, R.raw.a18, R.raw.a19, R.raw.a20, R.raw.a21,
                R.raw.a22, R.raw.a23, R.raw.a24, R.raw.a25, R.raw.a26,
                R.raw.a27, R.raw.a28, R.raw.a29, R.raw.a30, R.raw.a31,
                R.raw.a32, R.raw.a33, R.raw.a34, R.raw.a35, R.raw.a36,
                R.raw.a37, R.raw.a38, R.raw.a39, R.raw.a40, R.raw.a41 };*/

        int arr[] = { R.raw.a1, R.raw.a2, R.raw.a3, R.raw.a4,R.raw.a5};

        int x = (int) (Math.random() * ((4) + 1));
        pager.setBackgroundAsset(arr[x]);
        // pager.setPageTransformer(true, new ZoomOutPageTransformer());

        pager.setAdapter(adapter);
        pager.setOffscreenPageLimit(1);

        pager.setCurrentItem(1);

    }

    // private BroadcastReceiver broadcastCoverReceiverpara = new
    // BroadcastReceiver() {
    // @Override
    // public void onReceive(Context context, Intent serviceIntent) {
    // pager.setBackgroundAsset(serviceIntent.getStringExtra( "coverpara"));

    // }
    // };

    @Override
    public void finish() {
        super.finish();
        overridePendingTransition(R.anim.slide_in_right, R.anim.slide_out_right);
    }
}

调用pager.setBackgroundAsset(arr[x])时出错。

任何人都可以帮帮我吗?

3 个答案:

答案 0 :(得分:2)

我找到了解决方案。

图像本身存在问题。

有些图像本身已经破碎。

谢谢你们的时间。

答案 1 :(得分:0)

看起来像

int x = (int) (Math.random() * ((4) + 1));

给你错误的数组ID

答案 2 :(得分:0)

自定义View可以使用此方法检测它们是否在布局编辑器(在开发人员工具中)内运行:

View.isInEditMode()

显然,ViewPagerParallax视图不是,因此在布局编辑器中使用非法方法。