Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/220.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 向打开浏览器URL的活动添加按钮_Java_Android_Xml - Fatal编程技术网

Java 向打开浏览器URL的活动添加按钮

Java 向打开浏览器URL的活动添加按钮,java,android,xml,Java,Android,Xml,我想在我的一个活动(显示新闻文章)中添加一个按钮,这样当用户单击按钮时,文章就会在他们的浏览器中打开。到目前为止,我已经在xml中添加了这个按钮,它出现了。我只是在点击侦听器方面遇到了一些问题。下面是我的代码,“setOnClickListener”出现错误,“非静态方法”setOnClickListener(android.view.view.onClickListener)不能从静态内容引用 我不知道这是什么意思!可能我没有在正确的位置调用该方法,或者该方法本身存在错误 请有人看一下,谢谢

我想在我的一个活动(显示新闻文章)中添加一个按钮,这样当用户单击按钮时,文章就会在他们的浏览器中打开。到目前为止,我已经在xml中添加了这个按钮,它出现了。我只是在点击侦听器方面遇到了一些问题。下面是我的代码,“setOnClickListener”出现错误,“非静态方法”setOnClickListener(android.view.view.onClickListener)不能从静态内容引用

我不知道这是什么意思!可能我没有在正确的位置调用该方法,或者该方法本身存在错误

请有人看一下,谢谢

import android.content.Intent;
import android.media.Image;
import android.net.Uri;
import android.support.v4.app.ShareCompat;
import android.support.v4.view.MenuItemCompat;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.ShareActionProvider;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.view.View.OnClickListener;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.JsonArrayRequest;
import com.android.volley.toolbox.JsonObjectRequest;
import com.android.volley.toolbox.NetworkImageView;

import org.json.JSONException;
import org.json.JSONObject;
public class NewsItemActivity extends AppCompatActivity {

    //Declare java object for the UI elements
    private TextView itemTitle;
    private TextView itemDate;
    private TextView itemContent;
    private NetworkImageView itemImage;

    private ShareActionProvider mShareActionProvider;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_news_item);

        itemTitle = (TextView) findViewById(R.id.itemTitle);
        itemDate = (TextView) findViewById(R.id.itemDate);
        itemContent = (TextView) findViewById(R.id.itemContent);
        itemImage = (NetworkImageView) findViewById(R.id.itemImage);

        EDANewsApp app = EDANewsApp.getInstance();

        Intent intent = getIntent();
        int itemId = intent.getIntExtra("newsItemId", 0);

        String url = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id="+itemId;

        JsonObjectRequest request = new JsonObjectRequest(url, listener, errorListener);

        app.requestQueue.add(request);

        Button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = getIntent();
                int itemId = intent.getIntExtra("newsItemId", 0);
                String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;

                Intent buttonIntent = new Intent();
                buttonIntent.setAction(Intent.ACTION_VIEW);
                buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
                buttonIntent.setData(Uri.parse(shareUrl));
                startActivity(buttonIntent);
            }
        });
    };
活动\u新闻\u item.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".NewsItemActivity">

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <TextView
                android:id="@+id/itemTitle"
                android:layout_margin="15dp"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textSize="20sp"
                android:textStyle="bold"
                android:textColor="#3183b9"/>

            <TextView
                android:id="@+id/itemDate"
                android:layout_marginLeft="15dp"
                android:layout_marginBottom="15dp"
                android:textSize="16sp"
                android:textStyle="italic"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                />

            <com.android.volley.toolbox.NetworkImageView
                android:id="@+id/itemImage"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                />

            <TextView
                android:id="@+id/itemContent"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_margin="20sp"
                android:textSize="16sp"
                />

            <Button
                android:id="@+id/BrowserButton"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="View in browser"
                android:layout_marginLeft="15dp"
                style="@style/BrowserButton"
                />

        </LinearLayout>
    </ScrollView>

</RelativeLayout>

更改

Button.setOnClickListener(new View.OnClickListener() { ... });

您需要在
按钮的实例上调用
setOnClickListener
方法,而不是在类本身上调用。

问题在于:

 Button.setOnClickListener(new View.OnClickListener() {
您必须在
onCreate()
中声明它,就像您在
textview中声明的那样:

Button btn;
为什么?

因为如果你把它放在
onCreate()
上,你将无法在
onCreate()
之外使用这个对象,所以建议把它作为公共对象

然后在
onCreate()中执行以下操作:

btn = (Button)findViewById(R.id.BrowserButton);
btn = (Button)findViewById(R.id.BrowserButton);
btn.setOnClickListener(this);
然后执行
OnclickListener()
,如下所示:

btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = getIntent();
            int itemId = intent.getIntExtra("newsItemId", 0);
            String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;

            Intent buttonIntent = new Intent();
            buttonIntent.setAction(Intent.ACTION_VIEW);
            buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            buttonIntent.setData(Uri.parse(shareUrl));
            startActivity(buttonIntent);
        }
    });
public class NewsItemActivity extends AppCompatActivity implements View.OnClickListener {
public void onClick(View v) {
switch(v.getId()) {
  case R.id.BrowserButton:         
      Intent intent = getIntent();
            int itemId = intent.getIntExtra("newsItemId", 0);
            String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;

            Intent buttonIntent = new Intent();
            buttonIntent.setAction(Intent.ACTION_VIEW);
            buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            buttonIntent.setData(Uri.parse(shareUrl));
            startActivity(buttonIntent);
        }
      break;

}
我仍然收到onClickListener的一个错误,说它“无法解析符号”

请确保已导入以下内容:

import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
您可以这样做:

添加
实现OnClickListener{
,如下所示:

btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = getIntent();
            int itemId = intent.getIntExtra("newsItemId", 0);
            String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;

            Intent buttonIntent = new Intent();
            buttonIntent.setAction(Intent.ACTION_VIEW);
            buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            buttonIntent.setData(Uri.parse(shareUrl));
            startActivity(buttonIntent);
        }
    });
public class NewsItemActivity extends AppCompatActivity implements View.OnClickListener {
public void onClick(View v) {
switch(v.getId()) {
  case R.id.BrowserButton:         
      Intent intent = getIntent();
            int itemId = intent.getIntExtra("newsItemId", 0);
            String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;

            Intent buttonIntent = new Intent();
            buttonIntent.setAction(Intent.ACTION_VIEW);
            buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            buttonIntent.setData(Uri.parse(shareUrl));
            startActivity(buttonIntent);
        }
      break;

}
然后在
按钮上执行以下操作:

btn = (Button)findViewById(R.id.BrowserButton);
btn = (Button)findViewById(R.id.BrowserButton);
btn.setOnClickListener(this);
然后添加一个
开关盒
,如下所示:

btn.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = getIntent();
            int itemId = intent.getIntExtra("newsItemId", 0);
            String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;

            Intent buttonIntent = new Intent();
            buttonIntent.setAction(Intent.ACTION_VIEW);
            buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            buttonIntent.setData(Uri.parse(shareUrl));
            startActivity(buttonIntent);
        }
    });
public class NewsItemActivity extends AppCompatActivity implements View.OnClickListener {
public void onClick(View v) {
switch(v.getId()) {
  case R.id.BrowserButton:         
      Intent intent = getIntent();
            int itemId = intent.getIntExtra("newsItemId", 0);
            String shareUrl = "http://www.efstratiou.info/projects/newsfeed/getItem.php?id=" + itemId;

            Intent buttonIntent = new Intent();
            buttonIntent.setAction(Intent.ACTION_VIEW);
            buttonIntent.addCategory(Intent.CATEGORY_BROWSABLE);
            buttonIntent.setData(Uri.parse(shareUrl));
            startActivity(buttonIntent);
        }
      break;

}

}

他应该用
onCreate()
创建
按钮的对象,如果他想在某处使用它,它可能会导致NPE…)嗨,对不起,我真的不明白这意味着什么?@user3005003看到我的答案了吗?我告诉过你怎么做。@Skizo不一定。这取决于他将如何使用它。如果其他类方法无法访问变量,则最好在本地声明变量。我知道@Sparta,但这是值得推荐的:)谢谢,这非常有用,解释也很有用。我仍然收到onClickListener的一个错误,说它“无法解析符号”。您是否导入了
import android.view.view.onClickListener?当我这么做的时候,它说它没用了,看看你有没有这三种进口产品请告诉我你在用什么进口产品