Error: "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity"
private int pos = position;
@Override
public开发者_如何学Go 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"
Please help.
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.
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);
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
Main Activity Class "listActivity" is as
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"
For Above codes Sample XML is
<?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
精彩评论