输入事件应用问题

时间:2015-04-09 20:14:28

标签: java android

我正在使用输入事件应用。该应用程序有一个文本字段和一个按钮。输入的信息将发送到接收器应用程序,在那里它处理操作,然后我们必须能够写入主活动中的文本视图。希望这个解释不会太混乱。

这是我构建主应用程序的地方:http://developer.android.com/guide/topics/ui/ui-events.html

这是我构建接收器的地方: http://developer.android.com/training/sharing/receive.html

主要应用加载但布局为空白。接收器在负载时崩溃。我无法确切知道原因,因此非常感谢一些帮助。

MainActivity.java(主应用)

package com.miller.main;

import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState, Bundle savedValues) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

     // Capture our button from layout
        final Button button = (Button)findViewById(R.id.corky);
        OnClickListener mCorkyListener = null;
        // Register the onClick listener with the implementation above
        button.setOnClickListener(mCorkyListener);

     // Create an anonymous implementation of OnClickListener
        @SuppressWarnings("unused") OnClickListener mCorkyListener1 = new OnClickListener() {
            @SuppressWarnings("null")
            public void onClick(View v) {
              // do something when the button is clicked
                // Capture our button from layout
                button.setOnClickListener(this);
                Intent sendIntent = new Intent();
                sendIntent.setAction(Intent.ACTION_SEND);
                sendIntent.putExtra(Intent.EXTRA_TEXT, "This is my text to send.");
                sendIntent.setType("text/plain");
                PackageManager packageManager = getPackageManager();
                Intent intent = null;
                List<ResolveInfo> activities = packageManager.queryIntentActivities(intent, 0);
                boolean isIntentSafe = activities.size() > 0;
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
                startActivity(sendIntent);
            }
        };
    }
    @Override
    protected void onPause() {
        Log.w("Monday", "Paused");
        super.onPause();
        Log.w("Monday", "Paused");
        // Another activity is taking focus (this activity is about to be "paused").
    }
    @Override
    protected void onStop() {
        Log.w("Tuesday", "Stopped");
        super.onStop();
        Log.w("Tuesday", "Stopped");
        // The activity is no longer visible (it is now "stopped")
    }
}

activity_main.xml(主应用)

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.miller.main.MainActivity" >

    <EditText
        android:id="@+id/text_field"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:hint="@string/text_field"
        android:inputType="text" />

    <Button
        android:id="@+id/corky"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_text"
        android:layout_below="@+id/text_field" />

</RelativeLayout>

MainActivity.java(接收方应用)

package com.miller.receiver;

import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 // Get intent, action and MIME type
    Intent intent = getIntent();
    String action = intent.getAction();
    String type = intent.getType(); {

    if (Intent.ACTION_SEND.equals(action) && type != null) {
        if ("text/plain".equals(type)) {
            handleSendText(intent); // Handle text being sent
        } else if (type.startsWith("image/")) {
            handleSendImage(intent); // Handle single image being sent
        }
    } else if (Intent.ACTION_SEND_MULTIPLE.equals(action) && type != null) {
        if (type.startsWith("image/")) {
            handleSendMultipleImages(intent); // Handle multiple images being sent
        }
    } else {
        // Handle other intents, such as being started from the home screen
    }
}

void handleSendText(Intent intent) {
    String sharedText = intent.getStringExtra(Intent.EXTRA_TEXT);
    if (sharedText != null) {
        // Update UI to reflect text being shared
    }
}

void handleSendImage(Intent intent) {
    Uri imageUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
    if (imageUri != null) {
        // Update UI to reflect image being shared
    }
}

void handleSendMultipleImages(Intent intent) {
    ArrayList<Uri> imageUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
    if (imageUris != null) {
        // Update UI to reflect multiple images being shared
    }
}
}
接收器应用程序的

activity_main.xml目前只是默认值。

Logcat for Receiver app

04-09 15:41:11.244: E/AndroidRuntime(1114): FATAL EXCEPTION: main
04-09 15:41:11.244: E/AndroidRuntime(1114): Process: com.miller.receiver, PID: 1114
04-09 15:41:11.244: E/AndroidRuntime(1114): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.miller.receiver/com.miller.receiver.MainActivity}: java.lang.NullPointerException
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.os.Handler.dispatchMessage(Handler.java:102)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.os.Looper.loop(Looper.java:136)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.app.ActivityThread.main(ActivityThread.java:5017)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at java.lang.reflect.Method.invokeNative(Native Method)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at java.lang.reflect.Method.invoke(Method.java:515)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at dalvik.system.NativeStart.main(Native Method)
04-09 15:41:11.244: E/AndroidRuntime(1114): Caused by: java.lang.NullPointerException
04-09 15:41:11.244: E/AndroidRuntime(1114):     at com.miller.receiver.MainActivity.<init>(MainActivity.java:20)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at java.lang.Class.newInstanceImpl(Native Method)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at java.lang.Class.newInstance(Class.java:1208)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
04-09 15:41:11.244: E/AndroidRuntime(1114):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
04-09 15:41:11.244: E/AndroidRuntime(1114):     ... 11 more

0 个答案:

没有答案