简体   繁体   中英

Unable to start activity componentinfo Nullpointerexception

I am totaly new to java and i am trying to create a app that can send UDP (UDP part code from Google) to my visual basic program running on my pc. I dont use the textboxes yet, but its only sending a string of text when i press the button (atleast i want it to do that but i dont even get to that part because of the errors. I already searched almost whole google xD but i cant find anything that helps me (atleast what i understand). I know this is a question thats already asked so much times but i cant get wiser out of them sorry:P

Kere is the logcat list:

08-10 22:02:22.519: E/AndroidRuntime(16182): FATAL EXCEPTION: main
08-10 22:02:22.519: E/AndroidRuntime(16182): java.lang.RuntimeException: Unable to         start activity ComponentInfo{com.example.keetdoaldiek/com.example.keetdoaldiek.Keet}:                          java.lang.NullPointerException
08-10 22:02:22.519: E/AndroidRuntime(16182):    at    android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2308)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2358)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.app.ActivityThread.access$600(ActivityThread.java:153)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.os.Handler.dispatchMessage(Handler.java:99)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.os.Looper.loop(Looper.java:137)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.app.ActivityThread.main(ActivityThread.java:5260)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  java.lang.reflect.Method.invokeNative(Native Method)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  java.lang.reflect.Method.invoke(Method.java:511)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:795)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  com.android.internal.os.ZygoteInit.main(ZygoteInit.java:562)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  dalvik.system.NativeStart.main(Native Method)
08-10 22:02:22.519: E/AndroidRuntime(16182): Caused by: java.lang.NullPointerException
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  com.example.keetdoaldiek.Keet.senbutton(Keet.java:39)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  com.example.keetdoaldiek.Keet.onCreate(Keet.java:28)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.app.Activity.performCreate(Activity.java:5104)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at  android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
08-10 22:02:22.519: E/AndroidRuntime(16182):    at       android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2262)
08-10 22:02:22.519: E/AndroidRuntime(16182):    ... 11 more

This is my main.java

package com.example.keetdoaldiek;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;


import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;





public class Keet extends ActionBarActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_keet);

    sendbutton();

}





private void sendbutton() {
    Button messagebutton = (Button)findViewById(android.R.id.button1);

    messagebutton.setOnClickListener (new View.OnClickListener() {


        @Override
        public  void onClick(View v) {

             String messageStr = "Hello Android!";
                int server_port = 54372;
                try
                {
                    DatagramSocket s = new DatagramSocket();
                    InetAddress local = null;
                    local = InetAddress.getLocalHost();
                    int msg_length = messageStr.length();
                    byte[] message = messageStr.getBytes();
                    DatagramPacket p = new DatagramPacket(message, msg_length, local, server_port);
                    s.send(p);
                    s.close();
                    android.util.Log.w("UDP", "Works fine!");

                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
                catch (Exception e)
                {
                    android.util.Log.w("UDP", "Catched here.");
                e.printStackTrace();

                }



        }
    });
}





@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.keet, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

}

and this my manifest.

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

<uses-sdk
    android:minSdkVersion="8"
    android:targetSdkVersion="21" />

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >

    <activity
        android:name="Keet"
        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>

I think its a variable that is nothing? but i don't know whitch one. i hope you guys can help me out with my problem.

Thanks,

You do not have the activity Keet declared correctly in the manifest. You can use either com.example.keetdoaldiek.Keet or .Keet as the android:name as follows:

<activity
    android:name="com.example.keetdoaldiek.Keet"
    android:label="@string/app_name" >
    <intent-filter>
        <action android:name="android.intent.action.MAIN" />
        <category android:name="android.intent.category.LAUNCHER" />
    </intent-filter>
</activity>

As per the official android documentation :

android:name The name of the class that implements the activity, a subclass of Activity. The attribute value should be a fully qualified class name (such as, "com.example.project.ExtracurricularActivity"). However, as a shorthand, if the first character of the name is a period (for example, ".ExtracurricularActivity"), it is appended to the package name specified in the element . Once you publish your application, you should not change this name (unless you've set android:exported="false").

There is no default. The name must be specified.

In your function sendbutton() change

Button messagebutton = (Button)findViewById(android.R.id.button1);

to

Button messagebutton = (Button)findViewById(R.id.button1);

and use

com.example.keetdoaldiek.R

This is because android.R is for general Android recources and com.example.keetdoaldiek.R is the generated index for your app.

Change

Button messagebutton = (Button)findViewById(android.R.id.button1);

to

Button messagebutton = (Button)findViewById(R.id.button1);

Second problem is your manifest is missing declaration to use internet which is required in this case: Put the below outside the application tag in the manifest.

<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM