[英]Android Intent extras
我完全迷失了這一點。 我是Android Dev的新手,但確實對此感到迷惑。
我有一個應用程序,該應用程序加載了具有所有客戶名稱和ID的列表視圖,我能夠獲取客戶的ID。 我試圖通過意圖
public final static String EXTRA_MESSAGE = "VIPID";
...
Intent intent = new Intent(ManageVIPCustomers.this, Contact.class);
intent.putExtra(EXTRA_MESSAGE, itemString);
startActivity(intent);
ManageVIPCustomers是發送活動,Contact是接收活動。 聯系人接收命令為:
Intent intent = getIntent();
String VIPString = intent.getStringExtra(ManageVIPCustomers.EXTRA_MESSAGE);
一切都可以正常編譯,但是單擊它表示應用程序已停止。
我的(部分)清單如下:
<activity
android:name=".ManageVIPCustomers"
android:label="@string/title_activity_manage_vipcustomers" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Contact"
android:label="@string/title_activity_contact" >
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
任何幫助都會很棒
編輯:下面的LogCat:
11-03 01:33:03.719: E/AndroidRuntime(1904): FATAL EXCEPTION: main
11-03 01:33:03.719: E/AndroidRuntime(1904): Process: edu.gatech.seclass.project2, PID: 1904
11-03 01:33:03.719: E/AndroidRuntime(1904): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{edu.gatech.seclass.project2/edu.gatech.seclass.project2.Contact}: java.lang.NullPointerException
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2110)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.ActivityThread.access$800(ActivityThread.java:135)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.os.Handler.dispatchMessage(Handler.java:102)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.os.Looper.loop(Looper.java:136)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.ActivityThread.main(ActivityThread.java:5001)
11-03 01:33:03.719: E/AndroidRuntime(1904): at java.lang.reflect.Method.invokeNative(Native Method)
11-03 01:33:03.719: E/AndroidRuntime(1904): at java.lang.reflect.Method.invoke(Method.java:515)
11-03 01:33:03.719: E/AndroidRuntime(1904): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-03 01:33:03.719: E/AndroidRuntime(1904): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-03 01:33:03.719: E/AndroidRuntime(1904): at dalvik.system.NativeStart.main(Native Method)
11-03 01:33:03.719: E/AndroidRuntime(1904): Caused by: java.lang.NullPointerException
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.Activity.findViewById(Activity.java:1884)
11-03 01:33:03.719: E/AndroidRuntime(1904): at edu.gatech.seclass.project2.Contact.<init>(Contact.java:29)
11-03 01:33:03.719: E/AndroidRuntime(1904): at java.lang.Class.newInstanceImpl(Native Method)
11-03 01:33:03.719: E/AndroidRuntime(1904): at java.lang.Class.newInstance(Class.java:1208)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
11-03 01:33:03.719: E/AndroidRuntime(1904): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2101)
11-03 01:33:03.719: E/AndroidRuntime(1904): ... 11 more
這與意圖或其他無關。 您的第二個Activity在設置Activity的內容視圖之前,在其構造函數中調用findViewById
,這可能是因為您在任何方法之外都聲明了這樣的內容:
View someView = findViewById(R.id.some_id);
創建對象時會運行構造函數或方法之外的賦值。 這不是調用findViewById
的正確時間。 您只需要聲明視圖並在設置內容視圖之后調用findViewById
:
View someView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.some_layout);
someView = findViewById(R.id.some_id);
...
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.