如何修复(无法启动活动java.lang.NullPointerException?)

时间:2016-04-29 02:43:05

标签: java android

直到昨天,一切都运转良好,并且无处不在出现以下问题。单击ImageView时,此图像应显示/显示图像的细节。但是,如果单击ImageView,结果不幸的是,App已停止

显示图片代码:

   package com.vgm_versi2rev;

import android.app.Activity;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.graphics.Point;
import android.os.Build;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

import com.vgm_versi2rev.ImageResizer;
import com.vgm_versi2rev.R;
import com.vgm_versi2rev.MyImage;


import android.app.Activity;
import android.content.Intent;
import android.graphics.Point;
import android.os.Bundle;
import android.view.Display;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import org.json.JSONException;
import org.json.JSONObject;

public class DisplayImage extends Activity{
    private MyImage image;
    private ImageView imageView;
    private TextView description;
    private String jstring;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_display_image);
        imageView = (ImageView) findViewById(R.id.display_image_view);
        description = (TextView) findViewById(R.id.text_view_description);
        Bundle extras = getIntent().getExtras();

        if (extras != null) {
            jstring = extras.getString("IMAGE");
        }
        image = getMyImage(jstring);
        description.setText(image.toString());
        Display display = getWindowManager().getDefaultDisplay();
        Point size = new Point();
        display.getSize(size);
        int width = size.x;
        int height = size.y;
        imageView.setImageBitmap(ImageResizer
                .decodeSampledBitmapFromFile(image.getPath(), width, height));
    }


    private MyImage getMyImage(String image) {
        try {
            JSONObject job = new JSONObject(image);
            return (new MyImage(job.getString("path"),job.getString("title"),
                    job.getString("datetime"),job.getString("description"),
                    job.getString("longitude"),job.getString("latitude")));
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return null;
    }


    public void btnBackOnClick(View v) {
        startActivity(new Intent(this, MainActivityPhoto.class));
        finish();
    }

    public void btnDeleteOnClick(View v) {
        DAOdb db = new DAOdb(this);
        db.deleteImage(image);
        db.close();
        startActivity(new Intent(this, MainActivityPhoto.class));
        finish();
    }

    @Override protected void onSaveInstanceState(Bundle outState) {
        // Save the user's current game state
        if (jstring != null) {
            outState.putString("jstring", jstring);
        }
        // Always call the superclass so it can save the view hierarchy state
        super.onSaveInstanceState(outState);
    }

    @Override protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // Always call the superclass so it can restore the view hierarchy
        super.onRestoreInstanceState(savedInstanceState);

        // Restore state members from saved instance
        if (savedInstanceState.containsKey("jstring")) {
            jstring = savedInstanceState.getString("jstring");
        }
    }
}

按下显示图像(ImageView)时返回的错误:

> 04-29 09:36:00.417: E/AndroidRuntime(30734): FATAL EXCEPTION: main
> 04-29 09:36:00.417: E/AndroidRuntime(30734):
> java.lang.RuntimeException: Unable to start activity
> ComponentInfo{com.vgm_versi2rev/com.vgm_versi2rev.DisplayImage}:
> java.lang.NullPointerException 04-29 09:36:00.417:
> E/AndroidRuntime(30734):  at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2295)
> 04-29 09:36:00.417: E/AndroidRuntime(30734):  at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2349)
> 04-29 09:36:00.417: E/AndroidRuntime(30734):  at
> android.app.ActivityThread.access$700(ActivityThread.java:159) 04-29
> 09:36:00.417: E/AndroidRuntime(30734):    at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:1316)
> 04-29 09:36:00.417: E/AndroidRuntime(30734):  at
> android.os.Handler.dispatchMessage(Handler.java:99) 04-29
> 09:36:00.417: E/AndroidRuntime(30734):    at
> android.os.Looper.loop(Looper.java:137) 04-29 09:36:00.417:
> E/AndroidRuntime(30734):  at
> android.app.ActivityThread.main(ActivityThread.java:5419) 04-29
> 09:36:00.417: E/AndroidRuntime(30734):    at
> java.lang.reflect.Method.invokeNative(Native Method) 04-29
> 09:36:00.417: E/AndroidRuntime(30734):    at
> java.lang.reflect.Method.invoke(Method.java:525) 04-29 09:36:00.417:
> E/AndroidRuntime(30734):  at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1209)
> 04-29 09:36:00.417: E/AndroidRuntime(30734):  at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1025) 04-29
> 09:36:00.417: E/AndroidRuntime(30734):    at
> dalvik.system.NativeStart.main(Native Method) 04-29 09:36:00.417:
> E/AndroidRuntime(30734): Caused by: java.lang.NullPointerException
> 04-29 09:36:00.417: E/AndroidRuntime(30734):  at
> com.vgm_versi2rev.DisplayImage.onCreate(DisplayImage.java:56) 04-29
> 09:36:00.417: E/AndroidRuntime(30734):    at
> android.app.Activity.performCreate(Activity.java:5372) 04-29
> 09:36:00.417: E/AndroidRuntime(30734):    at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1104)
> 04-29 09:36:00.417: E/AndroidRuntime(30734):  at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2257)
> 04-29 09:36:00.417: E/AndroidRuntime(30734):  ... 11 more

我的Android Manifest如下:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.vgm_versi2rev"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="10"
        android:targetSdkVersion="14" />
 <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
     <uses-feature
        android:name="android.hardware.camera"
        android:required="false"/>

    <application
        android:allowBackup="true"
        android:icon="@drawable/vgm"
        android:label="@string/app_name"
         android:theme="@style/AppTheme" >

         <activity
            android:name=".Homepage"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />
                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>

        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

        <activity
            android:name=".Lihat_Data"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DetailActivity">

        </activity>

        <activity
            android:name=".MainActivityPetiole"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>

       <activity
            android:name=".MainActivityPhoto"
            android:label="@string/app_name">
            <intent-filter>
                 <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
        <activity
            android:name=".DisplayImage"

            android:label="@string/title_activity_display_image"
            android:theme="@android:style/Theme.NoTitleBar.Fullscreen">
        </activity>


    </application>

</manifest>

帮助我非常感谢! 最诚挚的问候

1 个答案:

答案 0 :(得分:0)

错误行是56:description.setText(image.toString());

我检查你的功能getMyImage(String image)它看起来像你 在这个函数中有一个循环,

private MyImage getMyImage(String image) {
    try {
        JSONObject job = new JSONObject(image);
        return (new MyImage(job.getString("path"),job.getString("title"),
                job.getString("datetime"),job.getString("description"),
                job.getString("longitude"),job.getString("latitude")));
    } catch (JSONException e) {
        e.printStackTrace();
    }
    return null;
}

一个函数返回自己有不同的参数,我有点困惑,但问题可能出在这个