小编Dex*_*ter的帖子

Android ListView addHeaderView()nullPointerException用于在XML中定义的预定义视图

试图使用addHeaderView()addFooterView()ListView.如果我尝试使用我在XML中为页眉或页脚预定义的视图,我会得到一个空指针异常.但是,如果我使用代码动态创建一个视图,它工作正常......

// This doesn't work... nullPointerException
ListView lv = (ListView) findViewById(R.id.my_list_view);
TextView header = (TextView) findViewById(R.id.my_header);
lv.addHeaderView(header);

// This works fine
ListView lv = (ListView) findViewById(R.id.my_list_view);
TextView header = new TextView(this);
TextView.setHeight(30);
TextView.setText("my header text!");
lv.addHeaderView(header);
Run Code Online (Sandbox Code Playgroud)

我的堆栈跟踪:

Caused by: java.lang.NullPointerException
    at android.widget.ListView.clearRecycledState(ListView.java:522)
    at android.widget.ListView.resetList(ListView.java:508)
    at android.widget.ListView.setAdapter(ListView.java:440)
    at com.company.myapp.MyActivity.refreshList(MyActivity.java:85)
    at com.company.myapp.MyActivity.onCreate(MyActivity.java:37)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)
    ... 11 more
Run Code Online (Sandbox Code Playgroud)

有线索吗?

android listview exception

21
推荐指数
1
解决办法
3万
查看次数

可以在类的构造函数中使用"new"来调用Java中的另一个构造函数吗?

我知道这this(...)用于从另一个构造函数调用一个类的构造函数.但我们可以使用new相同的吗?

为了更清楚这个问题,Line-2是否有效?如果是(因为编译器没有投诉),为什么输出null不是Hello

class Test0 {
    String name;

    public Test0(String str) {
        this.name= str;
    }

    public Test0() {
        //this("Hello");    // Line-1
        new Test0("Hello"){}; // Line-2
    }

    String getName(){
        return name;
    }
}

public class Test{
    public static void main(String ags[]){
        Test0 t = new Test0();
        System.out.println(t.getName());
    }
}
Run Code Online (Sandbox Code Playgroud)

java constructor

12
推荐指数
3
解决办法
1999
查看次数

RESTful URI 尾部斜杠或无尾部斜杠

当涉及到 Restful URI 上的尾部斜杠时,我可以引用权威立场吗?罗伊菲尔丁的一个会很棒。网络有两种方式的权威意见。这两个位置是: 尾部斜杠表示资源,而没有则没有。另一个论点是尾部斜杠没有语义价值。是哪个?例子:

  @GetMapping(path = "/users/")
  public List<User> getUsers() {
   ....
  }

  @GetMapping(path = "/users/{id}")
  public User getUser(@PathVariable String type)  {
   .....
  }

  @PutMapping(path = "/users/")
  public User updateUser(@RequestBody User user) {
   ....
  }

  @PostMapping(path = "/users/")
  public User createUser(@RequestBody User user) {
   ....
  }

  @DeleteMapping(path = "/users/{id}")
  public void deleteUser(@PathVariable Long id) {
   ....
  } 
Run Code Online (Sandbox Code Playgroud)

应该删除尾部斜杠吗?

rest restful-url

9
推荐指数
2
解决办法
5999
查看次数

Android:错误"错配发件人ID",同时通过PHP和GCM将通知推送到Android设备

我正试图通过我的.php页面向我的手机发送通知...一切都设置正确,但我收到错误:

{"multicast_id":7751536172966571167,"success":0,"failure":1,"canonical_ids":0,"results":[{"error":"MismatchSenderId"}]}
Run Code Online (Sandbox Code Playgroud)

我不知道为什么因为发件人ID是对的,api也是(我已经尝试过服务器密钥和浏览器密钥,只是为了确定).

我真的不知道我哪里错了!

在我的应用程序中我只有发件人ID,所有都已经正确,在我的服务器中我已经获得了浏览器的密钥(现在):

<?php require_once("../pi_classes/commonSetting.php");
include('../pi_classes/User.php');
ini_set("display_errors",1);
class GCM{
    function __construct(){}
    public function send_notification($registatoin_ids,$message){
        // GOOGLE API KEY
        define("GOOGLE_API_KEY","xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
        $url="https://android.googleapis.com/gcm/send";
        $fields=array(
            "registration_ids"=>$registatoin_ids,
            "data"=>$message,
        );
        var_dump($fields);
        $headers=array(
            "Authorization: key=".GOOGLE_API_KEY,
            "Content-Type: application/json"
        );
        $ch=curl_init();
        curl_setopt($ch,CURLOPT_URL,$url);
        curl_setopt($ch,CURLOPT_POST,true);
        curl_setopt($ch,CURLOPT_HTTPHEADER,$headers);
        curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
        curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
        curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode($fields));
        $result=curl_exec($ch);
        if($result===FALSE){
            die("Curl failed: ".curl_error($ch));
        }
        curl_close($ch);
        echo $result;
    }
}
// ======================
//=INVIA LE NOTIFICHE AGLI UTENTI =
// ======================
$messaggio="Ciao, sono una notifica!";
$pushCounter=0;
$registatoin_ids=array();
$result=mysql_query("SELECT android_regi_id FROM user_details");
while($row=mysql_fetch_array($result)){
    $token=$row["android_regi_id"];
    if($token!=""){
        $registatoin_ids[]=$token;
        $pushCounter++;
    }
}
if($pushCounter>0){
    $gcm=new …
Run Code Online (Sandbox Code Playgroud)

android push mismatch sender-id google-cloud-messaging

5
推荐指数
1
解决办法
1万
查看次数

为什么setText()在TextView上的以下代码中不起作用?

// All required Imports

public class PuzzleActivity extends  Activity implements OnClickListener{
    private PuzzlerDB db; 
    private Puzzle puz; 

    private int puzId=1;     
    private String puzAns="";
    private int score=0;

    private TextView tvPuzContent;
    private EditText etPuzAns;
    private Button btnCheck;

   @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
        final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE); 

        setContentView(R.layout.activity_puzzle);

        if ( customTitleSupported ) {
            getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title);
        }

        db = new PuzzlerDB(this);
        puz = db.getPuzzle(puzId); 
        puzAns = puz.getAnswer(); 

        tvPuzContent = (TextView) findViewById(R.id.tv_puz_content);
        tvPuzContent.setText(puz.getContent());
        btnCheck = (Button) findViewById(R.id.btn_check);
        btnCheck.setOnClickListener(this);   

        android.util.Log.v("IN SIDE …
Run Code Online (Sandbox Code Playgroud)

android textview settext android-layout

4
推荐指数
1
解决办法
1万
查看次数

为什么在GitHub“拉动请求审核”中禁用“批准”按钮?

问题背景:

到目前为止,我是GitHub存储库的唯一所有者。我将代码从系统提交到了一个名为release的分支(实际上,如果需要的话,它是基于develop分支的标记)。在GitHub中,我创建了一个pull请求,其中base分支是master分支,compare分支是release分支。

但是我只能对评论发表评论,而不能批准。可能是什么原因?

注意:没有合并冲突,并且在创建拉取请求时,显示可能合并的绿色信号。

在此处输入图片说明

这是master分支受我保护的方式:

在此处输入图片说明

git github pull-request

4
推荐指数
3
解决办法
932
查看次数

如何用多个手指在画布上绘图

我正在使用Canvas创建绘图应用程序的 android 类。这是我第一次尝试使用 Canvas 类。到目前为止,我使用的代码工作正常,绘图工作正常。但我在这段代码中意识到,它允许用户仅用一根手指进行绘画,我的意思是说,如果用户使用多于一根手指在画布上绘画,则不允许用户用多根手指进行绘画。我浏览了有关多个触摸事件的文档,但未能在我的代码中实现它。那么有人可以帮我解决这个问题吗?

我用于在画布上绘图的代码:

public class DrawView extends View implements OnTouchListener 
{
    private Canvas      m_Canvas;
    
    private Path        m_Path;
    
    private Paint       m_Paint;
    
    ArrayList<Pair<Path, Paint>> arrayListPaths = new ArrayList<Pair<Path, Paint>>();
    
    ArrayList<Pair<Path, Paint>> undonePaths = new ArrayList<Pair<Path, Paint>>(); 
    
    private float mX, mY;
    
    private Bitmap bitmapToCanvas;
    
    private static final float TOUCH_TOLERANCE = 4;
        
    public DrawView(Context context) 
    {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);      
        this.setOnTouchListener(this);
        
        onCanvasInitialization();
    }      
    
    public void onCanvasInitialization()
    {
        m_Paint = new Paint();
        m_Paint.setAntiAlias(true);
        m_Paint.setDither(true);
        m_Paint.setColor(Color.parseColor("#37A1D1"));
        m_Paint.setStyle(Paint.Style.STROKE);
        m_Paint.setStrokeJoin(Paint.Join.ROUND);
        m_Paint.setStrokeCap(Paint.Cap.ROUND);
        m_Paint.setStrokeWidth(2);      
                
        m_Path = …
Run Code Online (Sandbox Code Playgroud)

android multi-touch android-canvas

2
推荐指数
1
解决办法
4695
查看次数