1

我的应用程序在启动时崩溃。我不知道为什么。我认为我做对了。而且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);
}

}

日志猫

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); *它给了我同样的错误*

编辑:在我全部编辑之后。它仍然崩溃。我发布了完整的代码。

4

4 回答 4

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 );
} 
于 2013-06-24T12:48:57.477 回答
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");

} 
于 2013-06-24T12:47:04.770 回答
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");

      } 
于 2013-06-24T12:46:14.963 回答
0
Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.getResources(ContextWrapper.java:81)

这表明您试图过早地访问您的资源,例如在初始化您的成员变量时。仅在活动生命周期中调用getResources()onCreate()稍后调用。

修复后,您会发现这mWebViewnull其他人建议的。setContentView()在调用findViewById()以在内容视图层次结构中查找组件之前,您需要先进行操作。

于 2013-06-24T13:08:37.380 回答