相关疑难解决方法(0)

接收者作为Android中的内部类

在我的代码中有一个扩展的内部类BroadcastReceiver.

我已将以下行添加到AndroidManifest.xml:

<receiver android:name="OuterClass$InnerClass android:enabled="true"/>
Run Code Online (Sandbox Code Playgroud)

但是我收到以下错误:

无法实例化接收器org.example.test.OuterClass $ InnerClass

我该如何解决这个问题?

java android broadcastreceiver

19
推荐指数
3
解决办法
2万
查看次数

无法实例化接收器:类没有零参数

虽然我知道已经有一些答案,但我不太了解它们,因为我只是Android编程的初学者.我尝试使用以下代码实例化我的接收器:

<receiver
    android:name="com.example.android.exampleapp.MainActivity$NetworkChangeReceiver"
    android:enabled="true"
    android:label="NetworkChangeReceiver">
    <intent-filter>
        <action android:name="android.net.wifi.STATE_CHANGE" />
    </intent-filter>
</receiver>
Run Code Online (Sandbox Code Playgroud)

但它不起作用.logcat说:

java.lang.RuntimeException:
Unable to instantiate receiver com.example.android.exampleapp.MainActivity$NetworkChangeReceiver:
    java.lang.InstantiationException:
        class com.example.android.exampleapp.MainActivity$NetworkChangeReceiver has no zero argument constructor
Run Code Online (Sandbox Code Playgroud)

我在MainActivity.java中的部分代码如下所示:

public class NetworkChangeReceiver extends BroadcastReceiver {
    /* All my code that reacts when WiFi state changes are here */
}
Run Code Online (Sandbox Code Playgroud)

我知道这个问题听起来很容易,但我真的不知道如何解决这个错误.我读过这个(我认为它有点无效 - 我没有空的构造函数)和一堆其他在线教程,但我仍然无法得到它.任何帮助,将不胜感激 :)

java android broadcastreceiver android-broadcastreceiver

4
推荐指数
1
解决办法
3762
查看次数

java.lang.InstantiationException:类com.e没有零参数构造函数

我收到此错误

java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.e.www.i/com.e.www.i.Search}: java.lang.InstantiationException: class com.e.www.i.Search has no zero argument constructor
Run Code Online (Sandbox Code Playgroud)

这是下面的类:

public class Search extends Activity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;

    private RecyclerView mRecyclerView1;
    private RecyclerView.Adapter mAdapter1;
    Context mContext;

    public Search(Context context) {

        mContext = context; // I guess the error is here, but I need to define context for below MyAdapter
    }
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recycler_view);

        mRecyclerView = (RecyclerView) findViewById(R.id.my_recycler_view);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(
                new LinearLayoutManager(this,LinearLayoutManager.HORIZONTAL, false));
        mAdapter = new MyAdapter(getDataSet(),mContext); …
Run Code Online (Sandbox Code Playgroud)

android

3
推荐指数
1
解决办法
2万
查看次数