这显然是一个被多次询问的问题。但我无法在任何地方找到任何解决方案。我正在创建一个名为awesomequotes的应用程序,所有这个应用程序都包含图像,但每当我点击图像以全屏查看它显示错误“不幸的是,aweseomequotes已经停止”。我跟踪了它显示的logcat错误....
09-21 15:45:45.950: E/AndroidRuntime(6967): FATAL EXCEPTION: main
09-21 15:45:45.950: E/AndroidRuntime(6967): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.awesomequotes/com.example.awesomequotes.FullScreenViewActivity}: java.lang.NullPointerException
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.ActivityThread.access$600(ActivityThread.java:151)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.os.Handler.dispatchMessage(Handler.java:99)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.os.Looper.loop(Looper.java:155)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.ActivityThread.main(ActivityThread.java:5520)
09-21 15:45:45.950: E/AndroidRuntime(6967): at java.lang.reflect.Method.invokeNative(Native Method)
09-21 15:45:45.950: E/AndroidRuntime(6967): at java.lang.reflect.Method.invoke(Method.java:511)
09-21 15:45:45.950: E/AndroidRuntime(6967): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
09-21 15:45:45.950: E/AndroidRuntime(6967): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
09-21 15:45:45.950: E/AndroidRuntime(6967): at dalvik.system.NativeStart.main(Native Method)
09-21 15:45:45.950: E/AndroidRuntime(6967): Caused by: java.lang.NullPointerException
09-21 15:45:45.950: E/AndroidRuntime(6967): at com.example.awesomequotes.FullScreenViewActivity.onCreate(FullScreenViewActivity.java:54)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.Activity.performCreate(Activity.java:5066)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1101)
09-21 15:45:45.950: E/AndroidRuntime(6967): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2311)
09-21 15:45:45.950: E/AndroidRuntime(6967): ... 11 more
我有各种类别,但在这里我只复制FullScreenActivity.java和布局文件的代码以进行全屏活动。
FullScreenActivity.java
package com.example.awesomequotes;
import app.AppController;
import picasa.model.Wallpaper;
import util.Utils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;
import android.widget.ProgressBar;
import android.widget.Toast;
import com.android.volley.Request.Method;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.ImageLoader.ImageContainer;
import com.android.volley.toolbox.ImageLoader.ImageListener;
import com.android.volley.toolbox.JsonObjectRequest;
public class FullScreenViewActivity extends Activity implements OnClickListener {
private static final String TAG = FullScreenViewActivity.class
.getSimpleName();
public static final String TAG_SEL_IMAGE = "selectedImage";
private Wallpaper selectedPhoto;
private ImageView fullImageView;
private LinearLayout llSetWallpaper, llDownloadWallpaper;
private Utils utils;
private ProgressBar pbLoader;
// Picasa JSON response node keys
private static final String TAG_ENTRY = "entry",
TAG_MEDIA_GROUP = "media$group",
TAG_MEDIA_CONTENT = "media$content", TAG_IMG_URL = "url",
TAG_IMG_WIDTH = "width", TAG_IMG_HEIGHT = "height";
@Override
protected void onCreate(Bundle savedInstanceState) {
llSetWallpaper.setOnClickListener(this);
super.onCreate(savedInstanceState);
llDownloadWallpaper.setOnClickListener(this);
setContentView(R.layout.activity_fullscreen_image);
fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
llSetWallpaper = (LinearLayout) findViewById(R.id.llSetWallpaper);
llDownloadWallpaper = (LinearLayout) findViewById(R.id.llDownloadWallpaper);
pbLoader = (ProgressBar) findViewById(R.id.pbLoader);
// hide the action bar in fullscreen mode
getActionBar().hide();
utils = new Utils(getApplicationContext());
// layout click listeners
llSetWallpaper.setOnClickListener(this);
llDownloadWallpaper.setOnClickListener(this);
// setting layout buttons alpha/opacity
llSetWallpaper.getBackground().setAlpha(70);
llDownloadWallpaper.getBackground().setAlpha(70);
Intent i = getIntent();
selectedPhoto = (Wallpaper) i.getSerializableExtra(TAG_SEL_IMAGE);
// check for selected photo null
if (selectedPhoto != null) {
// fetch photo full resolution image by making another json request
fetchFullResolutionImage();
} else {
Toast.makeText(getApplicationContext(),
getString(R.string.msg_unknown_error), Toast.LENGTH_SHORT)
.show();
}
}
/**
* Fetching image fullresolution json
* */
private void fetchFullResolutionImage() {
String url = selectedPhoto.getPhotoJson();
// show loader before making request
pbLoader.setVisibility(View.VISIBLE);
llSetWallpaper.setVisibility(View.GONE);
llDownloadWallpaper.setVisibility(View.GONE);
// volley's json obj request
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Method.GET, url,
null, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
Log.d(TAG,
"Image full resolution json: "
+ response.toString());
try {
// Parsing the json response
JSONObject entry = response
.getJSONObject(TAG_ENTRY);
JSONArray mediacontentArry = entry.getJSONObject(
TAG_MEDIA_GROUP).getJSONArray(
TAG_MEDIA_CONTENT);
JSONObject mediaObj = (JSONObject) mediacontentArry
.get(0);
String fullResolutionUrl = mediaObj
.getString(TAG_IMG_URL);
// image full resolution widht and height
final int width = mediaObj.getInt(TAG_IMG_WIDTH);
final int height = mediaObj.getInt(TAG_IMG_HEIGHT);
Log.d(TAG, "Full resolution image. url: "
+ fullResolutionUrl + ", w: " + width
+ ", h: " + height);
ImageLoader imageLoader = AppController
.getInstance().getImageLoader();
// We download image into ImageView instead of
// NetworkImageView to have callback methods
// Currently NetworkImageView doesn't have callback
// methods
imageLoader.get(fullResolutionUrl,
new ImageListener() {
@Override
public void onErrorResponse(
VolleyError arg0) {
Toast.makeText(
getApplicationContext(),
getString(R.string.msg_wall_fetch_error),
Toast.LENGTH_LONG).show();
}
@Override
public void onResponse(
ImageContainer response,
boolean arg1) {
if (response.getBitmap() != null) {
// load bitmap into imageview
fullImageView
.setImageBitmap(response
.getBitmap());
adjustImageAspect(width, height);
// hide loader and show set &
// download buttons
pbLoader.setVisibility(View.GONE);
llSetWallpaper
.setVisibility(View.VISIBLE);
llDownloadWallpaper
.setVisibility(View.VISIBLE);
}
}
});
} catch (JSONException e) {
e.printStackTrace();
Toast.makeText(getApplicationContext(),
getString(R.string.msg_unknown_error),
Toast.LENGTH_LONG).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
// unable to fetch wallpapers
// either google username is wrong or
// devices doesn't have internet connection
Toast.makeText(getApplicationContext(),
getString(R.string.msg_wall_fetch_error),
Toast.LENGTH_LONG).show();
}
});
// Remove the url from cache
AppController.getInstance().getRequestQueue().getCache().remove(url);
// Disable the cache for this url, so that it always fetches updated
// json
jsonObjReq.setShouldCache(false);
// Adding request to request queue
AppController.getInstance().addToRequestQueue(jsonObjReq);
}
/**
* Adjusting the image aspect ration to scroll horizontally, Image height
* will be screen height, width will be calculated respected to height
* */
@SuppressWarnings("deprecation")
@SuppressLint("NewApi")
private void adjustImageAspect(int bWidth, int bHeight) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
if (bWidth == 0 || bHeight == 0)
return;
int sHeight = 0;
if (android.os.Build.VERSION.SDK_INT >= 13) {
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
sHeight = size.y;
} else {
Display display = getWindowManager().getDefaultDisplay();
sHeight = display.getHeight();
}
int new_width = (int) Math.floor((double) bWidth * (double) sHeight
/ (double) bHeight);
params.width = new_width;
params.height = sHeight;
Log.d(TAG, "Fullscreen image new dimensions: w = " + new_width
+ ", h = " + sHeight);
fullImageView.setLayoutParams(params);
}
/**
* View click listener
* */
@Override
public void onClick(View v) {
Bitmap bitmap = ((BitmapDrawable) fullImageView.getDrawable())
.getBitmap();
switch (v.getId()) {
// button Download Wallpaper tapped
case R.id.llDownloadWallpaper:
utils.saveImageToSDCard(bitmap);
break;
// button Set As Wallpaper tapped
case R.id.llSetWallpaper:
utils.setAsWallpaper(bitmap);
break;
default:
break;
}
}
}
这是布局文件
activity_fullscreen_image.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/black" >
<ProgressBar
android:id="@+id/pbLoader"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
</ProgressBar>
<!-- Scroll view for fullscreen preview -->
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgFullscreen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY" />
</LinearLayout>
</HorizontalScrollView>
<!-- Set as wallpaper button -->
<LinearLayout
android:id="@+id/llSetWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
android:background="@drawable/btn_rounded_corner"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ico_apply" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:text="@string/set_wallpaper"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
<!-- Download wallpaper button -->
<LinearLayout
android:id="@+id/llDownloadWallpaper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="10dp"
android:background="@drawable/btn_rounded_corner"
android:gravity="center_vertical"
android:orientation="horizontal" >
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:src="@drawable/ico_download" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:text="@string/download_wallpaper"
android:textColor="@color/white"
android:textSize="18dp" />
</LinearLayout>
</RelativeLayout>
这是清单文件
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.awesomequotes"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="11"
android:targetSdkVersion="18" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.SET_WALLPAPER" />
<application
android:name="app.AppController"
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".SplashActivity"
android:label="@string/app_name"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name=".FullScreenViewActivity"
android:screenOrientation="portrait" >
</activity>
<activity
android:name="info.androidhive.awesomewallpapers.SettingsActivity"
android:label="@string/action_settings"
android:screenOrientation="portrait" >
</activity>
</application>
</manifest>
我真的很感激帮助。我被困在这里已经超过2天了。
谢谢
答案 0 :(得分:0)
在你的onCreate()
中llSetWallpaper.setOnClickListener(this);
super.onCreate(savedInstanceState);
llDownloadWallpaper.setOnClickListener(this);
setContentView(R.layout.activity_fullscreen_image);
fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
llSetWallpaper = (LinearLayout) findViewById(R.id.llSetWallpaper);
llDownloadWallpaper = (LinearLayout) findViewById(R.id.llDownloadWallpaper);
pbLoader = (ProgressBar) findViewById(R.id.pbLoader);
在找到视图之前设置侦听器。你应该先findViewById
答案 1 :(得分:0)
您的问题出在onCreate
方法中:
llSetWallpaper.setOnClickListener(this);
super.onCreate(savedInstanceState);
llDownloadWallpaper.setOnClickListener(this);
setContentView(R.layout.activity_fullscreen_image);
fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
llSetWallpaper = (LinearLayout) findViewById(R.id.llSetWallpaper);
llDownloadWallpaper = (LinearLayout) findViewById(R.id.llDownloadWallpaper);
pbLoader = (ProgressBar) findViewById(R.id.pbLoader);
您在设置项目之前设置了侦听器,它应该是这样的:
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_fullscreen_image);
//First you instantiate the items
fullImageView = (ImageView) findViewById(R.id.imgFullscreen);
llSetWallpaper = (LinearLayout) findViewById(R.id.llSetWallpaper);
llDownloadWallpaper = (LinearLayout) findViewById(R.id.llDownloadWallpaper);
pbLoader = (ProgressBar) findViewById(R.id.pbLoader);
//Then you set the listener
llDownloadWallpaper.setOnClickListener(this);