0

我正在尝试在我的应用程序中使用带有通知的远程视图。但是当我运行我的应用程序时,我得到了E/AndroidRuntime(11337): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.jeema.HWDTest/net.jeema.HWDTest.HWDTestActivity}: java.lang.NullPointerException。不知道为什么会发生。我是 android 新手。请帮助我。无法提示为什么我在这里遇到运行时异常。

我的代码:

public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
        final RemoteViews contentView = new RemoteViews(getPackageName(),
                R.layout.upload_progress_bar);
        contentView.setProgressBar(R.id.progress_bar, MAX_PROGRESS,
                mProgressStatus, false);
        notification=new Notification();
        notification.contentView = contentView;

        // Start file upload in a background thread
        new Thread(new Runnable() {
            public void run() {
                while (mProgressStatus < MAX_PROGRESS) {
                    mProgressStatus = doWork();

                    // Update the progress bar
                    mHandler.post(new Runnable() {
                        public void run() {
                            contentView.setProgressBar(R.id.progress_bar,
                                    MAX_PROGRESS, mProgressStatus, false);
                        }
                    });
                }
            }

            private int doWork() {
                HttpURLConnection conn = null;
                DataOutputStream dos = null;
                DataInputStream inStream = null;
                String lineEnd = "\r\n";
                String twoHyphens = "--";
                String boundary = "*****";
                int bytesRead;
                byte[] buffer;
                String urlString = "http://xxxxx/xxxx/xxx.php";
                try {
                    UUID uniqueKey = UUID.randomUUID();
                    fname = uniqueKey.toString();
                    Log.e("UNIQUE NAME", fname);
                    FileInputStream fileInputStream = new FileInputStream(
                            new File(selectedPath));
                    URL url = new URL(urlString);
                    conn = (HttpURLConnection) url.openConnection();
                    int length = fileInputStream.available();
                    System.out.println("Video Length--->>>>" + length);
                    conn.setDoInput(true);
                    conn.setDoOutput(true);
                    conn.setUseCaches(false);
                    conn.setRequestMethod("POST");
                    conn.setRequestProperty("Connection", "Keep-Alive");
                    conn.setRequestProperty("Content-Type",
                            "multipart/form-data;boundary=" + boundary);
                    dos = new DataOutputStream(conn.getOutputStream());
                    dos.writeBytes(twoHyphens + boundary + lineEnd);
                    dos.writeBytes("Content-Disposition: form-data; name=\"uploadedfile\";filename=\""
                            + fname + "" + lineEnd);
                    dos.writeBytes(lineEnd);
                    buffer = new byte[8192];
                    bytesRead = 0;
                    while ((bytesRead = fileInputStream.read(buffer)) > 0) {
                        dos.write(buffer, 0, bytesRead);
                    }
                    dos.writeBytes(lineEnd);
                    dos.writeBytes(twoHyphens + boundary + twoHyphens + lineEnd);
                    Log.e("Debug", "File is written");
                    fileInputStream.close();
                    dos.flush();
                    dos.close();

                } catch (MalformedURLException ex) {
                    Log.e("Debug", "error: " + ex.getMessage(), ex);
                } catch (IOException ioe) {
                    Log.e("Debug", "error: " + ioe.getMessage(), ioe);
                }
                // ------------------ read the SERVER RESPONSE
                try {
                    inStream = new DataInputStream(conn.getInputStream());
                    String str;
                    while ((str = inStream.readLine()) != null) {
                        Log.e("Debug", "Server Response " + str);
                    }
                    inStream.close();

                } catch (IOException ioex) {
                    Log.e("Debug", "error: " + ioex.getMessage(), ioex);
                }
                return 0;
            }
        }).start();
    }

我的日志猫:

08-26 14:15:49.542: E/AndroidRuntime(12359): FATAL EXCEPTION: main
08-26 14:15:49.542: E/AndroidRuntime(12359): java.lang.RuntimeException: Unable to start activity ComponentInfo{net.jeema.HWDTest/net.jeema.HWDTest.HWDTestActivity}: java.lang.NullPointerException
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.app.ActivityThread.access$1500(ActivityThread.java:117)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.os.Looper.loop(Looper.java:123)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.app.ActivityThread.main(ActivityThread.java:3683)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at java.lang.reflect.Method.invokeNative(Native Method)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at java.lang.reflect.Method.invoke(Method.java:507)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at dalvik.system.NativeStart.main(Native Method)
08-26 14:15:49.542: E/AndroidRuntime(12359): Caused by: java.lang.NullPointerException
08-26 14:15:49.542: E/AndroidRuntime(12359):    at net.jeema.HWDTest.HWDTestActivity.onCreate(HWDTestActivity.java:36)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
08-26 14:15:49.542: E/AndroidRuntime(12359):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
08-26 14:15:49.542: E/AndroidRuntime(12359):    ... 11 more

我的清单.xml:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.jeema.HWDTest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />
    <uses-permission android:name="android.permission.INTERNET" />
    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".HWDTestActivity"
            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>
4

1 回答 1

0

您在创建通知之前正在使用它......试试这种方式:

notification=new Notification();
notification.flags = notification.flags | Notification.FLAG_ONGOING_EVENT;
于 2012-08-26T08:55:28.513 回答