嗨,我这是前一个问题的重新考虑,因为我之前并不清楚。我有一个json feed包含在其中我找到了数组的长度。因为它应该产生一个。但我想要做的是,如果要添加第二个项目,则需要使用其他布局。
所以我目前有一个字符串,其中包含字符串值YES,然后将其发送到一个意图发送到另一个活动中,这个放在if语句中,如果x = Yes则使用layout1如果x = no use layout2。
我遇到的问题是当我在if语句中放入一个字符串并记录它时它可以工作但是当我把setContentView放入并且它强制应用程序关闭原因时?
这是我的代码
第一项活动
HttpResponse response = client.execute(post);
BufferedReader reader = new BufferedReader(new InputStreamReader(response.getEntity().getContent(), "UTF-8"));
String json = reader.readLine();
JSONObject obj = new JSONObject(json);
JSONObject objData = obj.getJSONObject("data");
JSONArray jArray = objData.getJSONArray("structure");
leagueCount = jArray.length();
if (leagueCount == 1){
teamFeedStructure = "YES" ;
}
if (leagueCount == 2){
teamFeedStructure = "NO" ;
}
现在已发送意图
Intent mainIntent = new Intent(firstActivity.this,secondActivity.class);
mainIntent.putExtra("leagueCount", teamFeedStructure);
firstActivity.this.startActivity(mainIntent);
firstActivity.this.finish();
现在在第二个活动中
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.introact);
String leagueCount = getIntent().getExtras().getString("leagueCount");
//String leaguecount = String.valueOf(leagueCount);
Log.v("lc", leagueCount);
if (leagueCount == "YES"){
String one = "one";
setContentView(R.layout.introact);
Log.v("lc", one);
}
//if (leaguecount == "2"){
//setContentView(R.layout.introact);
//}
错误
04-23 21:53:34.455: W/dalvikvm(954): threadid=1: thread exiting with uncaught exception (group=0x40015560)
04-23 21:53:34.465: E/AndroidRuntime(954): FATAL EXCEPTION: main
04-23 21:53:34.465: E/AndroidRuntime(954): java.lang.RuntimeException: Unable to start activity ComponentInfo{co.uk.fantasticmedia.TheEvoStikLeague/co.uk.fantasticmedia.TheEvoStikLeague.IntroActivity}: java.lang.NullPointerException
04-23 21:53:34.465: E/AndroidRuntime(954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.os.Looper.loop(Looper.java:130)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 21:53:34.465: E/AndroidRuntime(954): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 21:53:34.465: E/AndroidRuntime(954): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 21:53:34.465: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 21:53:34.465: E/AndroidRuntime(954): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 21:53:34.465: E/AndroidRuntime(954): at dalvik.system.NativeStart.main(Native Method)
04-23 21:53:34.465: E/AndroidRuntime(954): Caused by: java.lang.NullPointerException
04-23 21:53:34.465: E/AndroidRuntime(954): at co.uk.fantasticmedia.TheEvoStikLeague.IntroActivity.onCreate(IntroActivity.java:45)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 21:53:34.465: E/AndroidRuntime(954): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-23 21:53:34.465: E/AndroidRuntime(954): ... 11 more
答案 0 :(得分:2)
你不能使用==比较字符串==将比较你需要做的内存位置:
if("1".equals(leaguecount))
Top Ten Errors Java Programmers Make(见第7条)
将硬编码字符串"1"
与equals方法(而不是something.equals("1")
进行比较,也可以节省您的空检查费用
我认为您的问题是您发送null
并且实际上没有初始化您的字符串。在你解析你的JSON后我会这样做:
这是重写:
teamFeedStructure = "NO";
if (leagueCount >= 2){
teamFeedStructure = "YES" ;
}
然后你的其他活动
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String leagueCount = getIntent().getStringExtra("leagueCount");
Log.d("lc", "Received league count of:"+ leagueCount);
if ("YES".equals(leagueCount){
setContentView(R.layout.introact);
Log.d("lc", "Using league count of 2 or more layout");
}else{
setContentView(R.layout.introact_something_else);
Log.d("lc", "Using default layout");
}
}