Android版的setContentView没有被应用 [英] Android setContentView not being applied

查看:115
本文介绍了Android版的setContentView没有被应用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我这是previous问题的reask只是因为我是不是之前清楚。我有一个JSON提要,由我发现其中的数组的长度。因为它应该是产生一个。但我想要做的是,如果第二个项目是要添加饲料另一个布局将需要使用。

所以我现在有这个放置一个if语句,说如果x =是再使用布局1如果x =没有用里面的布局2包含的是这是在随后的意图发送到花药活动字符串值的字符串。

我遇到的问题是,当我在把一个字符串中的if语句,并记录它它的作品,但是当我把在的setContentView并强制关闭应用,为什么?

这是我的code

在第1个活动

 的Htt presponse响应= client.execute(岗位);
的BufferedReader读者=新的BufferedReader(新的InputStreamReader(response.getEntity()的getContent(),UTF-8)。);
JSON字符串= reader.readLine();JSONObject的OBJ =新的JSONObject(JSON);
JSONObject的objData = obj.getJSONObject(数据);JSONArray jArray = objData.getJSONArray(结构);leagueCount = jArray.length();
如果(leagueCount == 1){
   teamFeedStructure =YES;
}
如果(leagueCount == 2){
   teamFeedStructure =否;
}

现在发送的意图

 意图mainIntent =新意图(firstActivity.this,secondActivity.class);mainIntent.putExtra(leagueCount,teamFeedStructure);firstActivity.this.startActivity(mainIntent);firstActivity.this.finish();

现在在第二个活动

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    // TODO自动生成方法存根
    super.onCreate(savedInstanceState);
    //setContentView(R.layout.introact);    。字符串leagueCount = getIntent()getExtras()的getString(leagueCount);    //字符串leaguecount =将String.valueOf(leagueCount);    Log.v(LC,leagueCount);    如果(leagueCount ==是){        串的一个=一;
        的setContentView(R.layout.introact);        Log.v(LC,一个);
    }    //如果(leaguecount ==2){       //setContentView(R.layout.introact);
    //}

该错误

  04-23 21:53:34.455:W / dalvikvm(954):主题ID = 1:螺纹未捕获的异常退出(组= 0x40015560)
04-23 21:53:34.465:E / AndroidRuntime(954):致命异常:主要
04-23 21:53:34.465:E / AndroidRuntime(954):了java.lang.RuntimeException:无法启动活动ComponentInfo{co.uk.fantasticmedia.TheEvoStikLeague/co.uk.fantasticmedia.TheEvoStikLeague.IntroActivity}:显示java.lang.NullPointerException
04-23 21:53:34.465:E / AndroidRuntime(954):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.app.ActivityThread.access $ 1500(ActivityThread.java:117)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:931)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.os.Handler.dispatchMessage(Handler.java:99)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.os.Looper.loop(Looper.java:130)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.app.ActivityThread.main(ActivityThread.java:3683)
04-23 21:53:34.465:E / AndroidRuntime(954):在java.lang.reflect.Method.invokeNative(本机方法)
04-23 21:53:34.465:E / AndroidRuntime(954):在java.lang.reflect.Method.invoke(Method.java:507)
04-23 21:53:34.465:E / AndroidRuntime(954):在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:839)
04-23 21:53:34.465:E / AndroidRuntime(954):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
04-23 21:53:34.465:E / AndroidRuntime(954):在dalvik.system.NativeStart.main(本机方法)
04-23 21:53:34.465:E / AndroidRuntime(954):致:显示java.lang.NullPointerException
04-23 21:53:34.465:E / AndroidRuntime(954):在co.uk.fantasticmedia.TheEvoStikLeague.IntroActivity.onCreate(IntroActivity.java:45)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 21:53:34.465:E / AndroidRuntime(954):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
04-23 21:53:34.465:E / AndroidRuntime(954):11 ...更多


解决方案

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

 如果(1.equals(leaguecount))

十大错误的Ja​​va程序员做(见7号)

您的硬盘codeD字符串比较1与equals方法(而不是 something.equals(1)也节省了你做一个空检查\\


我觉得你的问题是你发送,而不是实际的初始化字符串。你解析你的JSON后,我会做这样的:

下面是一个重写:

  teamFeedStructure =NO;
如果(leagueCount&GT = 2){
   teamFeedStructure =YES;
}

那么你的其他活动。

  @覆盖
保护无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);    串leagueCount = getIntent()getStringExtra(leagueCount);
    Log.d(LC,收稿联赛计数:+ leagueCount);    如果(是.equals(leagueCount){
        的setContentView(R.layout.introact);
        Log.d(LC,2以上布局使用联盟计数);
    }其他{
        的setContentView(R.layout.introact_something_else);
        Log.d(LC,使用默认的布局);
    }
 }

Hi I'm this is a reask of a previous question just because i wasn't clear before. i have a json feed that consists which i found the length of the array in it. as it should it produces one. but what i'm trying to do is if a second item were to be added that feed another layout would need to be used.

so i currently have a string that contains the String value of YES this is then sent in an intent to anther activity this is placed inside an if statement that says if x = Yes then use layout1 if x = no use layout2.

the problem i'm having is when i put a string in the if statement and log it it works but when i put setContentView in and it forces the app to close why?

here is my code

in the 1st 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" ; 
}                        

now the intent sent

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

mainIntent.putExtra("leagueCount", teamFeedStructure);

firstActivity.this.startActivity(mainIntent);

firstActivity.this.finish();

now in the second 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);
    //}

The Error

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

解决方案

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

这篇关于Android版的setContentView没有被应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆