我的应用在启动时崩溃了。我不知道为什么。我想我做得对。而eclipse并没有显示错误,
MainActivity.java (编辑)的
package org.flikkema.robin.dagrooster;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.os.Bundle;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.webkit.WebView;
import android.widget.Toast;
@SuppressLint("SimpleDateFormat")
public class MainActivity extends Activity {
// Declare as global variable
WebView mWebview;
public boolean onCreateOptionsMenu(Menu menu){
// Make MenuInflater
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
inflater.inflate(R.menu.main, menu);
// Return True
return true;
}
@SuppressLint("SetJavaScriptEnabled")
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Toast.makeText(this, "Laden van roosterwijzigingen.",
Toast.LENGTH_SHORT).show();
mWebview =(WebView) findViewById(R.id.webView1);
// Enable JavaScript
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
}
public boolean onOptionsItemSelected(MenuItem item) {
mWebview =(WebView) findViewById(R.id.webView1);
switch (item.getItemId()) {
// If button 1 is pressed
case R.id.about:
// Toast Message
Toast.makeText(this, "App made by Robin Flikkema & Sander ten Hove",
Toast.LENGTH_LONG).show();
break;
// If Share is pressed
case R.id.menu_share:
// Toast message
Toast.makeText(this, "Roosterwijzigingen delen",
Toast.LENGTH_SHORT).show();
// Making share selector
String shareBody = "Er vervallen nieuwe lessen! Check het uit, met de Eemsdeltacollege Roosterwijzigingen App.";
Intent sharingIntent = new Intent(android.content.Intent.ACTION_SEND);
sharingIntent.setType("text/plain");
sharingIntent.putExtra(android.content.Intent.EXTRA_SUBJECT, "Eemsdeltacollege Roosterwijzigingen");
sharingIntent.putExtra(android.content.Intent.EXTRA_TEXT, shareBody);
startActivity(Intent.createChooser(sharingIntent, getResources().getString(R.string.share_using)));
break;
// If Next is pressed
case R.id.menu_next:
// Toast message
Toast.makeText(this, "Morgen",
Toast.LENGTH_SHORT).show();
// Calculate Date
Calendar cal=Calendar.getInstance();
int currentDay=cal.get(Calendar.DAY_OF_MONTH);
//Set the date to 1 day
cal.set(Calendar.DAY_OF_MONTH, currentDay+1);
while (cal.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
cal.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
cal.add(Calendar.DATE, 1);
}
Date DateDate = cal.getTime();
SimpleDateFormat postFormater = new SimpleDateFormat("dd-M-yyyy");
String newDateStr = postFormater.format(DateDate);
// Set URL
mWebview .loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php?datum="+newDateStr);
setContentView(mWebview );
break;
// If Refresh is pressed
case R.id.menu_refresh:
// Toast message
Toast.makeText(this, "Roosterwijzigingen ophalen.",
Toast.LENGTH_SHORT).show();
// Load URL
mWebview .loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
setContentView(mWebview );
break;
// If Overmorgen is pressed
case R.id.menu_nextt:
// Toast message
Toast.makeText(this, "Overmorgen",
Toast.LENGTH_SHORT).show();
// Calculate Date
Calendar cal1=Calendar.getInstance();
int currentDay1=cal1.get(Calendar.DAY_OF_MONTH);
//Set the date to 1 day
cal1.set(Calendar.DAY_OF_MONTH, currentDay1+2);
while (cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SATURDAY ||
cal1.get(Calendar.DAY_OF_WEEK) == Calendar.SUNDAY) {
cal1.add(Calendar.DATE, 1);
}
Date DateDate1 = cal1.getTime();
SimpleDateFormat postFormater1 = new SimpleDateFormat("dd-M-yyyy");
String newDateStr1 = postFormater1.format(DateDate1);
// Set URL
mWebview .loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php?datum="+newDateStr1);
setContentView(mWebview );
break;
// If Help is pressed
case R.id.action_settings:
// Toast message
Toast.makeText(this, "Laden van de Help pagina.",
Toast.LENGTH_SHORT).show();
// Load Help
mWebview .loadUrl("https://dl.dropboxusercontent.com/u/53488707/RoosterwijzigingenApp/index.html");
setContentView(mWebview );
break;
// If Vandaag is pressed
case R.id.menu_back:
// Toast message
Toast.makeText(this, "Vandaag",
Toast.LENGTH_SHORT).show();
// Load Help
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
setContentView(mWebview);
break;
}
return super.onOptionsItemSelected(item);
}
}
logcat的
06-24 14:41:52.079: E/AndroidRuntime(5986): FATAL EXCEPTION: main
06-24 14:41:52.079: E/AndroidRuntime(5986): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.flikkema.robin.dagrooster/org.flikkema.robin.dagrooster.MainActivity}: java.lang.NullPointerException
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1983)
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.app.ActivityThread.access$600(ActivityThread.java:130)
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.os.Looper.loop(Looper.java:137)
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.app.ActivityThread.main(ActivityThread.java:4745)
06-24 14:41:52.079: E/AndroidRuntime(5986): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 14:41:52.079: E/AndroidRuntime(5986): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 14:41:52.079: E/AndroidRuntime(5986): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
06-24 14:41:52.079: E/AndroidRuntime(5986): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
06-24 14:41:52.079: E/AndroidRuntime(5986): at dalvik.system.NativeStart.main(Native Method)
06-24 14:41:52.079: E/AndroidRuntime(5986): Caused by: java.lang.NullPointerException
06-24 14:41:52.079: E/AndroidRuntime(5986): at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
编辑:我添加后:WebView mWebview =(WebView)findViewById(R.id.webView1); *它给了我同样的错误*
编辑: 在我编辑完所有内容之后。它仍然崩溃。我发布了完整的代码。
答案 0 :(得分:1)
您尚未初始化WebView
。
WebView mWebview ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mWebview = new WebView(this);
Toast.makeText(this, "Laden van roosterwijzigingen.",
Toast.LENGTH_SHORT).show();
// Enable JavaScript
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
setContentView(mWebview );
}
答案 1 :(得分:0)
很简单,你没有初始化mWebview
使用这种方式;
WebView mWebview =(WebView) findviewById(R.id.webview_id);
修改强>
我认为你必须这样做
// Declare as global variable
WebView mWebview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayoutfile);
Toast.makeText(this, "Laden van roosterwijzigingen.",
Toast.LENGTH_SHORT).show();
mWebview =(WebView) findviewById(R.id.webview_id);
// Enable JavaScript
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
}
答案 2 :(得分:0)
mWebview
可能null
。
你必须绑定它:
mWebView = (WebView) findViewById(R.id.YourWebView);
或发起它:
mWebView = new WebView(this);
我认为你的后缀m代表“成员”。所以在onCreate之外声明它并在onCreate方法中绑定或启动它。
更新:
private WebView mWebview;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mylayoutfile);
Toast.makeText(this, "Laden van roosterwijzigingen.",
Toast.LENGTH_SHORT).show();
mWebview =(WebView) findviewById(R.id.webview_id);
// Enable JavaScript
mWebview.getSettings().setJavaScriptEnabled(true); // enable javascript
mWebview.loadUrl("http://divers.ommelandercollege.nl/webportalen/dagrooster.php");
}
答案 3 :(得分:0)
Caused by: java.lang.NullPointerException
at android.content.ContextWrapper.getResources(ContextWrapper.java:81)
这表明您过早地尝试访问资源,例如在初始化成员变量时。只能在getResources()
或之后的活动生命周期中调用onCreate()
。
修正后,您会发现其他人建议mWebView
为null
。在调用setContentView()
之前,您需要先findViewById()
才能在内容视图层次结构中查找组件。