java 错误:“无法从类型 Activity 对非静态方法 startActivity(Intent) 进行静态引用”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5591350/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Error: "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity"
提问by shripal
private int pos = position;
@Override
public void onClick(View v) {
Toast.makeText(context, "Click-" + String.valueOf(pos), Toast.LENGTH_SHORT).show();
iBean = (InboxBean)result.get(position);
ConstantData.inbox_subject = iBean.subject;
ConstantData.inbox_body = iBean.body;
ConstantData.inbox_postDate = iBean.postdate;
startActivity(new Intent(InboxActivity.this,InboxDetailActivity.class));//getting error at startActivity
}
});
I am getting the following error at startActivity line:
"Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity"
我在 startActivity 行收到以下错误:
"Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity"
Please help.
请帮忙。
回答by Vladimir Ivanov
I assume you have this in the inner static class of your activity. Don't do that. OnClickListeners should exist in the context of the particular instance of activity.
我假设您在活动的内部静态类中有这个。不要那样做。OnClickListeners 应该存在于特定活动实例的上下文中。
回答by Jaldip Katre
To make static reference to non static methods, you need to call them by using the context of the activity or application.
要对非静态方法进行静态引用,您需要使用活动或应用程序的上下文来调用它们。
This is also true when you need to call activity from non activity class.
当您需要从非活动类调用活动时也是如此。
The following type of code can be used
可以使用以下类型的代码
Intent cc=new Intent(yourcontext,Yourpackagename.ClassName.class);
yourcontext.startActivity(cc);
回答by Imran
Plz Try this sample its has one main activity i.e. ListActivity and on click an item sub Activity i.e Activity shows its detail
请试试这个示例,它有一个主要活动,即 ListActivity,点击一个项目子活动,即活动显示其详细信息
Main Activity Class "listActivity" is as
主要活动类“listActivity”是
package edu.android.clickablelist;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.app.ListActivity;
import android.content.Intent;
public class listActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] {"Linux", "Windows7", "Eclipse", "Suse", "Ubuntu", "Solaris", "Android", "iPhone"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_checked, names));
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
final String keyword = o.toString();
Bundle b = new Bundle();
b.putString("Test", keyword );
Intent i = new Intent(listActivity.this,
DetailsActivity.class);
i.putExtras(b);
startActivityForResult(i,0);
}
}
and SubActivity is As
和子活动是
package edu.android.clickablelist;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.view.*;
public class DetailsActivity extends Activity
{
TextView tv;
private static final int SUB_ACTIVITY_REQUEST_CODE = 0;
@Override
public void onCreate(Bundle Savedinstances)
{
super.onCreate(Savedinstances);
setContentView(R.layout.details);
//for getting parameters passed from listActivity.java
Bundle b = this.getIntent().getExtras();
String selItem = b.getString("Test");
//how to set text of selected item?
tv = (TextView) findViewById(R.id.SelectedItem);
tv.setText("You are viewing Details of " + selItem);
Button btn = (Button) findViewById(R.id.btnClick2);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
setResult(RESULT_OK);
finish();
}
});
}
//this is important to get result of StartActivityForResult
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == SUB_ACTIVITY_REQUEST_CODE){
Bundle b = data.getExtras();
tv.setText(b.getString("Test"));
}
}
}
You also need to add reference of sub activity in "AndroidManifest.xml"
您还需要在“AndroidManifest.xml”中添加子活动的引用
For Above codes Sample XML is
对于上述代码示例 XML 是
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.android.clickablelist"
android:versionCode="1"
android:versionName="1.0">
<application android:icon="@drawable/icon" android:label="@string/app_name">
<activity android:name=".listActivity"
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=".DetailsActivity"
class=".DetailsActivity"
android:label="DetailsActivity">
</activity>
</application>
</manifest>
Hopefully this will helps
希望这会有所帮助