gpt4 book ai didi

java - Android 程序不想运行 - "Unfortunately, (myAppName) has stopped."

转载 作者:行者123 更新时间:2023-12-01 13:02:08 24 4
gpt4 key购买 nike

我是 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

我的主要 Activity :

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;
}
}

list 文件:

<?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 时想要一个按钮转到另一个 Activity 时,会发生同样的事情,这是不寻常的,并且我确实确保另一个 Activity 位于 list 文件中。在相机的情况下 - 我只是按照上面链接中的示例进行操作,但它不起作用。

最佳答案

imgFavorite = (ImageView)findViewById(R.id.imageView1); 返回 null

您确定 activity_main.xml 包含 ID 为 imageView1ImageView 吗?

关于java - Android 程序不想运行 - "Unfortunately, (myAppName) has stopped.",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23446818/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com