gpt4 book ai didi

java - Splash Activity 和应用程序关闭后,运行时主 Activity 不会出现

转载 作者:行者123 更新时间:2023-12-02 10:40:22 26 4
gpt4 key购买 nike

我的代码中的所有内容在 .java 和 .xml 文件中看起来都很好(根据软件),因为每次我单击模拟器中的应用程序图标时,我都没有错误,它会显示启动屏幕,然后显示应用程序退出而不显示我的主要 Activity 。到目前为止,这是我的代码:

SplashActivity.java

public class SplashActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//hide title bar of activity
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
//Making activity full screen
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_splash);

int t =3000; //time for spash screen 3000 means 3sec
new Handler().postDelayed(new Runnable() {
@Override
public void run() {

startActivity(new Intent(SplashActivity.this, MainActivity.class));
}
},t);
}

}

SplashActivity.xml

<ImageView
android:layout_width="250dp"
android:layout_height="250dp"
android:src="@drawable/icon"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />

MainActivity.java:

public class MainActivity extends AppCompatActivity
{

String webAdress ="https://www.google.com/";
WebView webView;
FrameLayout frameLayout;
ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {


super.onCreate(savedInstanceState);
webView = (WebView) findViewById(R.id.webView);
frameLayout = (FrameLayout) findViewById(R.id.frameLayout);
progressBar = (ProgressBar) findViewById(R.id.progressBar);

webView.setWebViewClient(new HelpClient());
webView.setWebChromeClient(new WebChromeClient() {
@Override
public void onProgressChanged(WebView view, int newProgress) {
frameLayout.setVisibility(View.VISIBLE);
progressBar.setProgress(newProgress);
setTitle("Loading..."); //when url is loading set this on actionbar
if (newProgress == 100) {
frameLayout.setVisibility(View.GONE); // Hide progress bar when page is loaded
setTitle(view.getTitle()); //get amd set title of opend page
}
super.onProgressChanged(view, newProgress);
}
});

webView.getSettings().setJavaScriptEnabled(true); //enable javaScript

//check internet connection
if (haveNetworkConnection()) {
webView.loadUrl(webAdress);
} else {

Toast.makeText(this, "No internet Connection", Toast.LENGTH_SHORT).show();
}
progressBar.setProgress(0);

}




private class HelpClient extends WebViewClient {

@Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
frameLayout.setVisibility(View.VISIBLE);
return true;
}
}

private boolean haveNetworkConnection(){
boolean haveConnectedWifi = false;
boolean haveConnectedMobile = false;

ConnectivityManager cm = (ConnectivityManager)getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo[] networkInfos = cm.getAllNetworkInfo();

for(NetworkInfo ni : networkInfos){
if(ni.getTypeName().equalsIgnoreCase("WIFI"))
if (ni.isConnected())
haveConnectedWifi = true;

if(ni.getTypeName().equalsIgnoreCase("MOBILE"))
if (ni.isConnected())
haveConnectedMobile = true;


}
return haveConnectedWifi || haveConnectedMobile;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
//check if the event was the back button and if there's history
if ((keyCode==KeyEvent.KEYCODE_BACK) && webView.canGoBack()){
webView.goBack();
return true;
}

//if it was the back or there is no web page history, bubble up to the default system behaviour
return super.onKeyDown(keyCode, event);
}

}

MainActivity.xml:

<!--Progressbar-->
<FrameLayout
android:id="@+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="3dip"
android:background="@android:color/transparent">
<ProgressBar
android:id="@+id/progressBar"
android:layout_width="match_parent"
style="?android:attr/progressBarStyleHorizontal"
android:layout_height="7dp"
android:layout_gravity="top"
android:layout_marginTop="-3dp"
android:progressDrawable="@drawable/custom_progress"
android:background="@android:color/transparent"
android:progress="20"/>
</FrameLayout>

<!--WebView-->
<WebView
android:id="@+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</WebView>

这是我的 list .xml:

<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<!--internet permision-->
<uses-permission android:name="android.permission.INTERNET"/>

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SplashActivity"
android:theme="@style/Theme.AppCompat.Light.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity">

</activity>

</application>

如有任何提示,我们将不胜感激,谢谢!

最佳答案

问题出在MainActivityonCreate方法中,您没有提供任何UI(xml布局文件),导致应用程序找不到要初始化的 View 元素、设置监听器等。我猜你可以在应用程序关闭时在 logcat 中找到异常。

解决方案:为MainActivity提供布局xml文件

public class MainActivity extends AppCompatActivity {

String webAdress ="https://www.google.com/";
WebView webView;
FrameLayout frameLayout;
ProgressBar progressBar;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); // Add this line

webView = (WebView) findViewById(R.id.webView);
...
}
}

关于java - Splash Activity 和应用程序关闭后,运行时主 Activity 不会出现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52957970/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com