我的代码安卓上的空指针错误



我在运行此安卓代码时遇到空指针错误。这是我的堆栈跟踪

02-06 23:11:57.999  26610-26610/? E/Zygote﹕ MountEmulatedStorage()
02-06 23:11:57.999  26610-26610/? E/Zygote﹕ v2
02-06 23:11:57.999  26610-26610/? I/libpersona﹕ KNOX_SDCARD checking this for 10266
02-06 23:11:57.999  26610-26610/? I/libpersona﹕ KNOX_SDCARD not a persona
02-06 23:11:58.009  26610-26610/? I/SELinux﹕ Function: selinux_compare_spd_ram, SPD-policy is existed. and_ver=SEPF_SM-N910W8_5.1.1 ver=38
02-06 23:11:58.009  26610-26610/? I/SELinux﹕ Function: selinux_compare_spd_ram , priority [1] , priority version is VE=SEPF_SM-N910W8_5.1.1_0038
02-06 23:11:58.009  26610-26610/? E/Zygote﹕ accessInfo : 0
02-06 23:11:58.009  26610-26610/? E/SELinux﹕ [DEBUG] get_category: variable seinfo: default sensitivity: NULL, cateogry: NULL
02-06 23:11:58.019  26610-26610/? I/art﹕ Late-enabling -Xcheck:jni
02-06 23:11:58.049  26610-26610/? D/TimaKeyStoreProvider﹕ TimaSignature is unavailable
02-06 23:11:58.049  26610-26610/? D/ActivityThread﹕ Added TimaKeyStore provider
02-06 23:11:58.129  26610-26610/com.mohitkishore.guidemydough D/SecWifiDisplayUtil﹕ Metadata value : none
02-06 23:11:58.159  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* installDecor mIsFloating : false
02-06 23:11:58.159  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* installDecor flags : -2139029248
02-06 23:11:58.359  26610-26673/com.mohitkishore.guidemydough D/OpenGLRenderer﹕ Use EGL_SWAP_BEHAVIOR_PRESERVED: true
02-06 23:11:58.369  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
02-06 23:11:58.369  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled return false
02-06 23:11:58.389  26610-26610/com.mohitkishore.guidemydough D/SRIB_DCS﹕ log_dcs ThreadedRenderer::initialize entered!
02-06 23:11:58.399  26610-26673/com.mohitkishore.guidemydough I/Adreno﹕ EGLInit: QTI Build: 07/18/15, 588fe7e, I15bf295f1a
02-06 23:11:58.409  26610-26673/com.mohitkishore.guidemydough I/OpenGLRenderer﹕ Initialized EGL, version 1.4
02-06 23:11:58.419  26610-26673/com.mohitkishore.guidemydough D/OpenGLRenderer﹕ Get maximum texture size. GL_MAX_TEXTURE_SIZE is 16384
02-06 23:11:58.419  26610-26673/com.mohitkishore.guidemydough D/OpenGLRenderer﹕ Enabling debug mode 0
02-06 23:11:58.589  26610-26610/com.mohitkishore.guidemydough I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@2c1f0005 time:129876203
02-06 23:11:59.619  26610-26610/com.mohitkishore.guidemydough D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
02-06 23:12:02.079  26610-26610/com.mohitkishore.guidemydough D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
02-06 23:12:04.079  26610-26610/com.mohitkishore.guidemydough D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
02-06 23:12:04.129  26610-26610/com.mohitkishore.guidemydough I/Timeline﹕ Timeline: Activity_launch_request id:com.mohitkishore.guidemydough time:129881742
02-06 23:12:04.159  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* installDecor mIsFloating : false
02-06 23:12:04.159  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* installDecor flags : -2139029248
02-06 23:12:04.179  26610-26610/com.mohitkishore.guidemydough D/SecWifiDisplayUtil﹕ Metadata value : none
02-06 23:12:04.179  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
02-06 23:12:04.179  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled return false
02-06 23:12:04.209  26610-26610/com.mohitkishore.guidemydough D/SRIB_DCS﹕ log_dcs ThreadedRenderer::initialize entered!
02-06 23:12:04.299  26610-26610/com.mohitkishore.guidemydough I/Timeline﹕ Timeline: Activity_idle id: android.os.BinderProxy@3c179391 time:129881916
02-06 23:12:04.569  26610-26610/com.mohitkishore.guidemydough V/ActivityThread﹕ updateVisibility : ActivityRecord{382632f6 token=android.os.BinderProxy@2c1f0005 {com.mohitkishore.guidemydough/com.mohitkishore.guidemydough.UI.HomePage}} show : false
02-06 23:12:05.759  26610-26610/com.mohitkishore.guidemydough D/ViewRootImpl﹕ ViewPostImeInputStage ACTION_DOWN
02-06 23:12:05.819  26610-26610/com.mohitkishore.guidemydough I/Timeline﹕ Timeline: Activity_launch_request id:com.mohitkishore.guidemydough time:129883434
02-06 23:12:05.849  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* installDecor mIsFloating : false
02-06 23:12:05.849  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* installDecor flags : -2139029248
02-06 23:12:05.859  26610-26610/com.mohitkishore.guidemydough D/AbsListView﹕ Get MotionRecognitionManager
02-06 23:12:05.859  26610-26610/com.mohitkishore.guidemydough E/MotionRecognitionManager﹕ mSContextService = android.hardware.scontext.ISContextService$Stub$Proxy@298ca5da
02-06 23:12:05.859  26610-26610/com.mohitkishore.guidemydough E/MotionRecognitionManager﹕ motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@2c073a0b
02-06 23:12:05.859  26610-26610/com.mohitkishore.guidemydough E/MotionRecognitionManager﹕ motionService = com.samsung.android.motion.IMotionRecognitionService$Stub$Proxy@2c073a0b
02-06 23:12:05.879  26610-26610/com.mohitkishore.guidemydough D/SecWifiDisplayUtil﹕ Metadata value : none
02-06 23:12:05.879  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled mFloatingMenuBtn : null
02-06 23:12:05.879  26610-26610/com.mohitkishore.guidemydough D/PhoneWindow﹕ *FMB* isFloatingMenuEnabled return false
02-06 23:12:05.929  26610-26610/com.mohitkishore.guidemydough D/AbsListView﹕ try again to check child on obtainview
02-06 23:12:05.929  26610-26610/com.mohitkishore.guidemydough D/AbsListView﹕ child is null again
02-06 23:12:05.929  26610-26610/com.mohitkishore.guidemydough D/AbsListView﹕ position = 0
02-06 23:12:05.929  26610-26610/com.mohitkishore.guidemydough D/AbsListView﹕ mAdapter =Adaptors.ProgressAdaptor@1bb65b63
02-06 23:12:05.929  26610-26610/com.mohitkishore.guidemydough D/AbsListView﹕ getChildCount = 0
02-06 23:12:05.929  26610-26610/com.mohitkishore.guidemydough D/AbsListView﹕ mAdapter.getCount = 3
02-06 23:12:05.939  26610-26610/com.mohitkishore.guidemydough D/AndroidRuntime﹕ Shutting down VM
02-06 23:12:05.939  26610-26610/com.mohitkishore.guidemydough E/AndroidRuntime﹕ FATAL EXCEPTION: main
    Process: com.mohitkishore.guidemydough, PID: 26610
    java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.ViewGroup$LayoutParams android.view.View.getLayoutParams()' on a null object reference
            at android.widget.ListView.measureScrapChild(ListView.java:1210)
            at android.widget.ListView.measureHeightOfChildren(ListView.java:1292)
            at android.widget.ListView.onMeasure(ListView.java:1202)
            at android.view.View.measure(View.java:18855)
            at android.widget.RelativeLayout.measureChild(RelativeLayout.java:698)
            at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:482)
            at android.view.View.measure(View.java:18855)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at android.view.View.measure(View.java:18855)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
            at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1435)
            at android.widget.LinearLayout.measureVertical(LinearLayout.java:721)
            at android.widget.LinearLayout.onMeasure(LinearLayout.java:612)
            at android.view.View.measure(View.java:18855)
            at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5908)
            at android.widget.FrameLayout.onMeasure(FrameLayout.java:436)
            at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:3072)
            at android.view.View.measure(View.java:18855)
            at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2380)
            at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1412)
            at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1655)
            at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1297)
            at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6981)
            at android.view.Choreographer$CallbackRecord.run(Choreographer.java:777)
            at android.view.Choreographer.doCallbacks(Choreographer.java:590)
            at android.view.Choreographer.doFrame(Choreographer.java:560)
            at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:763)
            at android.os.Handler.handleCallback(Handler.java:739)
            at android.os.Handler.dispatchMessage(Handler.java:95)
            at android.os.Looper.loop(Looper.java:145)
            at android.app.ActivityThread.main(ActivityThread.java:6872)
            at java.lang.reflect.Method.invoke(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:372)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)

这也是我的代码:

package com.mohitkishore.guidemydough.UI;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;
import com.mohitkishore.guidemydough.R;
import Adaptors.ProgressAdaptor;
import Model.Goals;
public class ProgressActivity extends ListActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_progress);
        TextView progressItemNameTextView1 = new TextView(ProgressActivity.this);
        TextView progressDescriptionTextView1 = new TextView(ProgressActivity.this);
        ImageView progressItemImageView1 = new ImageView(this);
        TextView progressItemNameTextView2 = new TextView(this);
        TextView progressDescriptionTextView2 = new TextView(this);
        ImageView progressItemImageView2 = new ImageView(this);

        TextView progressItemNameTextView3 = new TextView(this);
        TextView progressDescriptionTextView3 = new TextView(this);
        ImageView progressItemImageView3 = new ImageView(this);

        progressItemNameTextView1.setText("Vacation");
        progressDescriptionTextView1.setText("Test 1");
        progressItemNameTextView2.setText("Home loan");
        progressDescriptionTextView2.setText("Test 2");
        progressItemNameTextView3.setText("Car loan");
        progressDescriptionTextView3.setText("Test 3");

        progressItemImageView1.setImageResource(R.drawable.goal_car);

        progressItemImageView2.setImageResource(R.drawable.goal_home);

        progressItemImageView3.setImageResource(R.drawable.goal_vaccation);
        Goals goal1 = new Goals( progressItemImageView1, progressItemNameTextView1, progressDescriptionTextView1);
        Goals goal2 = new Goals( progressItemImageView2, progressItemNameTextView2, progressDescriptionTextView2);
        Goals goal3 = new Goals( progressItemImageView3, progressItemNameTextView3, progressDescriptionTextView3);
        Goals[] goals = {goal1, goal2, goal3};
        ProgressAdaptor adaptor = new ProgressAdaptor(this, goals);
        setListAdapter(adaptor);
    }
}

这是我的 XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                xmlns:tools="http://schemas.android.com/tools"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:paddingLeft="@dimen/activity_horizontal_margin"
                android:paddingRight="@dimen/activity_horizontal_margin"
                android:paddingTop="@dimen/activity_vertical_margin"
                android:paddingBottom="@dimen/activity_vertical_margin"
                android:background="@color/app_background_color"
                tools:context="com.mohitkishore.guidemydough.UI.ProgressActivity">

    <ListView
        android:id="@id/android:list"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />
</RelativeLayout>

也很抱歉这里没有理由的很多工资法。我正在参加黑客马拉松,这就是我所拥有的一切

您可以从

文件中获取宽度和高度dimens.xml

int width = (int) getResources().getDimension(R.dimen.width);
int height = (int) getResources().getDimension(R.dimen.height);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(width, height);
progressItemNameTextView1.setLayoutParams(params);

并在dimens.xml文件中定义宽度和高度

<dimen name="width">50dp</dimen>
<dimen name="height">50dp</dimen>

希望它会起作用。

最新更新