0

私のアプリは現在2つのアクティビティで構成されています。。ギャラリービューを使用したメインアクティビティ。ListViewを使用したFriendsListActivity

ユーザーが戻るボタンでFriendsListActivityから離れ、MAINアクティビティに戻ると、次のエラーがデバッグモードでポップアップし続けます。

DalvikVM [localhost:8676]スレッド[<1> main](Suspended(exception RuntimeException))
ActivityThread.performResumeActivity(IBinder、boolean)line:2095
ActivityThread.handleResumeActivity(IBinder、boolean、boolean)line:2110
BinderProxy(ActivityThread $ H) .handleMessage(Message)行:954 ActivityThread $ H(Handler).dispatchMessage(Message)行:99 Looper.loop()行:123 ActivityThread.main(String [])行:3647 Method.invokeNative(Object、Object [] 、Class、Class []、Class、int、boolean)行:使用不可[ネイティブメソッド] Method.invoke(Object、Object ...)行:507
ZygoteInit $ MethodAndArgsCaller.run()行:839 ZygoteInit.main(String [])行:597 NativeStart.main(String [])行:使用不可[ネイティブメソッド]スレッド[<8>バインダースレッド#2](実行中)
スレッド[<7>バインダースレッド#1](実行中)

LogCatを使用

03-13 22:01:10.972:エラー/ AndroidRuntime(1038):致命的な例外:メイン03-13 22:01:10.972:エラー/ AndroidRuntime(1038):java.lang.RuntimeException:アクティビティを再開できません{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):android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2110)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):android.appで。 ActivityThread $ H.handleMessage(ActivityThread.java:954)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):android.os.Handler.dispatchMessage(Handler.java:99)03-13 22:01: 10.972:ERROR / AndroidRuntime(1038):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):com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):dalvik.system.NativeStart.main(Native)メソッド)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):原因:java.lang.NullPointerException 03-13 22:01:10.972:ERROR / AndroidRuntime(1038):com.package.MAIN.MAIN.onResume(MAIN.java:91)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):android.app.Instrumentation.callActivityOnResume(Instrumentation .java:1149)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):android.app.Activity.performResume(Activity.java:3833)03-13 22:01:10.972:ERROR / AndroidRuntime(1038 ):android.app.ActivityThread.performResumeActivity(ActivityThread.java:2085)03-13 22:01:10.972:ERROR / AndroidRuntime(1038):... 10 morePerformResume(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以上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以上

変数パネルの詳細情報には、次のように記載されています。this:
ActivityThread
e:NullPointerException
原因:NullPointerException
detailMessage:null
stackTrace:null
r:ActivityThread $ ActivityClientRecord
activity:MAIN

1回のEclipse再開後のdetailMessage:アクティビティを再開できません(MAIN)

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" />

MAINアクティビティは次のようになります。

    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を開始するときにMAINアクティビティをfinish()し、FriendListActivityを閉じるときにMAINアクティビティを再開すると、クラッシュが発生しないことがわかりました。ただし、これは基本的にアプリを再起動するものであり、それは意図されたものではありません。


おかげさまで、問題は解決しました。これが私がしたことです。
。ギャラリーに関連するすべてのアクションをonCreateに移動すると、クリーシュは発生しなくなりました。しかし、MAINアクティビティに戻った後、アクティビティは表示されませんでした。
。次に、すべてのonCreate(superを除く)、generateGallery、およびonStart()をonResumeに移動しました。今では正常に動作します!

4

1 に答える 1

0

なぜ onPause() finish() を呼び出すのですか?

コメントアウトしようとしましたか?

そして、このフレンドサービスとは何ですか?

おそらくそれはあなたの例外を引き起こします。

更新:

generateMyGallery(); を移動します。onResume から onCreate へ。ユーザーが別のアクティビティから戻るたびに onResume が呼び出されるためです。そして、generateMyGallery() も

于 2011-03-11T12:53:51.430 に答える