每当我启动我的应用程序时,它都会崩溃。由于我过去在使用 RelativeLayout 时遇到了一些类似的问题,我猜这个问题与此有关。我使用图形界面创建了布局。
java 应用程序和布局都不会返回任何错误或重要警告。
MainActivity.java
package com.XXX.mcgneussapp2;
import android.app.Activity;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.CookieManager;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ProgressBar Pbar = (ProgressBar) findViewById(R.id.pB1);
final TextView loadtext = (TextView) findViewById(R.id.textView1);
final ImageView imgFavorite = (ImageView) findViewById(R.id.imageView2);
final ImageView imgFavorite2 = (ImageView) findViewById(R.id.imageView3);
final ImageView imgFavorite3 = (ImageView) findViewById(R.id.imageView4);
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.setWebViewClient(new WebViewClient()
{
@Override
public boolean shouldOverrideUrlLoading(WebView view, String url)
{
return false;
}
public void onPageStarted(WebView view, String url, Bitmap favicon)
{
view.setVisibility(View.GONE);
Pbar.setVisibility(ProgressBar.VISIBLE);
loadtext.setVisibility(View.VISIBLE);
}
public void onPageFinished (WebView view, String url)
{
view.setVisibility(View.VISIBLE);
Pbar.setVisibility(ProgressBar.GONE);
loadtext.setVisibility(View.GONE);
}
});
imgFavorite.setClickable(true);
imgFavorite.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) //favoriten
{
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("https://www.free-way.me/mcgapp/recommend.php");
imgFavorite3.setVisibility(View.VISIBLE);
imgFavorite2.setVisibility(View.VISIBLE);
}
});
imgFavorite2.setClickable(true);
imgFavorite2.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) //weiter
{
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("https://www.free-way.me/mcgapp/index.php?siteid=1&nohead");
imgFavorite3.setVisibility(View.VISIBLE);
imgFavorite2.setVisibility(View.GONE);
}
});
imgFavorite3.setClickable(true);
imgFavorite3.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) //zurück
{
WebView engine = (WebView) findViewById(R.id.web_engine);
engine.loadUrl("https://www.free-way.me/mcgapp/index.php?nohead");
imgFavorite3.setVisibility(View.GONE);
imgFavorite2.setVisibility(View.VISIBLE);
}
});
engine.getSettings().setJavaScriptEnabled(true);
CookieManager cookieManager = CookieManager.getInstance();
cookieManager.setAcceptCookie(true);
engine.loadUrl("https://www.free-way.me/mcgapp/index.php?nohead");
imgFavorite3.setVisibility(View.GONE);
}
}
activity_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"
tools:context=".MainActivity" >
<ImageView
android:id="@+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="@android:drawable/bottom_bar" />
<ImageView
android:id="@+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignTop="@+id/imageView5"
android:layout_centerHorizontal="true"
android:clickable="true"
android:src="@android:drawable/ic_menu_myplaces" />
<ImageView
android:id="@+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/imageView5"
android:layout_marginLeft="14dp"
android:clickable="true"
android:src="@android:drawable/ic_media_previous" />
<ImageView
android:id="@+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="@+id/imageView5"
android:layout_marginRight="14dp"
android:clickable="true"
android:src="@android:drawable/ic_media_next" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/pB1"
android:layout_centerHorizontal="true"
android:layout_marginBottom="24dp"
android:text="Bitte warten"
android:textAppearance="?android:attr/textAppearanceMedium"
tools:ignore="HardcodedText" />
<ProgressBar
android:id="@+id/pB1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<WebView
android:id="@+id/web_engine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="@+id/imageView5"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
LogCat 转储
> 02-12 11:45:32.795: D/AndroidRuntime(309): Shutting down VM 02-12
> 11:45:32.795: W/dalvikvm(309): threadid=1: thread exiting with
> uncaught exception (group=0x4001d800) 02-12 11:45:32.805:
> E/AndroidRuntime(309): FATAL EXCEPTION: main 02-12 11:45:32.805:
> E/AndroidRuntime(309): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.XXX.mcgneussapp2/com.XXX.mcgneussapp2.MainActivity}:
> java.lang.ClassCastException: android.widget.ImageView 02-12
> 11:45:32.805: E/AndroidRuntime(309): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
> 02-12 11:45:32.805: E/AndroidRuntime(309): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
> 02-12 11:45:32.805: E/AndroidRuntime(309): at
> android.app.ActivityThread.access$2300(ActivityThread.java:125) 02-12
> 11:45:32.805: E/AndroidRuntime(309): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
> 02-12 11:45:32.805: E/AndroidRuntime(309): at
> android.os.Handler.dispatchMessage(Handler.java:99) 02-12
> 11:45:32.805: E/AndroidRuntime(309): at
> android.os.Looper.loop(Looper.java:123) 02-12 11:45:32.805:
> E/AndroidRuntime(309): at
> android.app.ActivityThread.main(ActivityThread.java:4627) 02-12
> 11:45:32.805: E/AndroidRuntime(309): at
> java.lang.reflect.Method.invokeNative(Native Method) 02-12
> 11:45:32.805: E/AndroidRuntime(309): at
> java.lang.reflect.Method.invoke(Method.java:521) 02-12 11:45:32.805:
> E/AndroidRuntime(309): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
> 02-12 11:45:32.805: E/AndroidRuntime(309): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-12
> 11:45:32.805: E/AndroidRuntime(309): at
> dalvik.system.NativeStart.main(Native Method) 02-12 11:45:32.805:
> E/AndroidRuntime(309): Caused by: java.lang.ClassCastException:
> android.widget.ImageView 02-12 11:45:32.805: E/AndroidRuntime(309):
> at
> com.XXX.mcgneussapp2.MainActivity.onCreate(MainActivity.java:24)
> 02-12 11:45:32.805: E/AndroidRuntime(309): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 02-12 11:45:32.805: E/AndroidRuntime(309): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
> 02-12 11:45:32.805: E/AndroidRuntime(309): ... 11 more 02-12
> 11:45:35.784: I/Process(309): Sending signal. PID: 309 SIG: 9 02-12
> 11:46:26.815: D/AndroidRuntime(343): Shutting down VM 02-12
> 11:46:26.815: W/dalvikvm(343): threadid=1: thread exiting with
> uncaught exception (group=0x4001d800) 02-12 11:46:26.825:
> E/AndroidRuntime(343): FATAL EXCEPTION: main 02-12 11:46:26.825:
> E/AndroidRuntime(343): java.lang.RuntimeException: Unable to start
> activity
> ComponentInfo{com.XXX.mcgneussapp2/com.XXX.mcgneussapp2.MainActivity}:
> java.lang.ClassCastException: android.widget.ImageView 02-12
> 11:46:26.825: E/AndroidRuntime(343): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
> 02-12 11:46:26.825: E/AndroidRuntime(343): at
> android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
> 02-12 11:46:26.825: E/AndroidRuntime(343): at
> android.app.ActivityThread.access$2300(ActivityThread.java:125) 02-12
> 11:46:26.825: E/AndroidRuntime(343): at
> android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
> 02-12 11:46:26.825: E/AndroidRuntime(343): at
> android.os.Handler.dispatchMessage(Handler.java:99) 02-12
> 11:46:26.825: E/AndroidRuntime(343): at
> android.os.Looper.loop(Looper.java:123) 02-12 11:46:26.825:
> E/AndroidRuntime(343): at
> android.app.ActivityThread.main(ActivityThread.java:4627) 02-12
> 11:46:26.825: E/AndroidRuntime(343): at
> java.lang.reflect.Method.invokeNative(Native Method) 02-12
> 11:46:26.825: E/AndroidRuntime(343): at
> java.lang.reflect.Method.invoke(Method.java:521) 02-12 11:46:26.825:
> E/AndroidRuntime(343): at
> com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
> 02-12 11:46:26.825: E/AndroidRuntime(343): at
> com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-12
> 11:46:26.825: E/AndroidRuntime(343): at
> dalvik.system.NativeStart.main(Native Method) 02-12 11:46:26.825:
> E/AndroidRuntime(343): Caused by: java.lang.ClassCastException:
> android.widget.ImageView 02-12 11:46:26.825: E/AndroidRuntime(343):
> at
> com.XXX.mcgneussapp2.MainActivity.onCreate(MainActivity.java:24)
> 02-12 11:46:26.825: E/AndroidRuntime(343): at
> android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
> 02-12 11:46:26.825: E/AndroidRuntime(343): at
> android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
> 02-12 11:46:26.825: E/AndroidRuntime(343): ... 11 more
//编辑: AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.XXX.mcgneussapp2"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="17" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name="com.XXX.mcgneussapp2.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>
</application>
</manifest>