创建并使用java类时出现Android错误

创建并使用java类时出现Android错误,java,android,Java,Android,我正在尝试用android做一个简单的应用程序,用一个新的类来学习如何使用它。主要活动有: package com.josejoaquin.testhttp; import android.support.v7.app.ActionBarActivity; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import

我正在尝试用android做一个简单的应用程序,用一个新的类来学习如何使用它。主要活动有:

package com.josejoaquin.testhttp;

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;

import android.widget.TextView;

public class MyActivity extends ActionBarActivity {



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

    Button Boton = (Button)findViewById(R.id.button);
    TextView Texto = (TextView)findViewById(R.id.textView);

    Boton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            clientehttp clienteweb = null;
            String total;
            total = clienteweb.getWeb();
            Texto.setText(total);

        }
    });


}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.my, 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);
}
}
另一个文件名为clientehtt.java,代码如下:

package com.josejoaquin.testhttp;


public class clientehttp {

   public String getWeb (){

    String texto ="Hola Mundo";
    return texto;


}
}
manifiest文件具有:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.josejoaquin.testhttp" >

<application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" >
    <activity
        android:name=".MyActivity"
        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>

但是当我点击应用程序关闭时,我得到了一个错误,我做错了什么?有人能帮我了解更多吗


致以最诚挚的问候。

我想您会收到一个
NullPointerException

问题出在
onClick()方法中:

        clientehttp clienteweb = null;
        String total;
        total = clienteweb.getWeb();
您正在将
clienteweb
的值设置为
null
,但是您正在尝试使用它的方法
clienteweb.getWeb()
,您不能这样做

将代码更改为:

        clientehttp clienteweb = new clientehttp();
        String total;
        total = clienteweb.getWeb();
一切都会好起来的。如果您想知道到底是什么地方出了问题,请考虑一下
null
的意思是
empty
nothing
。如果你的
clientweb
是空的,它几乎还不存在,因此没有任何方法,比如
getWeb()
,供你调用

因此,声明:

clientehttp clienteweb = null;
松散的意思是“我声明了一个类型为
clienthttp
的变量,并将其初始化为nothing”

一旦实例化了它(使用
new
表达式),它就会变成“filled”,您可以使用它的方法。要了解更多信息,您应该了解什么是
null
,以及java对象是如何使用的

此外,您可能还应该使用Java的标准命名约定来使代码更干净。 您应该添加一个特定的约定,即使用大写字母命名所有类(如果您正在使用Eclipse,它可能已经警告您这样做)


因此,将类
clienthttp
重命名为
clienthttp
,以便更清楚地知道它是一个类。最简单的方法是右键单击
clienthttp.java
并进行折射/重命名,更改将应用于所有位置。

那么错误是什么?请发布日志猫。谢谢,它很有魅力,但我不明白如果我创建了这种类型的新变量,为什么要添加新的clienthttp()。@user1532116这就是原因。使用
new
表达式创建新对象。欢迎:)阅读我在答案中发布的链接,了解更多信息。