简体   繁体   English

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

[英]Error: “Cannot make a static reference to the non-static method startActivity(Intent) from the type Activity”

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. 请帮忙。

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应存在于特定活动实例的上下文中。

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 ie ListActivity and on click an item sub Activity ie Activity shows its detail Plz尝试这个样本它有一个主要的活动,即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 和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"));
        }
    }




}

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 希望这会有所帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 获取错误“无法对类型为Intent的非静态方法putExtra(String,String)进行静态引用” - Getting error “Cannot make a static reference to the non-static method putExtra(String, String) from the type Intent” 无法解析webView且无法从Activity类型静态引用非静态方法findViewById(int) - webView cannot be resolved & cannot make a static reference to the non-static method findViewById(int) from the type Activity Java错误:无法对非静态方法进行静态引用 - Java Error: Cannot make a static reference to the non-static method 类型无法从类型View静态引用非静态方法setVisibility(int) - Type Cannot make a static reference to the non-static method setVisibility(int) from the type View 无法从类型XHTMLImporter静态引用非静态方法setHyperlinkStyle(String) - Cannot make a static reference to the non-static method setHyperlinkStyle(String) from the type XHTMLImporter 无法从类型Cmd对非静态方法getVideoURL()进行静态引用 - Cannot make a static reference to the non-static method getVideoURL() from the type Cmd 无法从类型teaminfo对非静态方法acceptadd(String)进行静态引用 - Cannot make a static reference to the non-static method acceptadd(String) from the type teaminfo 无法从View MainActivity.java类型静态引用非静态方法invalidate() - Cannot make a static reference to the non-static method invalidate() from the type View MainActivity.java Java - 无法从类型“something”中对非静态方法“something”进行静态引用 - Java - Cannot make a static reference to the non-static method "something" from the type "something" 无法使 static 引用非静态方法 --- 从类型 --- Java Bukkit Minecraft - Cannot make a static reference to the non-static method --- from the type --- Java Bukkit Minecraft
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM