如何识别错误?
package com.danny;
import com.danny.myfragmentactivity.R;
import android.os.Bundle;
//import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
public class MainActivity extends ActionBarActivity {
@Override
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState != null) {
return;
}
//try {
HeadlineFragment headlineFragment=new HeadlineFragment();
// headlineFragment.setArguments(getIntent().getExtras());
getFragmentManager().beginTransaction()
.add(R.id.container, headlineFragment).commit();
//}
//catch (ClassCastException e){
// throw new ClassCastException(this.toString()+"error occur in MainActivity Java");
//}
}
}
Package com.danny;
import com.danny.myfragmentactivity.R;
import android.app.Activity;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class HeadlineFragment extends Fragment {
private static final CharSequence myText = "hello danny";
public HeadlineFragment(){}
@Override
public void onAttach(Activity activity){
super.onAttach(activity);
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View inflaterView=inflater.inflate(R.layout.headline_fragment, container, false);
return inflaterView;
}
@Override
public void onStart(){
super.onStart();
updateActivityDisplay();
}
void updateActivityDisplay(){
//try {
TextView headlineTextView = (TextView) getActivity().findViewById(R.id.headline_fragmentXML);
headlineTextView.setText(myText);
//}
//catch (ClassCastException e) {
// throw new ClassCastException (this.toString()+"error in updateActivityDisplay Class");
//}
}
}
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.danny.myfragmentactivity.MainActivity"
tools:ignore="MergeRootFrame" />
<?xml version="1.0" encoding="utf-8"?>
<Textview xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/headline_fragmentXML"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/green"
></Textview>
05-23 14:36:03.476: D/AndroidRuntime(5503): Shutting down VM
05-23 14:36:03.476: W/dalvikvm(5503): threadid=1: thread exiting with uncaught exception (group=0x4144a2a0)
05-23 14:36:03.476: E/AndroidRuntime(5503): FATAL EXCEPTION: main
05-23 14:36:03.476: E/AndroidRuntime(5503): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.danny.myfragmentactivity/com.danny.myfragmentactivity.MainActivity}: java.lang.ClassNotFoundException: com.danny.myfragmentactivity.MainActivity
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2034)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.app.ActivityThread.access$700(ActivityThread.java:140)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.os.Handler.dispatchMessage(Handler.java:99)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.os.Looper.loop(Looper.java:137)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.app.ActivityThread.main(ActivityThread.java:4935)
05-23 14:36:03.476: E/AndroidRuntime(5503): at java.lang.reflect.Method.invokeNative(Native Method)
05-23 14:36:03.476: E/AndroidRuntime(5503): at java.lang.reflect.Method.invoke(Method.java:511)
05-23 14:36:03.476: E/AndroidRuntime(5503): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
05-23 14:36:03.476: E/AndroidRuntime(5503): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
05-23 14:36:03.476: E/AndroidRuntime(5503): at dalvik.system.NativeStart.main(Native Method)
05-23 14:36:03.476: E/AndroidRuntime(5503): Caused by: java.lang.ClassNotFoundException: com.danny.myfragmentactivity.MainActivity
05-23 14:36:03.476: E/AndroidRuntime(5503): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
05-23 14:36:03.476: E/AndroidRuntime(5503): at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
05-23 14:36:03.476: E/AndroidRuntime(5503): at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
05-23 14:36:03.476: E/AndroidRuntime(5503): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2025)
05-23 14:36:03.476: E/AndroidRuntime(5503): ... 11 more
05-23 14:36:15.007: I/Process(5503): Sending signal. PID: 5503 SIG: 9
答案 0 :(得分:0)
创建inflaterView全局变量并使用它而不是getActivity()。
View inflaterView;
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
inflaterView=inflater.inflate(R.layout.headline_fragment, container, false);
return inflaterView;
}
void updateActivityDisplay(){
TextView headlineTextView = (TextView) inflaterView.findViewById(R.id.headline_fragmentXML);
headlineTextView.setText(myText);
}
答案 1 :(得分:0)
使用getSupportFragmentManager
代替getFragmentManager
。
答案 2 :(得分:0)
java.lang.ClassNotFoundException: com.danny.myfragmentactivity.MainActivity
当您的清单文件显示在MainActivity
时,您的com.danny
位于包com.danny.myfragmentactivity
中。确保清单和代码一致。