gpt4 book ai didi

未应用 Android setContentView

转载 作者:行者123 更新时间:2023-11-29 22:08:08 26 4
gpt4 key购买 nike

您好,我是对上一个问题的重新提问,只是因为我之前不清楚。我有一个 json 提要,我在其中找到了数组的长度。它应该产生一个。但我想要做的是,如果要添加第二个项目,则需要使用另一个布局。

所以我目前有一个字符串,其中包含 YES 的字符串值,然后将其发送到另一个 Activity 的 Intent 中,该 Activity 被放置在一个 if 语句中,如果 x = Yes,则使用 layout1,如果 x = no,则使用 layout2。

我遇到的问题是,当我在 if 语句中放入一个字符串并记录它时,它可以正常工作,但是当我放入 setContentView 时,它会强制关闭应用程序,为什么?

这是我的代码

在第一个 Activity 中

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

Intent mainIntent = new Intent(firstActivity.this,secondActivity.class);

mainIntent.putExtra("leagueCount", teamFeedStructure);

firstActivity.this.startActivity(mainIntent);

firstActivity.this.finish();

现在在第二个 Activity 中

@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

最佳答案

你不能使用==来比较字符串==会比较你需要做的内存位置:

 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" ;
}

然后是你的其他 Activity

@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");
}
}

关于未应用 Android setContentView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10287998/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com