1

こんにちは、これは以前の質問の再質問です。以前は明確ではなかったからです。配列の長さを見つけたjsonフィードがあります。必要に応じて、それを生成します。しかし、私がやろうとしているのは、別のレイアウトを使用する必要がある 2 番目の項目を追加する場合です。

だから私は現在、YESの文字列値を含む文字列を持っています。これは、別のアクティビティにインテントで送信されます。これは、ifステートメント内に配置され、x = Yesの場合はlayout1を使用します。

私が抱えている問題は、ifステートメントに文字列を入れてログに記録すると動作しますが、setContentViewを入れてアプリを強制的に閉じると、なぜですか?

ここに私のコードがあります

1回目の活動で

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();

2 番目のアクティビティで

@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
4

1 に答える 1

3

You cannot use == to compare strings == will compare the memory location you need to do:

 if("1".equals(leaguecount))

Top Ten Errors Java Programmers Make (See number 7 )

Comparing your hard coded string "1" with the equals method (instead of something.equals("1") also saves you having to do a null check\


I think your problem is your sending null and not actually initialising your string. After you parse your JSON I would do it like this:

Here's a rewrite:

teamFeedStructure = "NO";
if (leagueCount >= 2){
   teamFeedStructure = "YES" ; 
}

Then your other 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");
    }
 }
于 2012-04-23T20:52:13.997 に答える