Android if/else上缺少}和/或)
我错过了一个}和/或)在代码末尾,我尝试了很多次,但我没有得到它,有人知道吗Android if/else上缺少}和/或),android,eclipse,if-statement,Android,Eclipse,If Statement,我错过了一个}和/或)在代码末尾,我尝试了很多次,但我没有得到它,有人知道吗 doclink.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { if(!uri.contains("https://www.facebook.com/")) { String
doclink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
else{
String natgeo = "natgeo";
String uri = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
};
编辑 我把这些都加上了,我希望它对我有好处
package com.example.a;
import android.app.Activity;
import android.content.ActivityNotFoundException;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
Button dadclink;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListenerOnButton();
}
public void addListenerOnButton() {
dadclink = (Button) findViewById(R.id.dadclink);
dadclink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
else{
String natgeo= "natgeo";
String uri = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
)};
}
}
编辑
编辑全文 active_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:gravity="center"
android:orientation="vertical"
android:textStyle="italic" >
<Button
android:id="@+id/dadclink"
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="center"
android:text="blah blah"
android:textSize="38sp"
android:textStyle="italic" />
</LinearLayout>
</RelativeLayout>
Manifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.a"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="8" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@android:style/Theme.NoTitleBar.Fullscreen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.LAUNCHER" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
</activity>
</application>
</manifest>
更新:您可以实现OnClickListener,如下代码所示:
public class MainActivity extends Activity {
Button dadclink;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dadclink = (Button) findViewById(R.id.dadclink);
dadclink.setOnClickListener(myClickListener);
}
private View.OnClickListener myClickListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.dadclink:
if(!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(intent);
}
else{
String natgeo= "natgeo";
String uri = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
break;
}
}
};
}
更新2:private View.OnClickListener myClickListener = new View.OnClickListener() {
final String uri = null;
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.dadclink:
String natgeo= "natgeo";
String uri = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
break;
}
}
};
还记得将这一行添加到您的manifest.xml
文件中:<activity
android:name=".MainActivity"
....
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
...
</activity>
您正在创建一个匿名内部类,即最后一行-
startActivity(i);
}
};
应该是
startActivity(i);
}
}); // <-- close the open paren.
然后
你的问题是什么?你没有关上你的其他东西,它不起作用!我更糊涂了:)我会编辑我的帖子并添加整个代码,希望你不要介意!更新:)我迷路了!有很多东西需要更新,比如eclipse,我还没有更新呢!这是有原因的!我会检查你添加的内容,给我时间。不幸的是,它不起作用,我添加了完整的程序。@Sohar你看到任何logcat错误吗?为什么要检查uri.contains()
?是否与我的update2代码兼容?删除if/else
条件并使用else
块代码进行测试。对不起,鸣人,你能粘贴整个代码吗?
private View.OnClickListener myClickListener = new View.OnClickListener() {
final String uri = null;
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.dadclink:
String natgeo= "natgeo";
String uri = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
break;
}
}
};
<activity
android:name=".MainActivity"
....
<intent-filter>
<action android:name="android.intent.action.VIEW" />
<category android:name="android.intent.category.DEFAULT" />
<category android:name="android.intent.category.BROWSABLE" />
</intent-filter>
...
</activity>
startActivity(i);
}
};
startActivity(i);
}
}); // <-- close the open paren.
class MyListener extends OnClickListener() {
@Override
public void onClick(View arg0) {
String natgeo = "natgeo";
String uri = (!uri.contains("https://www.facebook.com/")) ?
"fb://Page/" : "https://www.facebook.com/";
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(uri+natgeo));
startActivity(intent);
}
}
doclink.setOnClickListener(new MyListener());
doclink.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!uri.contains("https://www.facebook.com/")) {
String natgeo = "natgeo";
String uri = "fb://Page/" + natgeo;
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(uri));
startActivity(intent);
} else {
String natgeo = "natgeo";
String uri = "https://www.facebook.com/" + natgeo;
Intent i = new Intent(Intent.ACTION_VIEW, Uri.parse(uri));
startActivity(i);
}
}
});