我有一个可以从其他活动中调用的NetworkErrorActivity。
我已经编写了下面的代码来查找我上次调用的活动。 它将它存储在String调用者中。
除此之外,我不确定如何继续。
我尝试过这样的事情:
callerClass = Class.forName(caller);
但它在Class的第一行中引发了一个错误 - " Class是原始类型。对泛型类的引用应该参数化"
以下是代码。
import android.support.v7.app.ActionBarActivity;
import android.content.Context;
import android.content.Intent;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
public class NetworkErrorActivity extends ActionBarActivity {
Class callerClass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_network_error);
String caller = getIntent().getStringExtra("caller");
try {
callerClass = Class.forName(caller);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/** Called when the user clicks the Retry button */
public void retryURL(View v) {
if(isNetworkStatusAvailable (getApplicationContext())) {
Intent intent = new Intent(this, callerClass);
this.startActivity(intent);
finish();
} else {
Toast.makeText(getApplicationContext(), "Internet Connection Unavailable.", Toast.LENGTH_SHORT).show();
}
}
public static boolean isNetworkStatusAvailable (Context context) {
ConnectivityManager connectivityManager = (ConnectivityManager) context.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivityManager != null)
{
NetworkInfo netInfos = connectivityManager.getActiveNetworkInfo();
if(netInfos != null)
if(netInfos.isConnected())
return true;
}
return false;
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.network_error, menu);
return true;
}
/*
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
*/
@Override
public void onBackPressed()
{
finish();
// super.onBackPressed(); // Comment this super call to avoid calling finish()
}
}