【问题标题】:Android(Eclipse) Startactivity gives no responseAndroid(Eclipse)Startactivity没有响应
【发布时间】:2015-03-19 08:54:00
【问题描述】:

我是 Java 和 android 编程的新手。我在 toast 消息之后添加了一个代码来在我的应用程序的界面之间切换,但是当我启动程序时,在 toast 消息之后它只是停留在同一个界面中而不会出现任何错误。

try
    {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost = new HttpPost("http://xxx/xxx.php");
        httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
        ResponseHandler<String> responseHandler = new BasicResponseHandler();
        String response = httpclient.execute(httppost, responseHandler); 
        String reverseString = response;
        if (reverseString =="success"){
            Toast.makeText(this, reverseString, Toast.LENGTH_LONG).show();
            //setContentView(R.layout.activity_main);
            startActivity(new Intent(registergame.this, (MainActivity.class)));
            finish();
        }else{
            Toast.makeText(this, reverseString, Toast.LENGTH_LONG).show();
        }

    } catch (ClientProtocolException e) {
    Toast.makeText(this, "CPE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    } catch (IOException e) {
    Toast.makeText(this, "IOE response " + e.toString(), Toast.LENGTH_LONG).show();
    // TODO Auto-generated catch block
    }

还有我的清单文件;

<activity
        android:name=".MainActivity"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".deneme"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.baglanti.DENEME" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>
    <activity
        android:name=".registergame"
        android:label="@string/app_name" >
        <intent-filter>
            <action android:name="com.example.baglanti.REGISTER" />

            <category android:name="android.intent.category.DEFAULT" />
        </intent-filter>
    </activity>

也试过了

startActivity(new Intent("android.intent.action.MAIN"));

但同样的事情发生了,它显示了 toast 消息并保持不变,有什么想法吗? 提前谢谢你。

【问题讨论】:

  • 在 startActivity(new Intent(registergame.this, (MainActivity.class)));不要使用 registergame.this,而是尝试使用 getApplicationContext() 。
  • 你看到这个吐司了吗 Toast.makeText(this, reverseString, Toast.LENGTH_LONG).show();
  • ashwin,你写了同样的东西,我发了:(
  • Amsheer 是的 toast 消息被看到,但之后没有任何反应:(

标签: java android eclipse android-layout android-intent


【解决方案1】:

一旦我也面临同样的问题。当我需要读取响应并且如果它“成功”然后允许下一个活动。就我而言,我对“如果”条件有疑问。那不是这个if (reverseString =="success")

试试吧

if (reverseString.contains("success"))

我想在评论中这样说。但是这里说的太长了。也许会有所帮助。我不确定。

注意:@回复评论 你必须看到吐司。因为您在 if 和 else 中添加了相同的内容。从其他地方删除吐司,看看你看不到吐司。那是因为您的 if 条件失败。

【讨论】:

  • 哇,它成功了,非常喜欢 Amsheer,我不认为“如果”可能会导致这种错误,因为看到了 toast 消息。
  • 对不起,我的英语很弱。我不明白。你是什​​么意思。我的答案是否有效?
  • 我会的,它不会让我在问题创建后几分钟过去之前接受它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-04-20
  • 1970-01-01
  • 2015-06-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多