2015-06-19 42 views
-1

我想用菜单打开一个活动,这个工作,没有任何问题。然而,当我把一个片段上的活动我打开,我得到这个错误用一个片段打开一个活动?

06-19 16:38:46.253 2732-2732/com.example.broodjepoep.stapvoorstap E/AndroidRuntime﹕ FATAL EXCEPTION: main 
    Process: com.example.broodjepoep.stapvoorstap, PID: 2732 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.broodjepoep.stapvoorstap/com.example.broodjepoep.stapvoorstap.LayoutFragmentOne}: android.view.InflateException: Binary XML file line #8: Error inflating class fragment 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2212) 
      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 
      at android.app.ActivityThread.access$800(ActivityThread.java:144) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
      at android.os.Handler.dispatchMessage(Handler.java:102) 
      at android.os.Looper.loop(Looper.java:136) 
      at android.app.ActivityThread.main(ActivityThread.java:5146) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
      at dalvik.system.NativeStart.main(Native Method) 
    Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class fragment 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:713) 
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
      at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228) 
      at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102) 
      at com.example.broodjepoep.stapvoorstap.LayoutFragmentOne.onCreate(LayoutFragmentOne.java:15) 
      at android.app.Activity.performCreate(Activity.java:5231) 
      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5146) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
            at dalvik.system.NativeStart.main(Native Method) 
    Caused by: java.lang.ClassCastException: you need to implement senddata method 
      at com.example.broodjepoep.stapvoorstap.FirstFragment.onAttach(FirstFragment.java:56) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:849) 
      at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1040) 
      at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1142) 
      at android.app.Activity.onCreateView(Activity.java:4786) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:689) 
            at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
            at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
            at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:228) 
            at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102) 
            at com.example.broodjepoep.stapvoorstap.LayoutFragmentOne.onCreate(LayoutFragmentOne.java:15) 
            at android.app.Activity.performCreate(Activity.java:5231) 
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2169) 
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2271) 
            at android.app.ActivityThread.access$800(ActivityThread.java:144) 
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1205) 
            at android.os.Handler.dispatchMessage(Handler.java:102) 
            at android.os.Looper.loop(Looper.java:136) 
            at android.app.ActivityThread.main(ActivityThread.java:5146) 
            at java.lang.reflect.Method.invokeNative(Native Method) 
            at java.lang.reflect.Method.invoke(Method.java:515) 
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732) 
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566) 
            at dalvik.system.NativeStart.main(Native Method) 

fwef背后菜单项

码点击

@Override 
    public boolean onOptionsItemSelected(MenuItem item) 
    { 
     // Handle action bar item clicks here. The action bar will 
     // automatically handle clicks on the Home/Up button, so long 
     // as you specify a parent activity in AndroidManifest.xml. 
     int id = item.getItemId(); 

     //noinspection SimplifiableIfStatement 
     if (id == R.id.action_settings) 
     { 
      return true; 
     } 


     Intent iOne = new Intent(this,LayoutFragmentOne.class); 
     Intent iTwo = new Intent(this,LayoutFragmentTwo.class); 

     switch(id) 
     { 
      case R.id.fragOne: 
       startActivity(iOne); 
       return true; 
      case R.id.fragTwo: 
       startActivity(iTwo); 
       return true; 
     } 
     return super.onOptionsItemSelected(item); 
    } 

LayoutFragmentOne.class

package com.example.broodjepoep.stapvoorstap; 

import android.os.Bundle; 
import android.support.v7.app.ActionBarActivity; 

/** 
* Created by Pizza on 19-6-2015. 
*/ 
public class LayoutFragmentOne extends ActionBarActivity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_fragment_one); 
    } 
} 

LayoutFragmentTwo.class

package com.example.broodjepoep.stapvoorstap; 

import android.os.Bundle; 
import android.os.PersistableBundle; 
import android.support.v7.app.ActionBarActivity; 

/** 
* Created by Pizza on 19-6-2015. 
*/ 
public class LayoutFragmentTwo extends ActionBarActivity 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_fragment_two); 
    } 
} 

如果我把两个片段放在MainActivity上,没有错误,我可以在它们之间进行通信而没有任何问题。只有当我尝试启动一个活动时才会出现问题带有一个片段

这是一个已知的问题,或者是否存在已知的修复?

我不知道,如果它是需要的,但这里有落后片段一个片段两个代码

一:

package com.example.broodjepoep.stapvoorstap; 

import android.app.Activity; 
import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.Button; 
import android.widget.EditText; 

/** 
* Created by Pizza on 19-6-2015. 
*/ 
public class FirstFragment extends Fragment 
{ 
    SendMessage sm; 
    Button btnSendData; 
    EditText tb1; 
    @Override 
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) 
    { 
     View v; 
     v = inflater.inflate(R.layout.fragment_one,container,false); 
     btnSendData = (Button)v.findViewById(R.id.btnSendData); 
     tb1 = (EditText)v.findViewById(R.id.tb1); 
     btnSendData.setOnClickListener(new Button.OnClickListener() 
     { 
      @Override 
      public void onClick(View v) 
      { 
       String message; 
       message = tb1.getText().toString(); 
       sm.sendData(message); 
      } 
     }); 
     return v; 

    } 



    interface SendMessage 
    { 
     public void sendData(String message); 
    } 

    @Override 
    public void onAttach(Activity activity) 
    { 
     super.onAttach(activity); 
     try 
     { 
      sm = (SendMessage) activity; 
     } 
     catch (ClassCastException e) 
     { 
      throw new ClassCastException("you need to implement senddata method"); 
     } 
    } 
} 

二:

package com.example.broodjepoep.stapvoorstap; 

import android.app.Fragment; 
import android.os.Bundle; 
import android.support.annotation.Nullable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.TextView; 

/** 
* Created by Pizza on 14-6-2015. 
*/ 
public class SecondFragment extends Fragment 
{ 
    TextView txt; 
    @Nullable 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) 
    { 
     View v; 
     v = inflater.inflate(R.layout.fragment_two,container,false); 
     txt = (TextView)v.findViewById(R.id.txt); 
     return v; 
    } 
    public void getData(String message) 
    { 
     txt.setText(message); 
    } 

} 

回答

0

的错误是很清楚:

Caused by: java.lang.ClassCastException: you need to implement senddata method 

当一个片段被附加到一个没有实现上述接口的活动时会发生什么情况。这一切都写在这个代码:

@Override 
public void onAttach(Activity activity) 
{ 
    super.onAttach(activity); 
    try 
    { 
     sm = (SendMessage) activity; 
    } 
    catch (ClassCastException e) 
    { 
     throw new ClassCastException("you need to implement senddata method"); 
    } 
} 

希望使用这样的代码片段活动,应实现的接口:

public class LayoutFragmentOne extends ActionBarActivity implements SendMessage 
{ 
    @Override 
    public void onCreate(Bundle savedInstanceState) 
    { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.layout_fragment_one); 
    } 

    @Override 
    public void sendData(String message) { 

    } 
} 
+1

非常感谢!我仍然不是android的专业版,我真的不喜欢android studio中的调试器。我更喜欢视觉工作室中的调试器,但这是我个人的观点。从错误中学习:)再次感谢:) – stefan

+0

@stefan欢迎:) – Simas

相关问题