* 更新:我不知道为什么,但删除一些未使用的导入显然修复了它。*
在较旧的 android 版本(例如 2.2)上启动我的应用程序时,我确实收到以下错误日志...
但我在较新的 Android 版本上没有任何错误。
"at my.package.MainActivity.onCreate(MainActivity.java:12)"
是我的 activity_main.xml 被加载的地方,所以我认为我的有问题
<fragment>
?
日志猫
09-06 14:18:47.037: E/AndroidRuntime(5887): FATAL EXCEPTION: main
09-06 14:18:47.037: E/AndroidRuntime(5887): java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.MainActivity}: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1768)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.os.Handler.dispatchMessage(Handler.java:99)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.os.Looper.loop(Looper.java:130)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.app.ActivityThread.main(ActivityThread.java:3835)
09-06 14:18:47.037: E/AndroidRuntime(5887): at java.lang.reflect.Method.invokeNative(Native Method)
09-06 14:18:47.037: E/AndroidRuntime(5887): at java.lang.reflect.Method.invoke(Method.java:507)
09-06 14:18:47.037: E/AndroidRuntime(5887): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
09-06 14:18:47.037: E/AndroidRuntime(5887): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
09-06 14:18:47.037: E/AndroidRuntime(5887): at dalvik.system.NativeStart.main(Native Method)
09-06 14:18:47.037: E/AndroidRuntime(5887): Caused by: java.lang.UnsupportedOperationException: Can't convert to dimension: type=0x2
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:463)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.view.ViewGroup$MarginLayoutParams.<init>(ViewGroup.java:3692)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.widget.LinearLayout$LayoutParams.<init>(LinearLayout.java:1400)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:1326)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.widget.LinearLayout.generateLayoutParams(LinearLayout.java:47)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.view.LayoutInflater.rInflate(LayoutInflater.java:625)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.view.LayoutInflater.inflate(LayoutInflater.java:408)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
09-06 14:18:47.037: E/AndroidRuntime(5887): at com.actionbarsherlock.internal.ActionBarSherlockCompat.setContentView(ActionBarSherlockCompat.java:857)
09-06 14:18:47.037: E/AndroidRuntime(5887): at com.actionbarsherlock.app.SherlockFragmentActivity.setContentView(SherlockFragmentActivity.java:251)
09-06 14:18:47.037: E/AndroidRuntime(5887): at my.package.MainActivity.onCreate(MainActivity.java:12)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
09-06 14:18:47.037: E/AndroidRuntime(5887): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
09-06 14:18:47.037: E/AndroidRuntime(5887): ... 11 more
MainActivity.java
package my.package;
import android.os.Bundle;
import com.actionbarsherlock.app.SherlockFragmentActivity;
public class MainActivity extends SherlockFragmentActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="@+id/mainFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="my.package.MainFragment" />
</LinearLayout>
MainFragment.java
package my.package;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.content.Intent;
import com.actionbarsherlock.app.SherlockFragment;
public class MainFragment extends SherlockFragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main, container, false);
Button button = (Button) view.findViewById(R.id.bmibutton1);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
updateDetail();
}
});
return view;
}
// May also be triggered from the Activity
public void updateDetail() {
BmiFragment fragment = (BmiFragment) getFragmentManager()
.findFragmentById(R.id.bmiFragment);
if (fragment != null && fragment.isInLayout()) {
} else {
Intent intent = new Intent(getActivity().getApplicationContext(),
BmiActivity.class);
startActivity(intent);
}
}
}
fragment_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="@+id/bmibutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="BMI" />
<Button
android:id="@+id/whtrbutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/bmibutton1"
android:layout_below="@+id/bmibutton1"
android:text="WHtR" />
</RelativeLayout>
AndroidManifest.xml
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.package"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="7"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/icon"
android:label="@string/app_name"
android:theme="@style/Theme.Sherlock.Light.DarkActionBar" >
<activity android:name=".MainActivity" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".BmiActivity"
android:label="@string/title_activity_bmi" >
</activity>
</application>
</manifest>