我完全迷失了这个。我是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);
Everything Compiles很好,但点击它就说应用程序已停止。
我的(部分)清单如下:
<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
答案 0 :(得分:2)
这与Intent或其附加内容无关。在为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);
...
}