Android 应用程序在后退按钮上崩溃(performResumeActivity)

发布于 2024-10-21 21:18:34 字数 14574 浏览 6 评论 0原文

我的应用程序目前包含 2 个活动。 。画廊视图的主要活动 。带有 ListView 的 FriendsListActivity

当用户使用后退按钮离开 FriendsListActivity 并返回到 MAIN 活动时,在调试模式下会不断弹出以下错误。

DalvikVM[localhost:8676] 线程 [<1> main](暂停(例外 运行时异常))
ActivityThread.performResumeActivity(IBinder, 布尔值)行:2095
ActivityThread.handleResumeActivity(IBinder, 布尔值,布尔值)行:2110
BinderProxy(ActivityThread$H).handleMessage(消息) 线路:954 ActivityThread$H(处理程序).dispatchMessage(消息) 行:99 Looper.loop() 行:123 ActivityThread.main(String[]) 行: 第3647章 对象[]、类、类[]、类、int、 布尔)行:不可用 [native 方法] Method.invoke(对象, 对象...)行:507
ZygoteInit$MethodAndArgsCaller.run() 行:839 ZygoteInit.main(字符串[]) 线路:597 NativeStart.main(String[]) 行:不是 可用[本机方法] 线程 [<8>活页夹线程#2](正在运行)
线程 [<7>活页夹线程 #1] (运行)

使用 LogCat

03-13 22:01:10.972: 错误/Android运行时(1038):致命 例外:主要 03-13 22:01:10.972: 错误/Android运行时(1038): java.lang.RuntimeException:无法 恢复活动 {com.package.MAIN/com.package.MAIN.MAIN}: java.lang.NullPointerException 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2095) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2110) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.app.ActivityThread$H.handleMessage(ActivityThread.java:954) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.os.Handler.dispatchMessage(Handler.java:99) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.os.Looper.loop(Looper.java:123) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.app.ActivityThread.main(ActivityThread.java:3647) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 java.lang.reflect.Method.invokeNative(本机 方法)03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 java.lang.reflect.Method.invoke(Method.java:507) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 dalvik.system.NativeStart.main(本机 方法)03-13 22:01:10.972: 错误/AndroidRuntime(1038):原因: java.lang.NullPointerException 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 com.package.MAIN.MAIN.onResume(MAIN.java:91) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.app.Activity.performResume(Activity.java:3833) 03-13 22:01:10.972: 错误/AndroidRuntime(1038):位于 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2085) 03-13 22:01:10.972: 错误/Android运行时(1038): ... 10 更多

“变量”面板中的更多信息说明:
这:活动线程
e: 空指针异常
原因:NullPointerException
详细消息:空
堆栈跟踪:空
r: ActivityThread$ActivityClientRecord
活动:

一次 Eclipse 后的主要详细消息 恢复:无法恢复活动(主)

FriendsListActivity 中的代码如下所示

public class FriendsListActivity extends ListActivity {

// ===========================================================
// Fields
// ===========================================================

private ArrayList<Friend> friends = new ArrayList<Friend>();
private FriendsArrayAdapter friendsArrayAdapter;
private ListView listView;

// ===========================================================
// onCreate
// ===========================================================

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.friends_list);

    registerForContextMenu(getListView());
    setButtonNewFriendClickListener();
}

public void generateFriendsList() {
    FriendsService fs = new FriendsService(this);
    friends = fs.getFriendsList();

    listView = (ListView) findViewById(android.R.id.list);
    friendsArrayAdapter = new FriendsArrayAdapter(
       this, R.layout.friend_list_item, friends);
    listView.setAdapter(friendsArrayAdapter);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.friends_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    FriendsService fs = new FriendsService(this);
    Friend f = new Friend();
    f = friends.get(info.position);
    switch (item.getItemId()) {
    case R.id.edit:
        Intent i = new Intent(this, FriendEditActivity.class);
        i.putExtra("userid", f.userId);
        startActivity(i);
        return true;
    case R.id.delete:
        fs.deleteFriend(f.userId);
        generateFriendsList();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

// ===========================================================
// onPause
// ===========================================================

protected void onPause() {
    super.onPause();
    finish();
}

// ===========================================================
// onResume
// ===========================================================

protected void onResume() {
    super.onResume();
    generateFriendsList();
}

// ===========================================================
// onStop
// ===========================================================

protected void onStop() {
    super.onStop();
}

// ===========================================================
// onDestroy
// ===========================================================

@Override
protected void onDestroy() {
    super.onDestroy();
}

// ===========================================================
// Activity methods
// ===========================================================

private void setButtonNewFriendClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_add_friend);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), FriendNewActivity.class);
            startActivity(i);
        }
    });
}

AndroidManifest 如下所示

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  android:versionCode="1"
  android:versionName="1.0" package="com.package.mypackage">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.package.mypackage.mypackage"
              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=".FriendsListActivity"></activity>
     <activity android:name=".FriendEditActivity"></activity>
     <activity android:name=".FriendNewActivity"></activity>
     <activity android:name=".TakePictureActivity"></activity>
     <activity android:name=".FriendsService"></activity>
     <activity android:name=".MyService"></activity>
</application>
<uses-sdk android:minSdkVersion="9" />

主要活动如下所示:

    package com.package.mypackage;

import java.util.ArrayList;

import com.package.domain.Domain;
import com.package.service.MyService;
import com.package.viewadapter.myImageAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Toast;

public class myActivity extends Activity {

// ===========================================================
// Fields
// ===========================================================

private MyImageAdapter myImageAdapter;
private ArrayList<Domain> domain = new ArrayList<Domain>();

// ===========================================================
// onCreate
// ===========================================================

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    /* Set Buttons to listen for any click event. */
    setButtonFriendsClickListener();
    setButtonCameraClickListener();
    setButtonPreferencesClickListener();
}

// ===========================================================
// onStart
// ===========================================================

@Override
public void onStart() {
    super.onStart();

    /* Find the gallery defined in the main.xml */        
    Gallery g = (Gallery) findViewById(R.id.gallery);
    /* Show a Toast message when image is clicked */
    g.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            MyImageAdapter image_ID = new MyImageAdapter(myActivity.this, position, null);
            if (image_ID.getItemId(position) == 0) {
                Toast test_toast = Toast.makeText(myActivity.this, "This is the New Image click", Toast.LENGTH_SHORT);
                test_toast.show();
            }
            else {
                Toast test_toast = Toast.makeText(myActivity.this, "The clicked image has image number " + image_ID.getItemId(position) + " in the imageadapter.", Toast.LENGTH_SHORT);
                test_toast.show();
            }
        }
    });

    g.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
            MyImageAdapter image_ID = new MyImageAdapter(myActivity.this, position, null);
            Toast test_toast = Toast.makeText(myActivity.this, "The long clicked image has image number " + image_ID.getItemId(position) + " in the imageadapter.", Toast.LENGTH_SHORT);
            test_toast.show();
            return true;
        }
    });
}

// ===========================================================
// onPause
// ===========================================================

protected void onPause() {
    super.onPause();
}

// ===========================================================
// onResume
// ===========================================================

protected void onResume() {
    super.onResume();
    generateMyGallery();
}

// ===========================================================
// onStop
// ===========================================================

protected void onStop() {
    super.onStop();
}

// ===========================================================
// onDestroy
// Is also called when user changes from horizontal
// to vertical orientation and back
// ===========================================================

@Override
protected void onDestroy() {
    super.onDestroy();
}

// ===========================================================
// Save and Restore UI states
// ===========================================================

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

// ===========================================================
// Main Activity methods
// ===========================================================


public void generateMyGallery() {
    MyService cs = new MyService(this);
    domain = cs.getDomainList();

    // Add the new_image drawable to the ArrayList
    Domain d = new Domain();
    d.photoLocation = "drawable";
    d.photoName = "new_image";
    d.extra1 = "no_text";
    d.extra2 = "no_text";
    domain.add(0, d);

    myImageAdapter = new MyImageAdapter(this, R.layout.text_overlay_image_view, domain);

    /* Find the gallery defined in the main.xml */        
    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setSpacing(10);

    /* Apply a new (custom) ImageAdapter to it. */
    g.setAdapter(myImageAdapter);
    g.setSelection(1);
}

private void setButtonFriendsClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_friends_list);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.friends_list);

            Intent myIntent = new Intent(v.getContext(), FriendsListActivity.class);
            startActivity(myIntent);
        }
    });
}

private void setButtonCameraClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_take_picture);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.take_picture);

            Intent myIntent = new Intent(v.getContext(), TakePictureActivity.class);
            startActivity(myIntent);
        }
    });
}

private void setButtonPreferencesClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_preferences);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent myIntent = new Intent(v.getContext(), MyPreferencesActivity.class);
            startActivity(myIntent);
        }
    });
    }
};

任何人都知道应用程序崩溃的原因。非常感谢任何帮助。

我确实发现,当我启动 FriendListActivity 时 finish() MAIN 活动并在关闭 FriendListActivity 时重新启动 MAIN 活动时,不会发生崩溃。然而,这基本上会重新启动应用程序,而这并不是本意。


谢谢大家,我的问题已经解决了。这就是我所做的。
。当将所有与画廊相关的操作移至 onCreate 时,崩溃不再发生。但返回主活动后,该活动并未显示。
。然后,我将所有 onCreate(除了 super)、generateGallery 和 onStart() 移至 onResume。现在效果很好!

My App consists of 2 Activities at the moment.
. the MAIN activity with a Gallery View
. a FriendsListActivity with a ListView

When the user moves away from the FriendsListActivity with the back button, and returns to the MAIN activity the following error keeps popping up in debug mode.

DalvikVM[localhost:8676] Thread [<1>
main] (Suspended (exception
RuntimeException))
ActivityThread.performResumeActivity(IBinder,
boolean) line: 2095
ActivityThread.handleResumeActivity(IBinder,
boolean, boolean) line: 2110
BinderProxy(ActivityThread$H).handleMessage(Message)
line: 954
ActivityThread$H(Handler).dispatchMessage(Message)
line: 99 Looper.loop() line: 123
ActivityThread.main(String[]) line:
3647 Method.invokeNative(Object,
Object[], Class, Class[], Class, int,
boolean) line: not available [native
method] Method.invoke(Object,
Object...) line: 507
ZygoteInit$MethodAndArgsCaller.run()
line: 839 ZygoteInit.main(String[])
line: 597
NativeStart.main(String[]) line: not
available [native method] Thread
[<8> Binder Thread #2] (Running)
Thread [<7> Binder Thread #1]
(Running)

With the LogCat

03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): FATAL
EXCEPTION: main 03-13 22:01:10.972:
ERROR/AndroidRuntime(1038):
java.lang.RuntimeException: Unable to
resume activity
{com.package.MAIN/com.package.MAIN.MAIN}:
java.lang.NullPointerException 03-13
22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2095)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2110)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.app.ActivityThread$H.handleMessage(ActivityThread.java:954)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.os.Handler.dispatchMessage(Handler.java:99)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.os.Looper.loop(Looper.java:123)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.app.ActivityThread.main(ActivityThread.java:3647)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
java.lang.reflect.Method.invokeNative(Native
Method) 03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
java.lang.reflect.Method.invoke(Method.java:507)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
dalvik.system.NativeStart.main(Native
Method) 03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): Caused by:
java.lang.NullPointerException 03-13
22:01:10.972:
ERROR/AndroidRuntime(1038): at
com.package.MAIN.MAIN.onResume(MAIN.java:91)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1149)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.app.Activity.performResume(Activity.java:3833)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): at
android.app.ActivityThread.performResumeActivity(ActivityThread.java:2085)
03-13 22:01:10.972:
ERROR/AndroidRuntime(1038): ... 10
more

Further info in the Variables panel states:
this: ActivityThread
e: NullPointerException
cause: NullPointerException
detailMessage: null
stackTrace: null
r: ActivityThread$ActivityClientRecord
activity: MAIN

detailMessage after one Eclipse Resume: Unable to resume activity (MAIN)

The code from the FriendsListActivity looks like this

public class FriendsListActivity extends ListActivity {

// ===========================================================
// Fields
// ===========================================================

private ArrayList<Friend> friends = new ArrayList<Friend>();
private FriendsArrayAdapter friendsArrayAdapter;
private ListView listView;

// ===========================================================
// onCreate
// ===========================================================

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.friends_list);

    registerForContextMenu(getListView());
    setButtonNewFriendClickListener();
}

public void generateFriendsList() {
    FriendsService fs = new FriendsService(this);
    friends = fs.getFriendsList();

    listView = (ListView) findViewById(android.R.id.list);
    friendsArrayAdapter = new FriendsArrayAdapter(
       this, R.layout.friend_list_item, friends);
    listView.setAdapter(friendsArrayAdapter);
}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater = getMenuInflater();
    inflater.inflate(R.menu.friends_context_menu, menu);
}

@Override
public boolean onContextItemSelected(MenuItem item) {
    AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
    FriendsService fs = new FriendsService(this);
    Friend f = new Friend();
    f = friends.get(info.position);
    switch (item.getItemId()) {
    case R.id.edit:
        Intent i = new Intent(this, FriendEditActivity.class);
        i.putExtra("userid", f.userId);
        startActivity(i);
        return true;
    case R.id.delete:
        fs.deleteFriend(f.userId);
        generateFriendsList();
        return true;
    default:
        return super.onContextItemSelected(item);
    }
}

// ===========================================================
// onPause
// ===========================================================

protected void onPause() {
    super.onPause();
    finish();
}

// ===========================================================
// onResume
// ===========================================================

protected void onResume() {
    super.onResume();
    generateFriendsList();
}

// ===========================================================
// onStop
// ===========================================================

protected void onStop() {
    super.onStop();
}

// ===========================================================
// onDestroy
// ===========================================================

@Override
protected void onDestroy() {
    super.onDestroy();
}

// ===========================================================
// Activity methods
// ===========================================================

private void setButtonNewFriendClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_add_friend);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent i = new Intent(v.getContext(), FriendNewActivity.class);
            startActivity(i);
        }
    });
}

The AndroidManifest looks like this

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
  android:versionCode="1"
  android:versionName="1.0" package="com.package.mypackage">
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<application android:icon="@drawable/icon" android:label="@string/app_name">
    <activity android:name="com.package.mypackage.mypackage"
              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=".FriendsListActivity"></activity>
     <activity android:name=".FriendEditActivity"></activity>
     <activity android:name=".FriendNewActivity"></activity>
     <activity android:name=".TakePictureActivity"></activity>
     <activity android:name=".FriendsService"></activity>
     <activity android:name=".MyService"></activity>
</application>
<uses-sdk android:minSdkVersion="9" />

The MAIN activity looks like this:

    package com.package.mypackage;

import java.util.ArrayList;

import com.package.domain.Domain;
import com.package.service.MyService;
import com.package.viewadapter.myImageAdapter;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.Toast;

public class myActivity extends Activity {

// ===========================================================
// Fields
// ===========================================================

private MyImageAdapter myImageAdapter;
private ArrayList<Domain> domain = new ArrayList<Domain>();

// ===========================================================
// onCreate
// ===========================================================

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.main);

    /* Set Buttons to listen for any click event. */
    setButtonFriendsClickListener();
    setButtonCameraClickListener();
    setButtonPreferencesClickListener();
}

// ===========================================================
// onStart
// ===========================================================

@Override
public void onStart() {
    super.onStart();

    /* Find the gallery defined in the main.xml */        
    Gallery g = (Gallery) findViewById(R.id.gallery);
    /* Show a Toast message when image is clicked */
    g.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
            MyImageAdapter image_ID = new MyImageAdapter(myActivity.this, position, null);
            if (image_ID.getItemId(position) == 0) {
                Toast test_toast = Toast.makeText(myActivity.this, "This is the New Image click", Toast.LENGTH_SHORT);
                test_toast.show();
            }
            else {
                Toast test_toast = Toast.makeText(myActivity.this, "The clicked image has image number " + image_ID.getItemId(position) + " in the imageadapter.", Toast.LENGTH_SHORT);
                test_toast.show();
            }
        }
    });

    g.setOnItemLongClickListener(new OnItemLongClickListener() {

        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
            MyImageAdapter image_ID = new MyImageAdapter(myActivity.this, position, null);
            Toast test_toast = Toast.makeText(myActivity.this, "The long clicked image has image number " + image_ID.getItemId(position) + " in the imageadapter.", Toast.LENGTH_SHORT);
            test_toast.show();
            return true;
        }
    });
}

// ===========================================================
// onPause
// ===========================================================

protected void onPause() {
    super.onPause();
}

// ===========================================================
// onResume
// ===========================================================

protected void onResume() {
    super.onResume();
    generateMyGallery();
}

// ===========================================================
// onStop
// ===========================================================

protected void onStop() {
    super.onStop();
}

// ===========================================================
// onDestroy
// Is also called when user changes from horizontal
// to vertical orientation and back
// ===========================================================

@Override
protected void onDestroy() {
    super.onDestroy();
}

// ===========================================================
// Save and Restore UI states
// ===========================================================

@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
    super.onRestoreInstanceState(savedInstanceState);
}

protected void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

// ===========================================================
// Main Activity methods
// ===========================================================


public void generateMyGallery() {
    MyService cs = new MyService(this);
    domain = cs.getDomainList();

    // Add the new_image drawable to the ArrayList
    Domain d = new Domain();
    d.photoLocation = "drawable";
    d.photoName = "new_image";
    d.extra1 = "no_text";
    d.extra2 = "no_text";
    domain.add(0, d);

    myImageAdapter = new MyImageAdapter(this, R.layout.text_overlay_image_view, domain);

    /* Find the gallery defined in the main.xml */        
    Gallery g = (Gallery) findViewById(R.id.gallery);
    g.setSpacing(10);

    /* Apply a new (custom) ImageAdapter to it. */
    g.setAdapter(myImageAdapter);
    g.setSelection(1);
}

private void setButtonFriendsClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_friends_list);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.friends_list);

            Intent myIntent = new Intent(v.getContext(), FriendsListActivity.class);
            startActivity(myIntent);
        }
    });
}

private void setButtonCameraClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_take_picture);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            setContentView(R.layout.take_picture);

            Intent myIntent = new Intent(v.getContext(), TakePictureActivity.class);
            startActivity(myIntent);
        }
    });
}

private void setButtonPreferencesClickListener() {
    Button clickButton = (Button)findViewById(R.id.button_preferences);
    clickButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent myIntent = new Intent(v.getContext(), MyPreferencesActivity.class);
            startActivity(myIntent);
        }
    });
    }
};

Anyone has an idea why the App crashes. Any help is much appreciated.

I did find out that, when I finish() the MAIN activity when I start the FriendListActivity and restart the MAIN activity when closing the FriendListActivity, that the crash does not occur. However, this basically restarts the App and that is not the intention.


Thanks all, I got the problem solved. This is what I did.
. When moving all gallery related actions to the onCreate, the creash didn't happen anymore. But then after returning to the MAIN activity, the activity wasn't shown.
. I then moved all the onCreate (except the super), the generateGallery, and the onStart() to the onResume. Now it works fine!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

离笑几人歌 2024-10-28 21:18:34

为什么要调用onPause() finish()?

你试过把它注释掉吗?

这个朋友服务是什么?

可能它会导致您的异常。

UPD:

我想说,移动generateMyGallery();从 onResume 到 onCreate。因为每次用户从另一个活动返回时 onResume 都会被调用。所以generateMyGallery()也是如此

why do you call in onPause() finish()?

have you tried to comment it out?

And what is this friendservice?

Possibly it causes your exception.

UPD:

I would say, move generateMyGallery(); from onResume into onCreate. Because everytime the user comes back from another activity onResume will be called. And so generateMyGallery() as well

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文