错误:“无法从类型Activity中对非静态方法startActivity(Intent)进行静态引用”

时间:2011-04-08 06:36:25

标签: java android

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 
                    }
            });

我在startActivity行遇到以下错误: "Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity"

请帮忙。

3 个答案:

答案 0 :(得分:6)

我假设你在活动的内部静态类中有这个。不要那样做。 OnClickListeners应该存在于特定活动实例的上下文中。

答案 1 :(得分:5)

要对非静态方法进行静态引用,您需要使用活动或应用程序的上下文来调用它们。

当您需要从非活动类调用活动时也是如此。

可以使用以下类型的代码

Intent cc=new Intent(yourcontext,Yourpackagename.ClassName.class);
yourcontext.startActivity(cc);

答案 2 :(得分:1)

Plz试试这个样本,它有一个主要的活动,即ListActivity,点击一个项目子活动,即活动显示其细节

主要活动类“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);
    }




}

和SubActivity为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"));
        }
    }




}

您还需要在“AndroidManifest.xml”

中添加子活动的引用

For Above codes Sample 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> 

希望这会有所帮助