我正在尝试使用菜单打开一个活动,这没有任何问题。但是,当我在我打开的活动上放一个片段时,我收到此错误
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上放置两个片段,则没有错误,我可以毫无问题地在它们之间进行通信。只有当我尝试启动活动时才会出现此问题上面有一个片段
这是一个已知的问题,还是有一个已知的修复?
我不知道是否需要它,但这里是Fragment one和Fragment two背后的代码
一:
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 :(得分: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) {
}
}