此错误已在此论坛上出现数百万次,但我真的找不到解决此错误的答案!我正在使用带有碎片的导航抽屉,到目前为止我已经合并了(我想我有)倒数计时器和谷歌地图。我不知道问题是什么我已经尝试“几乎”在互联网上提到的每个解决方案都有这个错误。
ERROR:
01-26 15:10:47.923: E/AndroidRuntime(3731): FATAL EXCEPTION: main
01-26 15:10:47.923: E/AndroidRuntime(3731): Process: com.example.TheWeddingApp, PID: 3731
01-26 15:10:47.923: E/AndroidRuntime(3731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.TheWeddingApp/com.example.TheWeddingApp.MainActivity}: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.navigationdrawer.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.access$800(ActivityThread.java:139)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.os.Handler.dispatchMessage(Handler.java:102)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.os.Looper.loop(Looper.java:136)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.reflect.Method.invokeNative(Native Method)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.reflect.Method.invoke(Method.java:515)
01-26 15:10:47.923: E/AndroidRuntime(3731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-26 15:10:47.923: E/AndroidRuntime(3731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-26 15:10:47.923: E/AndroidRuntime(3731): at dalvik.system.NativeStart.main(Native Method)
01-26 15:10:47.923: E/AndroidRuntime(3731): Caused by: android.support.v4.app.Fragment$InstantiationException: Unable to instantiate fragment com.example.navigationdrawer.FragmentOne: make sure class name exists, is public, and has an empty constructor that is public
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.support.v4.app.Fragment.instantiate(Fragment.java:401)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.support.v4.app.Fragment.instantiate(Fragment.java:369)
01-26 15:10:47.923: E/AndroidRuntime(3731): at com.example.TheWeddingApp.MainActivity.onCreate(MainActivity.java:54)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.Activity.performCreate(Activity.java:5248)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
01-26 15:10:47.923: E/AndroidRuntime(3731): ... 11 more
01-26 15:10:47.923: E/AndroidRuntime(3731): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.example.navigationdrawer.FragmentOne" on path: DexPathList[[zip file "/mnt/asec/com.example.TheWeddingApp-1/pkg.apk"],nativeLibraryDirectories=[/mnt/asec/com.example.TheWeddingApp-1/lib, /vendor/lib, /system/lib]]
01-26 15:10:47.923: E/AndroidRuntime(3731): at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:56)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.ClassLoader.loadClass(ClassLoader.java:497)
01-26 15:10:47.923: E/AndroidRuntime(3731): at java.lang.ClassLoader.loadClass(ClassLoader.java:457)
01-26 15:10:47.923: E/AndroidRuntime(3731): at android.support.v4.app.Fragment.instantiate(Fragment.java:391)
01-26 15:10:47.923: E/AndroidRuntime(3731): ... 16 more
的Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.TheWeddingApp"
android:versionCode="1"
android:versionName="1.0" >
<permission
android:name="com.example.TheWeddingApp.permission.MAPS_RECEIVE"
android:protectionLevel="signature" />
<uses-permission android:name="com.example.TheWeddingApp.permission.MAPS_RECEIVE" />
<uses-sdk
android:minSdkVersion="13"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="com.google.android.providers.gsf.permission.READ_GSERVICES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<!-- Required to show current location -->
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-feature
android:glEsVersion="0x00020000"
android:required="true" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.example.TheWeddingApp.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<meta-data
android:name="com.google.android.maps.v2.API_KEY"
android:value="AIzaSyBTbyjkzYDeEFGsljSoLBr54riAjhWCVJg" />
</application>
</manifest>
MainActivity.java
package com.example.TheWeddingApp;
import android.app.ActionBar;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.support.v4.widget.DrawerLayout;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
final String[] data ={"Home","Venue","Gallery","Meet the Groom","Meet `the Bride"};`
final String[] fragments ={
"com.example.navigationdrawer.FragmentOne",
"com.example.navigationdrawer.FragmentTwo",
"com.example.navigationdrawer.FragmentThree",
"com.example.navigationdrawer.FragmentFour",
"com.example.navigationdrawer.FragmentFive"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActionBar().getThemedContext(), android.R.layout.simple_list_item_1, data);
ActionBar bar = getActionBar();
bar.setBackgroundDrawable(new ColorDrawable(Color.parseColor("#ff0000")));
final DrawerLayout drawer = (DrawerLayout)findViewById(R.id.drawer_layout);
final ListView navList = (ListView) findViewById(R.id.drawer);
navList.setAdapter(adapter);
navList.setOnItemClickListener(new OnItemClickListener(){
@Override
public void onItemClick(AdapterView<?> parent, View view, final int pos,long id){
drawer.setDrawerListener( new DrawerLayout.SimpleDrawerListener(){
@Override
public void onDrawerClosed(View drawerView){
super.onDrawerClosed(drawerView);
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main, Fragment.instantiate(MainActivity.this, fragments[pos]));
tx.commit();
}
});
drawer.closeDrawer(navList);
}
});
FragmentTransaction tx = getSupportFragmentManager().beginTransaction();
tx.replace(R.id.main,Fragment.instantiate(MainActivity.this, fragments[0]));
tx.commit();
}
}
activity_main.xml中
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<FrameLayout
android:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<ListView
android:id="@+id/drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#ff0000"
android:choiceMode="singleChoice"/>
</android.support.v4.widget.DrawerLayout>
FragmentOne.java
package com.example.TheWeddingApp;
import java.text.SimpleDateFormat;
import java.util.Date;
import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.support.v4.app.Fragment;
import android.support.v4.app.NavUtils;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;
public class FragmentOne extends Fragment {
private TextView tvDay, tvHour, tvMinute, tvSecond, tvEvent;
private LinearLayout linearLayout1, linearLayout2;
private Handler handler;
private Runnable runnable;
public static Fragment newInstance(Context context) {
FragmentOne f = new FragmentOne();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_one, null);
getActivity().setContentView(R.layout.fragment_one);
initUI();
countDownStart();
return root;
}
@SuppressLint("SimpleDateFormat")
private void initUI() {
linearLayout1 = (LinearLayout) getActivity().findViewById(R.id.ll1);
linearLayout2 = (LinearLayout) getActivity().findViewById(R.id.ll2);
tvDay = (TextView) getActivity().findViewById(R.id.txtTimerDay);
tvHour = (TextView) getActivity().findViewById(R.id.txtTimerHour);
tvMinute = (TextView) getActivity().findViewById(R.id.txtTimerMinute);
tvSecond = (TextView) getActivity().findViewById(R.id.txtTimerSecond);
tvEvent = (TextView) getActivity().findViewById(R.id.tvevent);
}
public void countDownStart() {
handler = new Handler();
runnable = new Runnable() {
@Override
public void run() {
handler.postDelayed(this, 1000);
try {
SimpleDateFormat dateFormat = new SimpleDateFormat(
"yyyy-MM-dd");
// Here Set your Event Date
Date futureDate = dateFormat.parse("2016-2-19");
Date currentDate = new Date();
if (!currentDate.after(futureDate)) {
long diff = futureDate.getTime()
- currentDate.getTime();
long days = diff / (24 * 60 * 60 * 1000);
diff -= days * (24 * 60 * 60 * 1000);
long hours = diff / (60 * 60 * 1000);
diff -= hours * (60 * 60 * 1000);
long minutes = diff / (60 * 1000);
diff -= minutes * (60 * 1000);
long seconds = diff / 1000;
tvDay.setText("" + String.format("%02d", days));
tvHour.setText("" + String.format("%02d", hours));
tvMinute.setText("" + String.format("%02d", minutes));
tvSecond.setText("" + String.format("%02d", seconds));
} else {
linearLayout1.setVisibility(View.VISIBLE);
linearLayout2.setVisibility(View.GONE);
tvEvent.setText("Android Event Start");
handler.removeCallbacks(runnable);
// handler.removeMessages(0);
}
} catch (Exception e) {
e.printStackTrace();
}
}
};
handler.postDelayed(runnable, 0);
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="@+id/ll1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#000000"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="@+id/tvevent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:singleLine="true"
android:text="Android Event Start"
android:textColor="#fff"
android:textSize="24sp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:id="@+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="10dp"
android:background="#000000"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="horizontal"
android:visibility="visible" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="@+id/txt_TimerDay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Days"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="@+id/txt_TimerHour"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Hour"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="@+id/txt_TimerMinute"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Minute"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/txtTimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="3"
android:gravity="center"
android:text="00"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="@+id/txt_TimerSecond"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Second"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#fff" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
FragmentTwo.java
package com.example.TheWeddingApp;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
public class FragmentTwo extends Fragment {
private GoogleMap googleMap;
public static Fragment newInstance(Context context) {
FragmentTwo f = new FragmentTwo();
return f;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.fragment_two, null);
getActivity().setContentView(R.layout.fragment_two);
try {
// Loading map
initilizeMap();
} catch (Exception e) {
e.printStackTrace();
}
return root;
}
private void initilizeMap() {
if (googleMap == null) {
googleMap = ((MapFragment) getActivity().getFragmentManager().findFragmentById(R.id.map)).getMap();
// check if map is created successfully or not
if (googleMap == null) {
Toast.makeText(getActivity().getApplicationContext(),
"Sorry! unable to create maps", Toast.LENGTH_SHORT)
.show();
}
}
}
@Override
public void onResume() {
super.onResume();
initilizeMap();
}
}
fragment_two.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
答案 0 :(得分:0)
您正在使用
final String[] fragments ={
"com.example.navigationdrawer.FragmentOne",
"com.example.navigationdrawer.FragmentTwo",
"com.example.navigationdrawer.FragmentThree",
"com.example.navigationdrawer.FragmentFour",
"com.example.navigationdrawer.FragmentFive"};
在Fragment
中实例化您的MainActivity
。在那里,您提供了Fragment
的完整限定路径,相应于堆栈跟踪,它不会退出。您Fragment
的套餐为com.example.TheWeddingApp
。您可以更改String[]
以反映真实路径。 E.g。
final String[] fragments ={
"com.example.TheWeddingApp.FragmentOne",
"com.example.TheWeddingApp.FragmentTwo",
"com.example.TheWeddingApp.FragmentThree",
"com.example.TheWeddingApp.FragmentFour",
"com.example.TheWeddingApp.FragmentFive"};