我是stackOverflow和Android开发的新手。请有人帮帮我吗?
我已经按照 this 教程从我目前正在处理的新应用中的按钮或imageView打开相机,但是当我想运行应用程序来测试它时,它在我的Android设备上给我以下错误; "不幸的是,(myAppName)已停止。"。我使用Eclipse作为我的IDE。
logcat的:
05-03 17:19:32.596: I/Process(785): Sending signal. PID: 785 SIG: 9
05-03 17:30:48.661: D/AndroidRuntime(1599): Shutting down VM
05-03 17:30:48.661: W/dalvikvm(1599): threadid=1: thread exiting with uncaught exception (group=0x41f3d2a0)
05-03 17:30:48.666: E/AndroidRuntime(1599): FATAL EXCEPTION: main
05-03 17:30:48.666: E/AndroidRuntime(1599): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.simon.cameratoets/com.simon.cameratoets.MainActivity}: java.lang.NullPointerException
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.ActivityThread.access$700(ActivityThread.java:140)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.os.Handler.dispatchMessage(Handler.java:99)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.os.Looper.loop(Looper.java:137)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.ActivityThread.main(ActivityThread.java:4921)
05-03 17:30:48.666: E/AndroidRuntime(1599): at java.lang.reflect.Method.invokeNative(Native Method)
05-03 17:30:48.666: E/AndroidRuntime(1599): at java.lang.reflect.Method.invoke(Method.java:511)
05-03 17:30:48.666: E/AndroidRuntime(1599): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1027)
05-03 17:30:48.666: E/AndroidRuntime(1599): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
05-03 17:30:48.666: E/AndroidRuntime(1599): at dalvik.system.NativeStart.main(Native Method)
05-03 17:30:48.666: E/AndroidRuntime(1599): Caused by: java.lang.NullPointerException
05-03 17:30:48.666: E/AndroidRuntime(1599): at com.simon.cameratoets.MainActivity.onCreate(MainActivity.java:21)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.Activity.performCreate(Activity.java:5206)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
05-03 17:30:48.666: E/AndroidRuntime(1599): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2074)
05-03 17:30:48.666: E/AndroidRuntime(1599): ... 11 more
05-03 17:35:57.711: I/Process(1599): Sending signal. PID: 1599 SIG: 9
我的主要活动:
package com.simon.cameratoets;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class MainActivity extends Activity {
ImageView imgFavorite;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imgFavorite = (ImageView)findViewById(R.id.imageView1);
imgFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
open();
}
});
}
public void open(){
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, 0);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Bitmap bp = (Bitmap) data.getExtras().get("data");
imgFavorite.setImageBitmap(bp);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
清单文件:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.simon.cameratoets"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.simon.cameratoets.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
任何帮助将不胜感激,如果这篇文章已经过去发布,我很抱歉。当我想要一个按钮在使用Intent
时转到另一个活动时,同样的事情发生同样的情况,这是不寻常的,我确保其他活动在清单文件中。在这种相机的情况下 - 我只是按照上面的链接示例,它不想工作。
答案 0 :(得分:1)
imgFavorite = (ImageView)findViewById(R.id.imageView1);
返回null
。
您确定activity_main.xml
包含身份ImageView
的{{1}}吗?