我想在我的一个活动中添加一个按钮(显示一篇新闻文章),因此当用户点击该按钮时,该文章将在其浏览器中打开.到目前为止,我已经在我的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); } }); };
activity_news_item.xml
Mohammed Aou.. 5
更改
Button.setOnClickListener(new View.OnClickListener() { ... });
至
Button button = (Button) findViewById(R.id.BrowserButton); button.setOnClickListener(new View.OnClickListener() { ... });
您需要setOnClickListener
在实例上调用该方法Button
,而不是在类本身上调用.
更改
Button.setOnClickListener(new View.OnClickListener() { ... });
至
Button button = (Button) findViewById(R.id.BrowserButton); button.setOnClickListener(new View.OnClickListener() { ... });
您需要setOnClickListener
在实例上调用该方法Button
,而不是在类本身上调用.