本文介绍了OnClick(视图v)抛出空指针异常。有人可以帮忙吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

单击(查看v)抛出空指针异常。一直试图将它分类几个小时。但无济于事。如果有人能帮助我解决这个问题,我会很高兴。



谢谢你的时间。



click(view v) is throwing a null pointer exception. have been trying to sort it for hours. but to no avail. I would appriciate if anyone could help me sort this issue.

Thanks for you time.

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends ActionBarActivity
{
   ankita db1 = null;

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

      if (savedInstanceState == null)
      {
         getSupportFragmentManager().beginTransaction().add(R.id.container, new PlaceholderFragment()).commit();

         //add();
      }

      //TextView txt1 = (TextView)findViewById(R.id.userName);
      //TextView txt2 = (TextView)findViewById(R.id.password);

      db1 = new ankita(this);
      Log.e("error", "before db create");
      db1.CreateDb();
      Log.e("error","after db create");
      add();
      //db1.insertrecord();
   }

public void add()
{
   Log.e("hello", "entered db insert");
   // TODO Auto-generated method stub
   Button addb= (Button) findViewById(R.id.add);
   try
   {
      addb.setOnClickListener(new View.OnClickListener()
      {
         @Override
         public void onClick(View v)
         {
            Log.e("hello", "entered db click");
            // TODO Auto-generated method stub
            TextView txt1 = (TextView) findViewById(R.id.userName);
            EditText txt = (EditText) findViewById(R.id.userName);

            TextView txt2 = (TextView) findViewById(R.id.password);
            Log.e("hello", "entered db insert1");
            String uname = (String) txt1.getText();
            Log.e("hello", "entered db insert2");
            String pass = (String) txt2.getText();
            Log.e("hello", "entered db insert3");
            db1.insertrecord(uname,pass);

         }
      });}
   catch(Exception e)
   {
      System.out.println("helloji"+e);
   }
}





修正了代码块并减少了制表



code block corrected and tabulation reduced

推荐答案

TextView txt1 = (TextView)	findViewById(R.id.userName);
			EditText txt = (EditText) findViewById(R.id.userName);



两者都使用相同的ID。可能


Both use the same id. Probably

EditText txt

为空。使用正确的ID来修复它。

is null. use the correct id to fix it.


这篇关于OnClick(视图v)抛出空指针异常。有人可以帮忙吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-12 01:09