2017-04-09 88 views
-2

錯誤和聊天活動對話框如下所示。無法實例化活動:class com.pz.mediatonmessanger.ChatDialogActivity沒有零參數構造函數

04-09 09:03:23.509 2722-2722/com.pz.mediatonmessanger E/AndroidRuntime: FATAL EXCEPTION: main 
    Process: com.pz.mediatonmessanger, PID: 2722 
    java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.pz.mediatonmessanger/com.pz.mediatonmessanger.ChatDialogActivity}: java.lang.InstantiationException: class com.pz.mediatonmessanger.ChatDialogActivity has no zero argument constructor 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2209) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
    at android.app.ActivityThread.access$800(ActivityThread.java:144) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5221) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.InstantiationException: class com.pz.mediatonmessanger.ChatDialogActivity has no zero argument constructor 
    at java.lang.Class.newInstance(Class.java:1563) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1065) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
    at android.app.ActivityThread.access$800(ActivityThread.java:144) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5221) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 
    Caused by: java.lang.NoSuchMethodException: <init> [] 
    at java.lang.Class.getConstructor(Class.java:531) 
    at java.lang.Class.getDeclaredConstructor(Class.java:510) 
    at java.lang.Class.newInstance(Class.java:1561) 
    at android.app.Instrumentation.newActivity(Instrumentation.java:1065) 
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2199) 
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360) 
    at android.app.ActivityThread.access$800(ActivityThread.java:144) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278) 
    at android.os.Handler.dispatchMessage(Handler.java:102) 
    at android.os.Looper.loop(Looper.java:135) 
    at android.app.ActivityThread.main(ActivityThread.java:5221) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at java.lang.reflect.Method.invoke(Method.java:372) 
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694) 

代碼

package com.pz.mediatonmessanger; 

import android.app.ProgressDialog; 
import android.content.Intent; 
import android.support.design.widget.FloatingActionButton; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.ListView; 

import com.pz.mediatonmessanger.Adapter.ChatDialogsAdapter; 
import com.quickblox.auth.QBAuth; 
import com.quickblox.auth.session.BaseService; 
import com.quickblox.auth.session.QBSession; 
import com.quickblox.chat.QBChatService; 
import com.quickblox.chat.QBRestChatService; 
import com.quickblox.chat.model.QBChatDialog; 
import com.quickblox.core.QBEntityCallback; 
import com.quickblox.core.exception.BaseServiceException; 
import com.quickblox.core.exception.QBResponseException; 
import com.quickblox.core.request.QBRequestBuilder; 
import com.quickblox.core.request.QBRequestGetBuilder; 
import com.quickblox.users.model.QBUser; 

import java.util.ArrayList; 




public class ChatDialogActivity extends AppCompatActivity { 

    FloatingActionButton floatingActionButton; 
    ListView lstChatDialogs; 


    public ChatDialogActivity(FloatingActionButton floatingActionButton) { 
     this.floatingActionButton = floatingActionButton; 
    } 

    public ChatDialogActivity(ListView lstChatDialogs) { 
     this.lstChatDialogs = lstChatDialogs; 
    } 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_chat_dialog); 

     createSessionForChat(); 

     lstChatDialogs = (ListView)findViewById(R.id.lstChatDialogs); 

     loadChatDialogs(); 



     floatingActionButton = (FloatingActionButton) findViewById(R.id.chatdialog_adduser); 

     floatingActionButton.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       Intent intent = new Intent(ChatDialogActivity.this, ListUsersActivity.class); 
       startActivity(intent); 
      } 
     }); 
    } 

    private void loadChatDialogs() { 

     QBRequestGetBuilder requestBuilder = new QBRequestGetBuilder(); 
     requestBuilder.setLimit(100); 

     QBRestChatService.getChatDialogs(null,requestBuilder).performAsync(new QBEntityCallback<ArrayList<QBChatDialog>>() { 
      @Override 
      public void onSuccess(ArrayList<QBChatDialog> qbChatDialogs, Bundle bundle) { 

       //Kod 
       ChatDialogsAdapter adapter = new ChatDialogsAdapter(getBaseContext(),qbChatDialogs); 
       lstChatDialogs.setAdapter(adapter); 
       adapter.notifyDataSetChanged(); 
      } 

      @Override 
      public void onError(QBResponseException e) { 
        Log.e("Błąd",e.getMessage()); 
      } 
     }); 
    } 

    private void createSessionForChat() { 
     final ProgressDialog mDialog = new ProgressDialog(ChatDialogActivity.this); 
     mDialog.setMessage("Ładowanie..."); 
     mDialog.setCanceledOnTouchOutside(false); 
     mDialog.show(); 

     String user,password; 
     user = getIntent().getStringExtra("user"); 
     password = getIntent().getStringExtra("password"); 

     final QBUser qbUser = new QBUser(user,password); 
     QBAuth.createSession(qbUser).performAsync(new QBEntityCallback<QBSession>() { 
      @Override 
      public void onSuccess(QBSession qbSession, Bundle bundle) { 
       qbUser.setId(qbSession.getUserId()); 
       try { 
        qbUser.setPassword(BaseService.getBaseService().getToken()); 
       } catch (BaseServiceException e) { 
        e.printStackTrace(); 
       } 

       QBChatService.getInstance().login(qbUser, new QBEntityCallback() { 
        @Override 
        public void onSuccess(Object o, Bundle bundle) { 
         mDialog.dismiss(); 
        } 

        @Override 
        public void onError(QBResponseException e) { 
         Log.e("Błąd",""+e.getMessage()); 
        } 
       }); 
      } 

      @Override 
      public void onError(QBResponseException e) { 

      } 
     }); 

    } 
} 
+1

你'Activity'類不應該有任何顯式的構造的。刪除你有的。 –

回答

0

活動沒有在Android的構造函數,當你與其他類你不能創建它們。它們由系統實例化,所有你需要做的就是用context.startActivity(intent)來啓動它們。

從這個活動中刪除所有的構造,並獲得與findViewById意見的參考,你應該罰款

相關問題