Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java Android中的NullPointerException错误:无法实例化_Java_Android_Android Activity_Nullpointerexception - Fatal编程技术网

Java Android中的NullPointerException错误:无法实例化

Java Android中的NullPointerException错误:无法实例化,java,android,android-activity,nullpointerexception,Java,Android,Android Activity,Nullpointerexception,大家好 我在开发android应用程序时遇到了一个很难纠正的错误。我试图在模拟器中运行,但它崩溃并给出nullpointerexception错误。然后,我创建了一个新项目,从零开始将这些文件复制到一个新项目中,但仍然存在相同的错误。然后,我将应用程序的一部分制作成一个新项目,但它仍然会给出相同的错误。 错误与启动我正在使用的活动有关。 下面是必要的文件,我希望有人能指导我正确的方向,告诉我我犯了什么错误非常感谢 DISPLAYPOINTS.JAVA package com.example.sa

大家好

我在开发android应用程序时遇到了一个很难纠正的错误。我试图在模拟器中运行,但它崩溃并给出nullpointerexception错误。然后,我创建了一个新项目,从零开始将这些文件复制到一个新项目中,但仍然存在相同的错误。然后,我将应用程序的一部分制作成一个新项目,但它仍然会给出相同的错误。 错误与启动我正在使用的活动有关。 下面是必要的文件,我希望有人能指导我正确的方向,告诉我我犯了什么错误非常感谢

DISPLAYPOINTS.JAVA

package com.example.safedrive;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.json.JSONObject;

import android.text.Html;
import android.app.Activity;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

public class DisplayPoints extends Activity {

    String name = "Ammaar";
    TextView display_points;
    Button btn_start = (Button) findViewById(R.id.btnStart); //Start Button Variable
    String displayPoints;
    InputStream is=null;
    String result=null;
    String line=null;
    int points;
    int code;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.display_points);
        TextView lblName = (TextView) findViewById(R.id.lblName);
        display_points = (TextView) findViewById(R.id.lblCurrPoints); //POINTS TO DISPLAY IN

        lblName.setText(Html.fromHtml("Welcome <b>" + name + "! </b>"));

        new Insert().execute();

        btn_start.setOnClickListener(new View.OnClickListener() {

            public void onClick(View arg0) {
                //MainScreen.IsStarted=true;
                //SpeedPoints start = new SpeedPoints();
                //Intent i = new Intent(getApplicationContext(), SpeedPoints.class);
                //startActivity(i);
                Toast.makeText(getApplicationContext(), "The app has started monitoring your drive...",
                        Toast.LENGTH_LONG).show();
                 //start.startAnimation();
            }
        });


     }

    class Insert extends AsyncTask<String, Void, String> {
            // Do the long-running work in here
            protected String doInBackground(String... args) {
                ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(1);
                nameValuePairs.add(new BasicNameValuePair("fname", name));

                try
                {
                HttpClient httpclient = new DefaultHttpClient();
                HttpPost httppost = new HttpPost("http://ammar123.net84.net/capstone/displayPoints.php");
                httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
                HttpResponse response = httpclient.execute(httppost); 
                HttpEntity entity = response.getEntity();
                is = entity.getContent();
                Log.e("pass 1", "connection success ");
                }
                catch(Exception e)
                {
                    Log.e("Fail 1", e.toString());
                    Toast.makeText(getApplicationContext(), "Invalid IP Address",
                    Toast.LENGTH_LONG).show();
                }     

                try
                {
                    BufferedReader reader = new BufferedReader
                    (new InputStreamReader(is,"iso-8859-1"),8);
                    StringBuilder sb = new StringBuilder();
                    while ((line = reader.readLine()) != null)
                    {
                        sb.append(line + "\n");
                    }
                    is.close();
                    result = sb.toString();
                Log.e("pass 2", "connection success ");
                }
                catch(Exception e)
                {
                    Log.e("Fail 2", e.toString());
                }     

            try
            {
                    JSONObject json_data = new JSONObject(result);
                    code=(json_data.getInt("code"));
                    points=(json_data.getInt("pointss"));
                    displayPoints = Integer.toString(points);
            }
            catch(Exception e)
            {
                    Log.e("Fail 3", e.toString());
            }

            return displayPoints;

          }//End of doInBackground method

            protected void onPostExecute(String displaypoints) {

                display_points.setText(displayPoints);
                Toast.makeText(getBaseContext(), "Points Retrieved!",
                        Toast.LENGTH_SHORT).show();

        }//End of onPostExecute
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.display_points, 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);
    }
}
package com.example.safedrive;
导入java.io.BufferedReader;
导入java.io.InputStream;
导入java.io.InputStreamReader;
导入java.util.ArrayList;
导入org.apache.http.HttpEntity;
导入org.apache.http.HttpResponse;
导入org.apache.http.NameValuePair;
导入org.apache.http.client.HttpClient;
导入org.apache.http.client.entity.UrlEncodedFormEntity;
导入org.apache.http.client.methods.HttpPost;
导入org.apache.http.impl.client.DefaultHttpClient;
导入org.apache.http.message.BasicNameValuePair;
导入org.json.JSONObject;
导入android.text.Html;
导入android.app.Activity;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.Menu;
导入android.view.MenuItem;
导入android.view.view;
导入android.widget.Button;
导入android.widget.TextView;
导入android.widget.Toast;
公共类DisplayPoints扩展了活动{
String name=“Ammaar”;
文本视图显示点;
按钮btn_start=(按钮)findViewById(R.id.btnStart);//启动按钮变量
字符串显示点;
InputStream=null;
字符串结果=null;
字符串行=null;
积分;
int代码;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(右布局显示点);
TextView lblName=(TextView)findViewById(R.id.lblName);
display_points=(TextView)findViewById(R.id.lblCurrPoints);//要在中显示的点
lblName.setText(Html.fromHtml(“欢迎”+name+“!”));
新的Insert().execute();
btn_start.setOnClickListener(新视图.OnClickListener(){
公共void onClick(视图arg0){
//MainScreen.IsStarted=true;
//速度点开始=新速度点();
//Intent i=新Intent(getApplicationContext(),SpeedPoints.class);
//星触觉(i);
Toast.makeText(getApplicationContext(),“应用程序已开始监视您的驱动器…”,
Toast.LENGTH_LONG).show();
//start.startAnimation();
}
});
}
类插入扩展异步任务{
//在这里做长期的工作
受保护的字符串doInBackground(字符串…args){
ArrayList nameValuePairs=新的ArrayList(1);
添加(新的BasicNameValuePair(“fname”,name));
尝试
{
HttpClient HttpClient=新的DefaultHttpClient();
HttpPost HttpPost=新的HttpPost(“http://ammar123.net84.net/capstone/displayPoints.php");
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
HttpResponse response=httpclient.execute(httppost);
HttpEntity=response.getEntity();
is=entity.getContent();
Log.e(“通过1”,“连接成功”);
}
捕获(例外e)
{
Log.e(“Fail 1”,e.toString());
Toast.makeText(getApplicationContext(),“无效IP地址”,
Toast.LENGTH_LONG).show();
}     
尝试
{
BufferedReader reader=新的BufferedReader
(新的InputStreamReader(is,“iso-8859-1”),8);
StringBuilder sb=新的StringBuilder();
而((line=reader.readLine())!=null)
{
sb.追加(第+行“\n”);
}
is.close();
结果=sb.toString();
Log.e(“通过2”,“连接成功”);
}
捕获(例外e)
{
Log.e(“Fail 2”,e.toString());
}     
尝试
{
JSONObject json_data=新的JSONObject(结果);
code=(json_data.getInt(“code”);
points=(json_data.getInt(“pointss”);
displayPoints=整数。toString(点);
}
捕获(例外e)
{
Log.e(“Fail 3”,e.toString());
}
返回显示点;
}//底底法
受保护的void onPostExecute(字符串显示点){
显示点。设置文本(显示点);
Toast.makeText(getBaseContext(),“检索到的点!”,
吐司。长度(短)。show();
}//onPostExecute的结束
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单显示点,菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
if(id==R.id.action\u设置){
复述
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.safedrive"
    android:versionCode="1"
    android:versionName="1.0" >

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

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

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.safedrive.DisplayPoints"
            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>
06-09 18:52:55.467: ERROR/ResourceType(87): Style contains key with bad entry: 0x01010479
06-09 18:52:56.567: ERROR/AndroidRuntime(771): FATAL EXCEPTION: main
06-09 18:52:56.567: ERROR/AndroidRuntime(771): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.safedrive/com.example.safedrive.DisplayPoints}: java.lang.NullPointerException
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1879)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.os.Looper.loop(Looper.java:137)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.ActivityThread.main(ActivityThread.java:4340)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at java.lang.reflect.Method.invokeNative(Native Method)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at java.lang.reflect.Method.invoke(Method.java:511)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at dalvik.system.NativeStart.main(Native Method)
06-09 18:52:56.567: ERROR/AndroidRuntime(771): Caused by: java.lang.NullPointerException
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.Activity.findViewById(Activity.java:1794)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at com.example.safedrive.DisplayPoints.<init>(DisplayPoints.java:34)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at java.lang.Class.newInstanceImpl(Native Method)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at java.lang.Class.newInstance(Class.java:1319)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1870)
06-09 18:52:56.567: ERROR/AndroidRuntime(771):     ... 11 more
btn_start = (Button) findViewById(R.id.btnStart); //Start Button Variable
setContentView(R.layout.display_points);