2011-03-11 146 views
0

我的應用程序包含2個活動。 。 MAIN活動與畫廊視圖 。具有列表視圖的FriendsListActivityAndroid應用程序在後退按鈕上崩潰(performResumeActivity)

當用戶使用後退按鈕移開FriendsListActivity並返回MAIN活動時,以下錯誤在調試模式下不斷彈出。

DalvikVM [本地主機:8676]線程[< 1> 主](暫停(例外 的RuntimeException))
ActivityThread.performResumeActivity(的IBinder, 布爾值)線:2095
ActivityThread.handleResumeActivity(的IBinder, 布爾值,布爾值)線:2110
BinderProxy(ActivityThread $ H).handleMessage(消息) 線:954 ActivityThread $ H(處理程序).dispatchMessage(消息) 行:99 Looper.loop()線:123 ActivityThread.main(String [])line: 3647 Method.invokeNative(Object, Object [],Class,Class [],Class,int, boolean)line:not available [native method] Method.invoke對象, 對象...)線:507
ZygoteInit $ MethodAndArgsCaller.run() 線:839 ZygoteInit.main(字符串[]) 線:597 NativeStart.main(字符串[])行:不 可用[原生方法]線程 [< 8> Binder Thread#2](Running)
Thread [< 7> Binder Thread#1] (Running)

隨着logcat的

03-13 22:01:10.972: ERROR/AndroidRuntime(1038):致命 例外:主03-13 22:01:10.972: ERROR/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.Activit yThread.performResumeActivity(ActivityThread.java:2095) 03-13 22:01:10.972: 錯誤/ AndroidRuntime(1038):at 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):at 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: ERROR/AndroidRuntime(1038):在 java.lang.reflect.Method.invokeNative(母語 方法)03 -13 22:01:10。972: ERROR/AndroidRuntime(1038):在 java.lang.reflect.Method.invoke(Method.java:507) 03-13 22:01:10.972: ERROR/AndroidRuntime(1038):在 融爲一體。 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):在 dalvik.system.NativeStart.main(母語 方法)03-13 22:01:10.972: ERROR/AndroidRuntime(1038): 顯示java.lang.NullPointerException 03-13:致3210 22:01:10.972: ERROR/AndroidRuntime(1038):在 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):在 android.app.ActivityThread.performResumeActivity(ActivityThread.java:2085) 03-13 22:01:10.972: ERROR/AndroidRuntime( 1038):... 10 更多

在變量面板國進一步信息:
這樣的:ActivityThread
E:NullPointerException異常
原因:NullPointerException異常
detailMessage:空
堆棧跟蹤:空
R:ActivityThread $ ActivityClientRecord
活動:MAIN

detailMessage一個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並關閉FriendListActivity重新啓動MAIN活動時,崩潰不會發生。但是,這基本上重新啓動應用程序,這不是意圖。


謝謝大家,我解決了問題。這就是我所做的。
。將所有畫廊相關的動作移動到onCreate時,鋸齒不再發生。但是,在返回MAIN活動後,沒有顯示活動。
。然後,我將所有onCreate(除了super),generateGallery和onStart()移至onResume。現在它工作正常!

+0

我認爲問題是在一個畫廊視圖的MAIN活動的onCreate()方法。 – sat 2011-03-11 12:38:00

+0

我現在還列出了MAIN活動中的onCreate()方法。我相信應用程序在重新創建MAIN活動時會崩潰。感謝所有幫助到目前爲止。 – Rutger 2011-03-13 15:33:34

回答

0

你爲什麼要打onPause()finish()?

你有沒有試過評論它?

這是什麼朋友服務?

可能會導致您的例外。

UPD:

我會說,移動generateMyGallery();從onResume到onCreate。因爲每次用戶從其他活動回來onResume都會被調用。所以generateMyGallery()以及

+0

我只想在返回到舊的MAIN活動之前很好地關閉活動。當我刪除完成()它並沒有解決問題。 Friendservice是一個從數據庫中獲取數據的服務。在使用後關閉FriendService與fs.finish()也沒有幫助。 – Rutger 2011-03-13 15:31:09

+0

我認爲,在onPause中使用完成並不是個好主意。並且您是否嘗試過評論所有使用您的服務?只是可以肯定,這個服務不會導致崩潰 – Tima 2011-03-14 08:21:22

+0

我會說,移動generateMyGallery();從onResume到onCreate。因爲每次用戶從其他活動回來onResume都會被調用。所以generateMyGallery()也是如此 – Tima 2011-03-14 11:03:30

相關問題