在activity_main中出现的对象为null。 我检查了对象和ID。我不明白我看不到的东西。
public class MainActivity extends AppCompatActivity
{
Button buttonSend;
TextView textChat;
EditText texSendMessage;
ScrollView scrollView;
Login login;
TextView textSocket;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
login = new Login();
startActivity(new Intent(this, Login.class));
buttonSend = (Button) findViewById(R.id.button_send);
textChat = (TextView) findViewById(R.id.text_chat);
texSendMessage = (EditText) findViewById(R.id.text_sendMessage);
scrollView = (ScrollView) findViewById(R.id.scroll_view);
textSocket = (TextView) findViewById(R.id.text_socket);
OkHttpClient client = new OkHttpClient();
String URL = "https://reqres.in/api/users?page=2";
Request request = new Request.Builder()
.url(URL)
.build();
client.newCall(request).enqueue(new Callback()
{
@Override
public void onFailure(Call call, IOException e)
{
e.printStackTrace();
}
@Override
public void onResponse(Call call, Response response) throws IOException
{
if (response.isSuccessful())
{
final String myResponse = response.body().string();
MainActivity.this.runOnUiThread(new Runnable()
{
@Override
public void run()
{
textSocket.setText(myResponse);
}
});
}
}
});
buttonSend.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View v)
{
Toast.makeText(getApplicationContext(), "Boş metin gönderilemez", Toast.LENGTH_SHORT).show();
}
});
buttonSend.setOnClickListener(v -> {
if (!searchString(texSendMessage.getText().toString()))
{
Toast.makeText(getApplicationContext(), "Boş metin gönderilemez", Toast.LENGTH_SHORT).show();
texSendMessage.getText().clear();
}
if (texSendMessage.getText().length() == 0 || texSendMessage.getText().toString().equals(""))
{
Toast.makeText(getApplicationContext(), "Boş metin gönderilemez", Toast.LENGTH_SHORT).show();
}
else
{
Log.e("TAG", "texSendMessage Boyut : " + texSendMessage.getText().length());
textChat.append(Login.getShPref().getString("USERNAME", "null") + ": " + texSendMessage.getText().toString() + '\n');
Log.e("LOG", "texSendMessage buttonSend : /" + texSendMessage.getText().toString() + "/");
texSendMessage.getText().clear();
scrollView.post(new Runnable()
{
@Override
public void run()
{
scrollView.smoothScrollTo(0, textChat.getBottom());
}
});
}
});
}
// arama fonksiyonu ile bosluk veya yeni satırların sayısını tutup
// sadece bosluk, sadece yeni satır veya ikisinden oluşan metinlerin gönderilmesini engeller.
boolean searchString(String s)
{
String[] strings = s.split("");
int count = 1;
for (int i = 0; i < s.length(); i++)
{
if (strings[count].equals(" ") || strings[count].equals("\n"))
{
count++;
}
}
return count - 1 != s.length();
}
}
输出结果为:
进程:com.example.tunca.localconnect,PID:31163 java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.tunca.localconnect / com.example.tunca.localconnect.MainActivity}: java.lang.NullPointerException:尝试调用虚拟方法'void android.widget .Button.setOnClickListener(android.view.View $ OnClickListener)'对空对象的引用 在上android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2858) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1612) 在android.os.Handler.dispatchMessage(Handler.java:105) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6710) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770) 原因:java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法'void android.widget.Button.setOnClickListener(android.view.View $ OnClickListener)' 在com.example.tunca.localconnect.MainActivity.onCreate(MainActivity.java:86) 在android.app.Activity.performCreate(Activity.java:6980) 在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214) 在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2811) 在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2933) 在android.app.ActivityThread.-wrap11(未知来源:0) 在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1612) 在android.os.Handler.dispatchMessage(Handler.java:105) 在android.os.Looper.loop(Looper.java:164) 在android.app.ActivityThread.main(ActivityThread.java:6710) 在java.lang.reflect.Method.invoke(本机方法) 在com.android.internal.os.Zygote $ MethodAndArgsCaller.run(Zygote.java:240) 在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
答案 0 :(得分:0)
首先清除您的问题,这是一个难以理解的代码,但是我仍然可以为您提供解决问题的方法。
为什么您创建了一个在任何地方都没有使用过的Login类, login = new Login();
这应该在click事件上执行,您还已经在onCreate方法中调用了该intent Activity类必须添加到Manifest文件中。 startActivity(new Intent(this,Login.class));
在点击事件中也应调用网络通话。
希望您对错误有所了解。